1、實(shí)現(xiàn)haproxy+keepalived高可用集群
1.1 部署規(guī)劃:
192.168.37.37:haproxy+keepalived 服務(wù)器1
192.168.37.47:haproxy+keepalived 服務(wù)器2
192.168.37.57:web 服務(wù)器1
192.168.37.67:web 服務(wù)器2
1.2 web服務(wù)器安裝配置
1.2.1? web1
安裝nginx:yum install nginx -y
修改頁面:echo "web1-192.168.37.57" > /usr/share/nginx/html/index.html
啟動(dòng):nginx
1.2.2? web2
安裝nginx:yum install nginx -y
修改頁面:echo "web2-192.168.37.67" > /usr/share/nginx/html/index.html
啟動(dòng):nginx
1.3 keepalived安裝配置
1.3.1 ka1(主服務(wù)器配置)
安裝:yum install keepalived -y
配置:vim /etc/keepalived/keepalived.conf
global_defs {
? notification_email {
? ? ? root@localhost
? }
? notification_email_from Alexandre.Cassen@firewall.loc
? smtp_server 172.0.0.1
? smtp_connect_timeout 30
? router_id ka1
? vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
? ? state MASTER
? ? interface eth0
? ? virtual_router_id 55
? ? priority 100
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 123456
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.37.100/32 dev eth0 label eth0:1
? ? }
}
啟動(dòng):systemctl start keepalived
1.3.2 ka2(從服務(wù)器配置)
keepalived配置文件:?
? ? state BACKUP
? ? interface eth0
? ? virtual_router_id 55
? ? priority 80
其他與主服務(wù)器相同
1.4 haproxy安裝配置
yum安裝:yum install haproxy -y
配置(兩臺(tái)服務(wù)器配置相同):
vim /etc/haproxy/haproxy.cfg
listen web_prot_80
? bind 192.168.37.100:80
? mode http
? balance static-rr
? server web1 192.168.37.57:80? weight 1 check port 80 inter 3s fall 2 rise 5
? server web2 192.168.37.67:80? weight 1 check port 80 inter 3s fall 2 rise 5
1.5? 測(cè)試haproxy負(fù)載均衡

1.6 測(cè)試keepalived
主服務(wù)器:ka1

從服務(wù)器:ka2

ka1宕機(jī)后

VIP飄到ka2

2、實(shí)現(xiàn)LVS+keepalived高可用集群
2.1 部署規(guī)劃:
192.168.0.104:客戶端client
192.168.37.3及192.168.0.113:路由
192.168.37.37:LVS1+keepalived 服務(wù)器1
192.168.37.47:LVS2+keepalived 服務(wù)器2
192.168.37.57:web 服務(wù)器1
192.168.37.67:web 服務(wù)器2

2.2 web服務(wù)器安裝配置
2.2.1 web1
安裝nginx:yum install nginx -y
修改頁面:echo "web1-192.168.37.57" > /usr/share/nginx/html/index.html
啟動(dòng):nginx
修改網(wǎng)關(guān):192.168.37.3 --重啟網(wǎng)絡(luò):systemctl restart network
2.2.2 web2
安裝nginx:yum install nginx -y
修改頁面:echo "web2-192.168.37.67" > /usr/share/nginx/html/index.html
啟動(dòng):nginx
修改網(wǎng)關(guān):192.168.37.3--重啟網(wǎng)絡(luò):systemctl restart network
2.2.3 綁定VIP
腳本:

執(zhí)行腳本:bash lvs_dr_rs.sh start

2.3 LVS1 + keepalived安裝配置
2.3.1 ka1(主服務(wù)器配置)
安裝:yum install keepalived ipvsadm? -y
配置keepalived: vim /etc/keepalived/keepalived.conf
global_defs {
? notification_email {
? ? ? root@localhost
? }
? notification_email_from Alexandre.Cassen@firewall.loc
? smtp_server 172.0.0.1
? smtp_connect_timeout 30
? router_id ka1
? vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
? ? state MASTER
? ? interface eth0
? ? virtual_router_id 55
? ? priority 100
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 123456
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.37.100/32 dev eth0 label eth0:1
? ? }
}
virtual_server 192.168.37.100 80 {
? ? delay_loop 6
? ? lb_algo rr
? ? lb_kind DR
? ? protocol TCP
? ? sorry_server 127.0.0.1 80
? ? real_server 192.168.37.57 80 {
? ? ? ? weight 1
? ? ? ? HTTP_GET {
? ? ? ? ? ? url {
? ? ? ? ? ? ? path /
? ? ? ? ? ? ? status_code 200
? ? ? ? ? ? }
? ? ? ? ? ? connect_timeout 3
? ? ? ? ? ? nb_get_retry 3
? ? ? ? ? ? delay_before_retry 3
? ? ? ? }
? ? }
? ? real_server 192.168.37.67 80 {
? ? ? ? weight 1
? ? ? ? HTTP_GET {
? ? ? ? ? ? url {
? ? ? ? ? ? ? path /
? ? ? ? ? ? ? status_code 200
? ? ? ? ? ? }
? ? ? ? ? ? connect_timeout 3
? ? ? ? ? ? nb_get_retry 3
? ? ? ? ? ? delay_before_retry 3
? ? ? ? }
? ? }
}
啟動(dòng):systemctl start keepalived
查看虛擬服務(wù)列表:

安裝nginx:yum install nginx -y
配置sorry server:echo sorry server1> /usr/share/nginx/html/index.html
2.3.2 ka2(從服務(wù)器配置)
keepalived配置文件:?
? ? state BACKUP
? ? interface eth0
? ? virtual_router_id 55
? ? priority 80
安裝nginx:yum install nginx -y
配置sorry server:echo sorry server2> /usr/share/nginx/html/index.html
其他與主服務(wù)器相同
2.4 其他配置
① 192.168.0.104:客戶端client
網(wǎng)關(guān):GATEWAY=192.168.0.113
② 192.168.37.3及192.168.0.113:路由
修改內(nèi)核參數(shù):vim /etc/sysctl.conf
net.ipv4.ip_forward=1 #啟動(dòng)路由轉(zhuǎn)發(fā)
生效:sysctl -p
2.5 測(cè)試

ka1宕機(jī)后

web1宕機(jī)后

web2宕機(jī)后
