一、keepalived介绍及搭建环境说明 1、The keepalived The keepalived server implements the vrrpd routing demon which enables routing failover for a pair (or set) of routers (or LVS directors) and the keepalived demon which sets up and does the health checking of virtual services in a Linux Virtual Servier. keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称VRRP) 2、官网 http://www.keepalived.org/ 3、master/slave ip和版本和虚拟ip 1)master ip 10.1.10.117 2)master version cat /etc/debian_version 7.8 3)slave ip 10.1.10.250 4)slave version cat /etc/debian_version 5.0.1 5)virtual ip 10.1.10.240 二、debian下安装和配置keepalived 1、先查询下 apt-cache search keepalived keepalived - Failover and monitoring daemon for LVS clusters 2、apt方式安装keepalived服务 apt-get -y install keepalived --force-yes 3、配置 1、apt方式安装后发现/etc/keepalived下是没有配置文件的 可以从/usr/share/doc/keepalived/samples复制样本配置文件 ll /usr/share/doc/keepalived/samples/keepalived.conf.sample -rw-r--r-- 1 root root 919 Sep 28 2009 /usr/share/doc/keepalived/samples/keepalived.conf.sample 4、查看模板配置 cat /usr/share/doc/keepalived/samples/keepalived.conf.sample ! Configuration File for keepalived global_defs { notification_email { acassen } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { interface eth0 virtual_router_id 50 nopreempt priority 100 advert_int 1 virtual_ipaddress { 192.168.200.11 192.168.200.12 192.168.200.13 } } virtual_server 10.10.10.2 1358 { delay_loop 6 lb_algo rr lb_kind NAT persistence_timeout 50 protocol TCP sorry_server 192.168.200.200 1358 real_server 192.168.200.2 1358 { weight 1 HTTP_GET { url { path /testurl3/test.jsp digest 640205b7b0fc66c1ea91c463fac6334d } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 5、复制模板配置到/etc/keepalived/ cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf 6、修改master/slave配置 1)master cat /etc/keepalived/keepalived.conf global_defs { router_id aaa } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 90 nopreempt priority 100 advert_int 1 virtual_ipaddress { 10.1.10.240/24 } } 2)slave cat /etc/keepalived/keepalived.conf global_defs { router_id bbb } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 90 priority 99 advert_int 1 virtual_ipaddress { 10.1.10.240/24 } } 7、配置说明 state #MASTER是主服务器,BACKUP是备服务器。 virtual_ipaddress #设置虚拟IP地址,也叫漂移IP地址。 priority #值越大优先级越高,主的值在设置上大于备的值 nopreempt #不抢占 virtual_router_id #取值范围是1-255(must be between 1 & 255) advert_int #MASTER和BACKUP之间同步检查的时间间隔(s) 8、自定义日志存放地方(不定义的话默认是在/var/log/messages) 1)cat /etc/rsyslog.conf(增加在最后) local0.* /var/log/keepalived 2)生效的话需要重启下rsyslog服务 /etc/init.d/rsyslog restart [ ok ] Stopping enhanced syslogd: rsyslogd. [ ok ] Starting enhanced syslogd: rsyslogd. 3)查看 ll /var/log/keepalived -rw-r----- 1 root adm 0 May 24 09:14 /var/log/keepalived 9、启动服务 keepalived -d -D -S 0 -f /etc/keepalived/keepalived.conf 10、查看进程 ps aux |grep keepalived |grep -v grep root 2234 0.0 0.0 16408 728 ? Ss 09:16 0:00 keepalived -d -D -S 0 -f /etc/keepalived/keepalived.conf root 2235 0.0 0.1 24824 1796 ? S 09:16 0:00 keepalived -d -D -S 0 -f /etc/keepalived/keepalived.conf root 2236 0.2 0.1 24824 1188 ? S 09:16 0:00 keepalived -d -D -S 0 -f /etc/keepalived/keepalived.conf 11、查看启动日志(master/slave) 1)cat /var/log/keepalived(master这里只截取部分日志信息) May 25 01:50:10 debian Keepalived: Starting Keepalived v1.2.2 (12/18,2011) May 25 01:50:10 debian Keepalived: Starting Healthcheck child process, pid=3323 May 25 01:50:10 debian Keepalived: Starting VRRP child process, pid=3324 May 25 01:50:10 debian Keepalived_vrrp: Netlink reflector reports IP 10.1.10.117 added May 25 01:50:11 debian Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE May 25 01:50:12 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE May 25 01:50:12 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.1.10.240 May 25 01:50:12 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.240 added 2)cat /var/log/keepalived(slave这里只截取部分日志信息) May 25 01:54:01 debian Keepalived:Starting Keepalived v1.1.15 (12/20,2007) May 25 01:54:01 debian Keepalived_healthcheckers: Using MII-BMSR NIC polling thread... May 25 01:54:01 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.250 added May 25 01:54:01 debian Keepalived_vrrp: 10.1.10.240/24 brd 10.1.10.240 dev eth0 scope global May 25 01:54:01 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE May 25 01:54:01 debian Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)] 12、使用ip命令查看(master) 1) eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:87:a1 brd ff:ff:ff:ff:ff:ff inet 10.1.10.117/24 brd 10.1.10.255 scope global eth0 inet 10.1.10.240/24 scope global secondary eth0 inet6 fe80::20c:29ff:fec8:87a1/64 scope link valid_lft forever preferred_lft forever 13、启动参数说明 --use-file, -f keepalived.conf_file Use the specified configuration file. --dump-conf, -d Dump the configuration data. --log-detail, -D Detailed log messages (the default with the rc script provided). --log-facility, -S 0-7 Set syslog facility to LOG_LOCAL[0-7] (default=LOG_DAEMON) 14、设置开机启动 1)查看当前运行级别 runlevel N 2 2)安装时已经自动加入了 ll /etc/rc2.d/ |grep keepalived lrwxrwxrwx 1 root root 20 2015-05-22 05:06 S20keepalived -> ../init.d/keepalived 三、测试 1)在master上执行/etc/init.d/keepalived stop可以看到日志 May 25 02:11:22 debian Keepalived: Terminating on signal May 25 02:11:22 debian Keepalived: Stopping Keepalived v1.2.2 (12/18,2011) May 25 02:11:22 debian Keepalived_vrrp: Terminating VRRP child process on signal May 25 02:11:22 debian Keepalived_healthcheckers: Terminating Healthchecker child process on signal May 25 02:11:22 debian Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs. 执行ip命令查看 lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:87:a1 brd ff:ff:ff:ff:ff:ff inet 10.1.10.117/24 brd 10.1.10.255 scope global eth0 inet6 fe80::20c:29ff:fec8:87a1/64 scope link valid_lft forever preferred_lft forever 2)在slave上查看日志 May 25 02:11:25 debian Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE May 25 02:11:26 debian Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE May 25 02:11:26 debian Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. May 25 02:11:26 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 0.0.0.0 May 25 02:11:26 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.1.10.240 May 25 02:11:26 debian Keepalived_vrrp: Netlink: skipping nl_cmd msg... May 25 02:11:26 debian Keepalived_healthcheckers: Netlink reflector reports IP 10.1.10.240 added May 25 02:11:31 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 0.0.0.0 May 25 02:11:31 debian Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.1.10.240 执行ip命令查看 lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:8c:61:6e brd ff:ff:ff:ff:ff:ff inet 10.1.10.250/24 brd 10.1.10.255 scope global eth0 inet 10.1.10.240/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe8c:616e/64 scope link valid_lft forever preferred_lft forever
本文出自 “7928217” 博客,请务必保留此出处http://7938217.blog.51cto.com/7928217/1654949
原文地址:http://7938217.blog.51cto.com/7928217/1654949