Keepalived+LVS負載均衡雙機備熱
阿新 • • 發佈:2018-12-08
實驗環境
兩臺Keepalived伺服器,三臺提供http服務的節點伺服器
主排程器(192.168.3.4)
從排程器(192.168.3.5)
HTTP-1(192.168.3.1)
HTTP-2(192.168.3.2)
HTTP-3(192.168.3.3)
- 主排程器:
[[email protected] ~]# yum -y install keepalived ipvsadm
###如果交換機外網有獨立的IP可以不用指定虛擬IP###
[[email protected] ~]# cd /etc/sysconfig/ network-scripts/
[[email protected] ~]# cp ifcfg-ens32 ifcfg-ens32:0
[[email protected] ~]# vim ifcfg-ens32:0 #配置虛擬IP地址
NAME=ens32:0
DEVICE=ens32:0
ONBOOT=yes
IPADDR=192.168.3.10
NETMASK=255.255.255.0
:wq!
[[email protected] ~]# ifup ens32:0 #開啟虛擬IP
[[email protected] ~]# ifconfig ens32:0 #檢視
[[email protected] ~]# vim /etc/sysctl.conf #調整/proc響應引數
...
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0
:wq!
[[email protected] ~]# cd /etc/keepalived
[[email protected] ~]# cp keepalived.conf keepalived.conf.bak
[[email protected] ~]# systemctl enable keepalived.service
[[email protected] ~]# vim /etc/keepalived/keeplived.conf
global_defs {
router_id LVS_DR_1 #路由器名稱
}
vrrp_instance VI_1 {
state MASTER
interface ens32 #網絡卡
virtual_router_id 1
priority 100 #優先順序
advert_int 1
authentication {
auth_type PASS
auth_pass 123456 #匹配密碼
}
virtual_ipaddress {
192.168.3.10 #VIP地址
}
}
virtual_server 192.168.3.10 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.3.1 80 { #真實伺服器
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.3.2 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.3.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
:wq!
[[email protected]MASTER ~]# systemctl restart keepalived.service
[[email protected]MASTER ~]# ip a
- 從排程器:
[[email protected] ~]# yum -y install keeplived ipvsadm
[[email protected] ~]# systemctl enable keepalived.service
[[email protected] ~]# cd /etc/keepalived
[[email protected] ~]# cp keepalived.conf keepalived.conf.bak
[[email protected] ~]# vim /etc/keepalived/keeplived.conf
global_defs {
router_id LVS_DR_2 #路由器名稱
}
vrrp_instance VI_1 {
state BACKUP
interface ens32 #網絡卡
virtual_router_id 1
priority 99 #優先順序
advert_int 1
authentication {
auth_type PASS
auth_pass 123456 #匹配密碼
}
virtual_ipaddress {
192.168.3.10 #漂移地址
}
}
virtual_server 192.168.3.10 80 { #真實伺服器
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.3.1 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.3.2 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.3.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
:wq!
[[email protected] ~]# systemctl restart keepalived
- 節點伺服器 :
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] ~]# cp ifcfg-lo ifcfg-lo:0
[[email protected] ~]# vim ifcfg-lo:0 #配置虛擬IP,要和LVS虛擬IP一致,子網掩碼全為1
DEVICE=lo:0
IPADDR=192.168.3.5
NETMASK=255.255.255.255
:wq!
[[email protected] ~]# ifup lo:0
[[email protected] ~]# ifconfig lo:0
[[email protected] ~]# vim /etc/rc.local #新增VIP本地訪問路由
...
/sbin/route add -host 192.168.3.10 dev lo:0
:wq!
[[email protected] ~]# route add -host 192.168.3.10 dev lo:0
[[email protected] ~]# vim /etc/sysctl.conf #調整/proc響應引數
...
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
:wq!
[[email protected] ~]# sysctl -p
- 測試:
客戶端訪問“http://192.168.3.10”
關閉主排程器,客戶端任然能夠正常訪問節點伺服器
關閉一臺節點伺服器,客戶端任然能正常訪問其他節點伺服器