这是一篇以apcache服务为例介绍lvs的nat模式配合keepalived实现的方案。实验拓扑图如下所示,虚线上面的大图可以看成是虚线下面“服务器群”的放大版:
本实验共用到4台linux虚拟服务器,其中两台rhel5.6作为主从HA(keepalived)服务器,另外两台rhel4.6模拟Apache服务器——用两台Apache服务器模拟多台Apache服务器。
实验原理是,用Apache服务器为代表模拟实际用到的服务器,用两台Apache模拟多台Apache,所有的Apache服务器均采用轮询机制提供服务,当其中一台Apache有故障将被自动剔除集群。当主HA宕机,从HA自动变成主HA继续提供服务,也就是这两台HA服务器(keepalived服务)同时只允许一台HA在线服务,另外一台作为备胎,此时的HA做为分发器使用,其上面配有两个虚拟ip地址,一个是公有虚拟ip(这里用10.10.16.143作为公有虚拟ip)——遥远的用户要访问本公司的网站就输入这个地址,一个是私有虚拟ip(这里用192.168.65.143作为私有虚拟ip)——集群里面的各Apache服务器的网关地址就是这个ip。
实验过程:
记得先搭建realserver(也就是Apache)服务器,再搭建keepalived服务器,这样在keepalived服务器上安装ipvsadm软件包时就不需要再次编译就可以监控到realserver地址
- 搭建RS1的Apache服务器
在realserver服务器的操作比较简单
#yum install httpd
#chkconfig --level 35 httpd on
#echo "RS1 192.168.65.133" > /var/www/html/index.html
#echo "ok" > /var/www/html/check.html
#service httpd start
到这里RS1配置完成。
- 搭建RS2的Apache服务器
#yum install httpd
#chkconfig --level 35 httpd on
#echo "RS2 192.168.65.130" > /var/www/html/index.html
#echo "ok" > /var/www/html/check.html
#service httpd start
到这里RS2配置完成。
3.搭建主HA服务器
不论是主从HA都添加两块网卡(eth0、eth1)
[[email protected]_master ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:03:54:66
inet addr:10.10.16.29 Bcast:10.10.16.255 Mask:255.255.255.0
[[email protected]_master ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.65.11
NETWORK=192.168.65.0
NETMASK=255.255.255.0
BROADCAST=192.168.65.255
ONBOOT=yes
HWADDR=00:0c:29:03:54:70
[[email protected]_master ~]# service iptables stop
[[email protected]_master ~]# setenforce 0
[[email protected]_master ~]# yum install gcc gcc-c++ openssl-devel kernel-devel -y
[[email protected]_master ~]# tar zxf keepalived-1.1.20.tar.gz
[[email protected]_master ~]# cd keepalived-1.1.20
[[email protected]_master ~]# ./configure --sysconfdir=/etc --with-kernel-dir=/usr/src/kernels/2.6.18-238.el5-i686/
[[email protected]_master ~]# make &&make install
[[email protected]_master ~]# cp /usr/local/sbin/keepalived /usr/sbin
[[email protected]_master ~]# genhash -s 192.168.65.133 -p 80 -u /check.html ####算出RS1的哈希值,将该值复制到keepalived配置文件对应的path下面
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd
[[email protected]_master ~]# genhash -s 192.168.65.130 -p 80 -u /check.html ####算出RS1的哈希值,将该值复制到keepalived配置文件对应的path下面
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd
在从HA服务器上分别做这两个realserver的哈希值和在主的上面是一样的,由于在两台realserver的Apache的/var/www/html目录下的check.html文件内容是一样的(都是“ok”),所以两个哈希值也是一样的。
[[email protected]_master ~]# cp keepalived.conf keepalived.conf_bak
[[email protected]_master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance apache_1 {
state MASTER
interface eth0
virtual_router_id 22
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.10.16.143
}
}
vrrp_instance apache_2 {
state MASTER
interface eth1
virtual_router_id 22
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.65.143
}
}
virtual_server 10.10.16.143 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 0 ####默认的连接持续时间是300秒,即一个连接在不做任何操作的300秒后会断开操作,这里为了实验轮询的更明显,将300秒改成0秒
protocol TCP
real_server 192.168.65.133 80 {
weight 1
HTTP_GET {
url {
path /check.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd ####将上面对192.168.65.133做的哈希值拷贝到这里
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.65.130 80 {
weight 1
HTTP_GET {
url {
path /check.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd ####将上面对192.168.65.133做的哈希值拷贝到这里
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
# real_server 192.168.65.133 80 {
#weight 1
#TCP_CHECK{
# connect_port 80
# connect_timeout 3
# nb_get_retry 3
# delay_before_retry 3
# }
# }
# real_server 192.168.65.130 80 {
#weight 1
#TCP_CHECK{
# connect_port 80
# connect_timeout 3
# nb_get_retry 3
# delay_before_retry 3
#}
# }
}
[[email protected]_master ~]# service keepalived start
[[email protected]_master ~]# chkconfig --level 35 keepalived on
[[email protected]_master ~]# tar zxf ipvsadm-1.24.tar.gz
[[email protected]_master ~]# cd ipvsadm-1.24
[[email protected]_master ~]# ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux
[[email protected]_master ~]# make &&make install
到这里,ipvsadm命令可以调用出来了,现在用ipvsadm命令查看realserver状态:
[[email protected]_master ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.16.143:80 rr
-> 192.168.65.130:80 Masq 1 0 0
-> 192.168.65.133:80 Masq 1 0 0
输入ipvsadm -ln会自动显示出realserver状态,说明到了这里主HA以及Apache服务器均基本配置完成。还差一步,因为主从HA服务器都安装了两块网卡,要打开HA服务器上面的ipv4包路由功能:
[[email protected]_master ~]# cat /etc/sysctl.conf
net.ipv4.ip_forward = 1 ####将该行的0改为1,代表包转发功能打开,用户可以通过这个HA服务器访问到内部的服务器
[[email protected]_master ~]# sysctl -p ####输入sysctl -p 使刚才的更改生效
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
好了,到这里,主HA服务器配置完成。
4.搭建从HA服务器
从HA服务器和主HA服务器配置步骤和内容基本相同
[[email protected]_slave ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:F8:73:52
inet addr:10.10.16.91 Bcast:10.10.16.255 Mask:255.255.255.0
[[email protected]_slave ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.65.22
NETWORK=192.168.65.0
NETMASK=255.255.255.0
BROADCAST=192.168.65.255
ONBOOT=yes
HWADDR=00:0c:29:f8:73:5c
[[email protected]_slave ~]# service iptables stop
[[email protected]_slave ~]# setenforce 0
[[email protected]_slave ~]# yum install gcc gcc-c++ openssl-devel kernel-devel -y
[[email protected]_slave ~]# tar zxf keepalived-1.1.20.tar.gz
[[email protected]_slave ~]# cd keepalived-1.1.20
[[email protected]_slave ~]# ./configure --sysconfdir=/etc --with-kernel-dir=/usr/src/kernels/2.6.18-238.el5-i686/
[[email protected]_slave ~]# make &&make install
[[email protected]_slave ~]# cp /usr/local/sbin/keepalived /usr/sbin
[[email protected]_slave ~]# cp keepalived.conf keepalived.conf_bak
可以将主HA的keepalived配置文件直接拷贝到从HA的keepalived配置文件的目录下,然后稍加改动一下:
[[email protected]_slave ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance apache_1 {
state BACKUP
interface eth0
virtual_router_id 22
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
10.10.16.143
}
}
vrrp_instance apache_2 {
state BACKUP
interface eth1
virtual_router_id 22
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.65.143
}
}
virtual_server 10.10.16.143 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.65.133 80 {
weight 1
HTTP_GET {
url {
path /check.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.65.130 80 {
weight 1
HTTP_GET {
url {
path /check.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
#real_server 192.168.65.133 80 {
#weight 1
#TCP_CHECK{
# connect_port 80
# connect_timeout 3
# nb_get_retry 3
# delay_before_retry 3
# }
# }
# real_server 192.168.65.130 80 {
#weight 1
#TCP_CHECK{
# connect_port 80
# connect_timeout 3
# nb_get_retry 3
# delay_before_retry 3
# }
#}
}
[[email protected]_slave ~]# service keepalived start
[[email protected]_slave ~]# chkconfig --level 35 keepalived on
[[email protected]_slave ~]# tar zxf ipvsadm-1.24.tar.gz
[[email protected]_slave ~]# cd ipvsadm-1.24
[[email protected]_slave ~]# ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux
[[email protected]_slave ~]# make &&make install
[[email protected]_slave ~]# cat /etc/sysctl.conf
net.ipv4.ip_forward = 1
[[email protected]_slave ~]# sysctl -p
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
[[email protected]_slave ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.10.16.143:80 rr
-> 192.168.65.130:80 Masq 1 0 0
-> 192.168.65.133:80 Masq 1 0 0
到这里,从HA服务器配置完成。
下面开始做测试,可以拿另外一台客户机做测试用,这里为了方便就在宿主机的浏览器上和从HA上面做测试操作,因为从HA是备胎不做任何服务。
上图是在宿主机的两个浏览器上访问实验中的“公网虚拟ip”(10.10.16.143)返回的结果,可以看出返回的结果值不一样,表明主HA给连续的http请求做了分发轮询。
上图是在从HA上面用文本浏览器(curl)对“公网虚拟ip”做的访问,从这也可以看出主HA做了轮询。
上图表示,主HA服务器在正常提供服务,从中可以看出,主HA上面的网卡eth0绑定了公网虚拟ip,eth1绑定了私网虚拟ip,当主HA网卡故障或主HA宕机是,这两个虚拟ip又会分别跑到从HA的eth0和eth1上面。
keepalived可以采用在第7层服务上面做高可用分发,也可以采用在第四层做高可用分发,这里从开始到现在keepalived用的一直是在7层上面做高可用,即是通过http做的高可用,通过触发check.html网页文件实现的高可用分发,下面演示当其中一台realserver宕机、网卡损坏或者Apache服务中断,HA服务器会如何选择,实际上HA会把坏的realserver踢出去,实际上keepalived采用七层分发方式,只要realserver出现宕机、网卡损坏、Apache服务停止中的任一种,HA都会把这台realserver服务器踢出去,下面模拟RS1的Apache服务停止的情况:
在HA上面只能监控到一台realserver(也就是RS2),表明RS1被踢出去了,下面用从的HA文本浏览器再次验证RS1已被踢出去:
实验表明,此时,不管用户访问多少次公网虚拟ip,实际上访问的就只是访问RS2这一台机器,因为RS1挂掉了,HA只能分发到RS2这一台了,启动RS1的Apache服务,用户将又可以“同时”访问RS1和RS2了。
现在来描述一下当keepalived工作在4层模式即在传输层靠端口号工作的情况,分别将上面主从HA的keepalived配置文件的#注释取消,并将相应的7层配置代码注释,重启keepalived进行试验,发现,当RS1的Apache服务挂掉时,HA扔将请求分发到RS1上面,同时也将请求分发到RS2上面,当然从RS1返回的是没有结果的,如下图:
此时,如果工作在4层的keepalived也想像工作在7层的keepalived那样灵敏,可以通过写脚本的方式来实现将RS1踢掉。
ok,到现在为止,lvs的nat模式基于rr算法的高可用配置结束。
ps:可以额外的配置一台dns服务器,将虚拟公网ip对应到公司的网站的域名上面,这样在访问的时候直接输入域名就可以不用输入公网虚拟ip了。
linux lvs集群nat模式(比上一篇的lvs nat实用),布布扣,bubuko.com