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自动接管服务。
阅读(9948) | 评论(0) | 转发(1) |