实验目的:使用keepalived实现lvs-dr模型双主高可用集群
实验环境:两台virtual server(实现lvs的双主)、两台real server(安装web service,用于负载均衡)、一台clietn用于验证结果
注意:为了不影响实验结果,在实验开始前先关闭iptables和selinux
操作步骤:
一、配置IP
1.配置A主机的IP
# ip addr add dev eth0 192.168.10.2/24
2.配置B主机的IP
# ip addr add dev eth0 192.168.10.23/24
3.配置C主机的IP,修改内核参数和回环的IP
# ip addr add dev eth0 192.168.10.3/24
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
# ip addr add dev lo 192.168.10.200/32 label lo:0 broadcast 192.168.10.200
# ip addr add dev lo 192.168.10.201/32 label lo:1 broadcast 192.168.10.201
4.配置D主机的IP,修改内核参数和回环的IP
# ip addr add dev eth0 192.168.10.33/24
# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
# ip addr add dev lo 192.168.10.200/32 label lo:0 broadcast 192.168.10.200
# ip addr add dev lo 192.168.10.201/32 label lo:1 broadcast 192.168.10.201
二、配置web
service(C和D主机做同样配置,默认主页的内容中的IP地址改为D主机的IP即可)
1.安装apache
# yum -y install httpd
2.创建默认主页
# vim /var/www/html/index.html
<h1>R1:192.168.10.3</h1>
3.启动服务
# service httpd start
三、配置keepalived,实现双主高可用
1.安装keepalived
# yum -y install keepalived
2.编辑A主机的配置文件/etc/keepalived/keepalived.conf,作如下配置:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id centos6
vrrp_mcast_group4 224.0.100.39
}
vrrp_instance lvs {
state MASTER
interface eth0
virtual_router_id 14
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass MDQ41fTp
}
virtual_ipaddress {
192.168.10.200/24 dev eth0
}
}
vrrp_instance lvs2 {
state BACKUP
interface eth0
virtual_router_id 15
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass XYZ41fTp
}
virtual_ipaddress {
192.168.10.201/24 dev eth0
}
}
virtual_server 192.168.10.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.10.3 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.10.33 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
}
virtual_server 192.168.10.201 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.10.3 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.10.33 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
}
B主机也作同样配置,稍作修改即可,需要修改的地方如下:
vrrp_instance lvs {
state BACKUP
priority 98
}
vrrp_instance lvs2 {
state MASTER
priority 100
}
四、配置sorry_server(在两台RS都不能正常提供服务且有用户访问时,sorry主机可显示告知信息),sorry_server需要配置在virtual server上(两台RS都做同样配置,默认主页中的IP地址改为本机的IP以示区分。)
1.安装apache
# yum -y install httpd
2.创建默认主页
# vim /var/www/html/index.html
<h1>sorry_server:192.168.10.2</h1>
3.启动服务
# service httpd start
五、模拟故障,验证结果(由于没有配置路由,因此client主机并不能访问,可在服务器网段中选择任意一台主机测试)
1.启动两台VS的keepalived服务
# service keepalived start
2.查看生成的lvs规则,如图有两组规则
# ipvsadm -ln
3.访问192.168.10.200,结果应是后端的RS轮询响应请求
4.访问192.168.10.201,结果应是后端的RS轮询响应请求
5.将后端RS的web服务关闭一台,此时访问192.168.10.200或192.168.10.201,响应请求的将只是另一台web服务正常运行的RS
6.将后端RS的web服务都关闭,此时访问192.168.10.200或192.168.10.201,响应请求的将只是各自的主server中定义的sorry_server
7.关闭一台VS的keepalived服务,此时访问192.168.10.200或192.168.10.201,响应的请求的将只是另一台正常运行keepalived服务的VS
原创文章,作者:人字拖,如若转载,请注明出处:http://www.178linux.com/75455