调度主机:192.168.0.85;192.168.0.52
realserver:192.168.0.96;192.168.0.66 192.168.0.52: 到http://www.keepalived.org/download.html下载源码keepalived,我用的是keepalived-1.2.5.tar.gz [root@desktop52 ~]# yum install kernel-devel openssl-devel libnl-devel popt-devel gcc make 如果不装kernel-devel可以编译成功,但安装后只有调度,没有lvs支持,因为他是调用内核的lvs,编译需要kernel-devel 解压,编译,如果要用snmp和debug也可已先安装再编译 configure后,可以看到Extra Lib:-lpot -lssl -lcrypto -lnl 默认安装目录为/usr/local/etc下,我们需要把相应文件靠到系统目录下(也可已在安装的时候指定安装目录为/etc) [root@desktop52 etc]# pwd /usr/local/etc [root@desktop52 etc]# cp rc.d/init.d/keepalived /etc/init.d/ [root@desktop52 etc]# cp sysconfig/keepalived keepalived/ rc.d/ sysconfig/ [root@desktop52 etc]# cp sysconfig/keepalived /etc/sysconfig/ [root@desktop52 keepalived]# ln -s /usr/local/sbin/keepalived /usr/sbin/ <===因为脚本里面的运行keepalived时只在/usr/sbin里面找 配置keepalived.conf [root@desktop52 keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { XXXX@gmail.com <===管理员邮箱 } notification_email_from keepalived@desktop52 <===通知邮箱from smtp_server 127.0.0.1 <===使用本机转发 email smtp_connect_timeout 30 router_id LVS_DEVEL <== load balancer 的标识 ID,用于 email 警报 } vrrp_instance VI_1 { state MASTER <===备机改为 BACKUP,此状态是由 priority 的值来决定的,当前 priority 的值小于备机的值,那么将会失去 MASTER 状态 interface eth0 <==#HA 监测网络接口 virtual_router_id 57 <==主、备机的 virtual_router_id 必须相同,arbitary unique number 0..255 priority 50 advert_int 1 <===主备之间的通告间隔秒数 authentication { <===主备切换时的验证 auth_type PASS auth_pass 1111 } virtual_ipaddress {<====HA 虚拟 ip,可加多个 192.168.0.136 } } virtual_server 192.168.0.136 80 { delay_loop 6 <===每隔 6 秒查询 realserver状态 lb_algo rr <===lvs 调度算法,这里使用轮叫 lb_kind DR # nat_mask 255.255.255.0 # persistence_timeout 50 <===同一 IP 的连接 60 秒内被分配到同一台 realserver protocol TCP <===同一 IP 的连接 60 秒内被分配到同一台 realserver real_server 192.168.0.96 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 <====故障重试秒数 delay_before_retry 3 <===重试延迟 } } real_server 192.168.0.66 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 备用机上该两个地方: state,priority,而 notification_email_from 可改可不该 [root@desktop52 keepalived]# /etc/init.d/keepalived start Starting keepalived: [ OK ] 启动时可以监控/var/log/messages日志文件,可以看到Activating healthchecker ...:80和Transition to MASTER STATE(备用机为BACKUP STATE) realserver:(两台电脑都一样) [root@desktop66 ~]# ifconfig lo:0 192.168.0.136 netmask 255.255.255.255 up <===lo上是默认是nonarp的所以不用更改内核,如果是别的端口,要注意该下面的内核配置: net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 [root@desktop66 ~]# route add -host 192.168.0.136 dev lo:0 [root@desktop66 ~]# yum install -y httpd [root@desktop66 ~]# vim /var/www/html/index.html [root@desktop66 ~]# /etc/init.d/httpd start [root@desktop85 ~]# ip addr show 1: 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 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 52:54:00:6c:e6:09 brd ff:ff:ff:ff:ff:ff inet 192.168.0.85/24 brd 192.168.0.255 scope global eth0 inet 192.168.0.136/32 scope global eth0 inet6 fe80::5054:ff:fe6c:e609/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 52:54:00:8d:82:cc brd ff:ff:ff:ff:ff:ff 看到我们的eth0中我们配置的136虚拟IP 注:这个我们只能用ip addr show查看调度器在哪台上运行,不想piranha和heartbeat可以ifconfig查看虚拟IP