一、部署环境
keepalived:10.10.10.30(CentOS 7)
lvs1:10.10.10.140(CentOS 6.4)
lvs2:10.10.10.150(CentOS 6.4)
二、在lvs1:10.10.10.140上配置
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# echo "ok" > keep.html
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:15:B7:DC
TYPE=Ethernet
UUID=93f4695a-8641-4360-9fce-5e3af0e32fc9
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.10.10.140
NETMASK=255.255.255.0
GATEWAY=10.10.10.30
[[email protected] ~]#/etc/init.d/network restart
[[email protected] ~]#/sbin/ifconfig eth0:0 10.10.10.140 broadcast 10.10.10.255 netmask 255.255.255.255 up
三、在lvs2:10.10.10.150上配置
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# /etc/init.d/httpd start
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# echo "ok" > keep.html
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:0D:33:AC
TYPE=Ethernet
UUID=e04f57dc-ef9d-4563-bfa4-9c8c1e8fc870
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
IPADDR=10.10.10.150
NETMASK=255.255.255.0
GATEWAY=10.10.10.30
[[email protected] ~]#/etc/init.d/network restart
[[email protected] ~]#/sbin/ifconfig eth0:0 10.10.10.140 broadcast 10.10.10.255 netmask 255.255.255.255 up
四、在keepalived:10.10.10.30上配置
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno33554984
HWADDR=00:0C:29:DC:FE:1B
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#IPV6_DEFROUTE=yes
#IPV6_PEERDNS=yes
#IPV6_PEERROUTES=yes
#IPV6_FAILURE_FATAL=no
NAME=eno33554984
#UUID=56cae8b8-235f-471d-9051-2508ee149e48
ONBOOT=yes
NM_CONTROLLED=no
IPADDR=10.10.10.30
NETMASK=255.255.255.0
[[email protected] ~]# /etc/init.d/network restart
[[email protected] ~]# cd /data/keepalived/
[[email protected] keepalived]# tar zxvf keepalived-1.2.12.tar.gz
[[email protected] keepalived]# cd keepalived-1.2.12/
[[email protected] keepalived-1.2.12]# ./configure --prefix=/usr/local/keepalived
[[email protected] keepalived-1.2.12]# make
[[email protected] keepalived-1.2.12]# make install
[[email protected] keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/
[[email protected] keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.2.2]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived-1.2.2]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
[[email protected] keepalived-1.2.12]# cd
##keepalived健康检查 HTTP_GET
[[email protected] ~]# /usr/local/keepalived/bin/genhash -s 10.10.10.140 -p 80 -u /keep.html
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd
[[email protected] ~]# /usr/local/keepalived/bin/genhash -s 10.10.10.150 -p 80 -u /keep.html
MD5SUM = eff5bc1ef8ec9d03e640fc4370f5eacd
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eno33554984
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.10.250
}
}
virtual_server 10.10.10.250 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.10.10.140 80 {
weight 1
HTTP_GET {
url {
path /keep.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 10.10.10.150 80 {
weight 1
HTTP_GET {
url {
path /keep.html
digest eff5bc1ef8ec9d03e640fc4370f5eacd
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
[[email protected] ~]# /etc/init.d/keepalived restart
五、在keepalived:10.10.10.30上用ipvsadm 命令测试
[[email protected] ~]# yum install -y ipvsadm
[[email protected] ~]# 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.10.250:80 wrr persistent 50
-> 10.10.10.140:80 Route 1 0 0
-> 10.10.10.150:80 Route 1 0 0
在10.10.10.140执行
[[email protected] html]# echo "" > keep.html
[[email protected] ~]# 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.10.250:80 wrr persistent 50
-> 10.10.10.150:80 Route 1 0 0
再在10.10.10.140执行
[[email protected] html]# echo "ok" > keep.html
[[email protected] ~]# 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.10.250:80 wrr persistent 50
-> 10.10.10.140:80 Route 1 0 0
-> 10.10.10.150:80 Route 1 0 0