應用環境:
lvs負責多台web端的負載均衡(lb);keepalived負責lvs的高可用(ha),這裡介紹主備模型。
測試環境:
hostname ip system
dr1 192.168.10.234 centos7.5
dr2 192.168.10.235 centos7.5
rs1 192.168.10.236 centos7.5
rs2 192.168.10.237 centos7.5
vip 192.168.10.239
dr配置步驟:
安裝軟體(在dr1和dr2上分別安裝)
yum install ipvsadm keepalived -y
2.修改配置檔案/etc/keepalived/keepalived.conf
! configuration file for keepalived
global_defs
vrrp_instance vi_1
virtual_ipaddress
}virtual_server 192.168.10.222 80
}real_server 192.168.10.237 80
}}
在dr2上只要修改
state master >> state backup
priority 150 >> priority 140
3.通過執行指令碼dr.sh配置,需加執行許可權(dr1和dr2都執行)
#!/bin/bash
# director伺服器上開啟路由**功能
echo 1 > /proc/sys/net/ipv4/ip_forward
# 測試發現因為排程器跟real_server在同一網段,如果需要**給外網或別的網段就開啟
ipv=/sbin/ipvsadm
vip=192.168.10.239
rs1=192.168.10.236
rs2=192.168.10.237
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
# 注意子網掩碼是255.255.255.255,代表vip所在網段沒有別的主機了
route add -host $vip dev eth0:0
# 增加一條路由
$ipv -c
$ipv -a -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
# -g代表dr模式
4.啟動keepalived服務,並通過ip a命令檢視vip是否配置生效(會出現乙個192.168.10.239的ip)
[root@centos7 ~]# systemctl start keepalived
rs配置步驟:
1.安裝web服務(rs1和rs2上分別安裝)
[root@centos7 ~]# yum install nginx -y
修改rs1首頁/usr/share/nginx/html/index.html(nginx預設首頁)
this is rs1,the ip is 192.168.10.236
修改rs2首頁/usr/share/nginx/html/index.html(nginx預設首頁)
this is rs2,the ip is 192.168.10.237
2.為方便操作,這裡新建乙個指令碼rs.sh記錄(rs1和rs2上同步操作)
#!/bin/bash
vip=192.168.10.239
case "$1" in
start)
echo "start lvs of realserver dr"
/sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $vip dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;stop)
/sbin/ifconfig lo:0 down
echo "close lvs of realserver dr"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;*) echo "usage: $0 "
exit 1
esac
exit 0
給指令碼新增執行許可權,並啟動指令碼示例
[root@centos7 ~]# chmod +x dr.sh
[root@centos7 ~]# ./dr.sh start // start:啟動; stop:停止;
start lvs of realserver dr
3.啟動web服務
[root@centos7 ~]# systemctl start nginx
測試
測試①: 預設現在訪問vip:192.168.10.239,走的是dr1(master)
[root@dib101 ~]# while true ; do curl 192.168.10.239; sleep 1;done
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
this is rs2,the ip is 192.168.10.237
this is rs1,the ip is 192.168.10.236
測試②:
斷開master:vip飄到backup上,訪問vip正常,client 輪詢依舊;
復活master:vip飄回master上,訪問vip正常,client 輪詢依舊; // 成功實現:lvs的高可用 和 nginx的負載均衡
測試③:
手動斷開nginx,然後再手動啟動nginx:
注意事項以及改進事項1.關閉防火牆,selinux等功能
安裝lVS keepalived心得
接著昨天蛋疼地無網裝keepalived,今天又在裝lvs。首先按照網上的教程做 這裡就說自己遇到的問題,第乙個是找不到 usr src kernels下的核心檔案。要先rpm kernel devel才會有。其次,用ln s 軟鏈結一直鏈不上去,可以在安裝keepalived的 configure...
LVS Keepalived負載均衡配置
lvs一般是和keepalived一起組合使用的,雖然也可以單獨使用lvs,但配置比較繁瑣,且可用性也沒有前者高。lvs和keepalived組合使用後,配置lvs的vip和負載均衡就都在keepalived中進行配置,只是在keepalived中呼叫lvs 配置 keepalived.conf g...
LVS keepalived負載均衡實戰
lvs keepalived負載均衡實戰 一 為什麼要使 用負載均衡技術?1 系統高可用性 2 系統可擴充套件性 3 負載均衡能力 lvs keepalived能很好的實現以上的要求,lvs提 供負載均衡,keepalived提供健康檢查,故障轉移,提高系統的可用性!採用這樣的架構以後 很容易對現有...