1.在lvs的master节点上设置:
[root@vm1 ~]# yum install keepalived -y
[root@vm1 ~]# vim /etc/keepalived/keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
notification_email {
-
yhai_ying@163.com
-
}
-
notification_email_from sns-lvs@gmail.com
-
smtp_server 127.0.0.1
-
# smtp_connect_timeout 30
-
router_id LVS_DEVEL
-
}
-
-
#written by :netseek
-
# VIP1
-
vrrp_instance VI_1 {
-
state MASTER #备份服务器上将MASTER改为BACKUP
-
interface eth0
-
virtual_router_id 51
-
priority 100 # 备份服务上将100改为99
-
advert_int 1
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
192.168.1.188
-
#(如果有多个VIP,继续换行填写.)
-
}
-
}
-
-
virtual_server 192.168.1.188 80 {
-
delay_loop 6 #(每隔10秒查询realserver状态)
-
lb_algo wrr #(lvs 算法)
-
lb_kind DR #(Direct Route)
-
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
-
protocol TCP #(用TCP协议检查realserver状态)
-
-
real_server 192.168.1.121 80 {
-
weight 3 #(权重)
-
TCP_CHECK {
-
connect_timeout 10 #(10秒无响应超时)
-
nb_get_retry 3
-
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.122 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@vm1 ~]# /etc/init.d/keepalived start
2.在lvs的backup节点上设置:
[root@vm4 ~]# yum install keepalived -y
[root@vm4 ~]# vim /etc/keepalived/keepalived.conf
-
! Configuration File for keepalived
global_defs {
notification_email {
yhai_ying@163.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
#written by :netseek
# VIP1
vrrp_instance VI_1 {
state BACKUP #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 99 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.188
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 192.168.1.188 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.1.121 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.122 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
下面我们做个测试:
配置完成后我们停掉master上的keepalived服务然后去查看日志文件,看看backup节点是否接管服务:
[root@vm1 ~]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
[root@vm1 ~]# tail -f /var/log/messages
Apr 20 07:05:43 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 20 07:05:44 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:05:49 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:08:10 vm1 Keepalived[1160]: Stopping Keepalived v1.2.7 (09/26,2012)
Apr 20 07:08:10 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) sending 0 priority
Apr 20 07:08:10 vm1 Keepalived_vrrp[1163]: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 20 07:08:10 vm1 Keepalived_healthcheckers[1162]: Netlink reflector reports IP 192.168.1.188 removed
Apr 20 07:08:10 vm1 kernel: IPVS: __ip_vs_del_service: enter
我们可以看到keepalived健康检查已经被移除。
再来看看backup的日志文件:
[root@vm4 ~]# tail -f /var/log/messages
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: IPVS: Destination already exists
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: IPVS: Destination already exists
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Using LinkWatch kernel netlink reflector...
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Activating healthchecker for service [192.168.1.121]:80
Apr 20 07:05:58 vm4 Keepalived_healthcheckers[1180]: Activating healthchecker for service [192.168.1.122]:80
Apr 20 07:08:10 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 20 07:08:11 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
Apr 20 07:08:16 vm4 Keepalived_vrrp[1181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.188
之前backup节点一直在监视master的keepalived服务的状态,当master节点上健康检查出现故障的时候,backup自动接管服务。
阅读(1071) | 评论(0) | 转发(0) |