Keepalived 的設計目標是搭建高可用的 LVS 負載均衡羣集,能夠調用 ipvsadm 工具來建立虛擬服務器、管理服務器池,而不單單用做雙機熱備。使用 Keepalived 搭建 LVS 羣集更加簡便易用,主要優點體如今:對 LVS 負載調度器實現熱備切換,提升可用性;對服務器池中的節點進行健康檢查,自動移除失效節點,恢復後再從新加入。html
本次咱們以 DR 模式的 LVS 羣集爲基礎,增長一臺從負載調度器,使用 Keepalived 來實現主、從調度器的熱備,從而構建兼有負載均衡、高可用兩種能力的 LVS 網站羣集平臺。web
後續上傳算法
DR1主調度服務器:192.160.100.200 DR2備調度服務器:192.168.100.201 web1節點服務器:192.168.100.202 web2節點服務器:192.168.100.203 虛擬 VIP 服務器:192.168.100.100 nfs服務器:192.168.100.233 Win10客戶端:192.168.100.10
檢查是否安裝nfs以及rpcbind軟件包shell
rpm -q nfs-utils rpcbind
若是沒有安裝則用yum倉庫進行安裝vim
yum -y install nfs-utils rpcbind
開啓服務瀏覽器
systemctl start rpcbind systemctl start nfs
建立掛載點,給文件目錄設置權限(實際應用中,咱們還需建立raid卷並掛載到目錄中以便使用)bash
mkdir -p /opt/web1 /opt/web2 chmod 777 /opt/web1 /opt/web2
編輯配置文件vim /etc/exports
服務器
/opt/web1 192.168.100.0/24(rw,sync) ##在此網段下的用戶能夠讀寫、同步目錄中的文件
/opt/web2 192.168.100.0/24(rw,sync)網絡
發佈共享負載均衡
exportfs -rv
此時咱們能夠對客戶端進行查看
showmount -e 192.168.100.233
setenforce 0 systemctl disable firewalld systemctl stop firewalld
安裝Apache
yum -y install httpd
掛載 nfs 到Apache主頁存放
mount.nfs 192.168.100.206:/opt/web1 /var/www/html
建立默認首頁文件
echo "<h1>This is web1 page.</h1>" > /var/www/html/index.html
建立虛擬網卡
cd /etc/sysconfig/network-scripts/ cp ifcfg-lo ifcfg-lo:0 vim ifcfg-lo:0 ##設置參數以下
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
cd /etc/init.d/ vim web.sh ##添加參數以下
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
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 "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
chmod +x web.sh ##給予執行權限 service web.sh start ifup lo:0 ##啓動網卡
setenforce 0 systemctl disable firewalld systemctl stop firewalld systemctl enable httpd systemctl start httpd
安裝Apache
yum -y install httpd
掛載 nfs 到Apache主頁存放
mount.nfs 192.168.100.233:/opt/web1 /var/www/html
建立默認首頁文件
echo "<h1>This is web2 page.</h1>" > /var/www/html/index.html
建立虛擬網卡
cd /etc/sysconfig/network-scripts/ cp ifcfg-lo ifcfg-lo:0 vim ifcfg-lo:0 ##設置參數以下
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ONBOOT=yes
cd /etc/init.d/ vim web.sh ##添加參數以下
#!/bin/bash
VIP=192.168.100.100
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
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 "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
chmod +x web.sh ##給予執行權限 service web.sh start ifup lo:0 ##啓動網卡
setenforce 0 systemctl disable firewalld systemctl stop firewalld systemctl enable httpd systemctl start httpd
安裝 ipvsadm 、keepalived 軟件包
yum -y install ipvsadm keepalived
調整 proc 響應參數
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
#proc響應關閉重定向功能
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
配置 VIP 承載接口
cd /etc/sysconfig/network-scripts/ cp ifcfg-ens33 ifcfg-ens33:0 vim ifcfg-ens33:0 ##參數以下
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.0
ifup ens33:0 ##啓用虛擬IP
cd /etc/init.d/ vim dr.sh
#!/bin/bash
GW=192.168.100.1
VIP=192.168.100.100
RIP1=192.168.100.202
RIP2=192.168.100.203
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
chmod +x dr.sh service dr.sh start
cd /etc/keepalived/ vim keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 #指向本地
router_id LVS_01 #指定名稱,備份服務器不一樣名稱
...
}vrrp_instance VI_1 {
state MASTER #備份服務器是BACKUP
interface ens33 #承接的端口
virtual_router_id 10 #主從調度器組號相同
...
auth_pass abc123 #驗證密碼
priority 100 #優先級,備份要小於主調度器
...
virtual_ipaddress {
192.168.100.100
}
...
virtual_server 192.168.100.100 80 {
delay_loop 6
lb_algo rr ##輪詢算法
lb_kind DR ##DR模式
persistence_timeout 50
protocol TCPreal_server 192.168.100.202 80 { weight 1 ##權重爲1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.100.203 80 { weight 1 TCP_CHECK { connect_port 80 #添加端口 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}
systemctl start keepalived
在經過客戶機的瀏覽器中,可以經過羣集的 VIP 地址(192.168.100.100)來正常訪問 Web1 & Web2 頁面。當主、從調度器有任一失效時,咱們仍能夠正常訪問 Web ,且只要節點服務器存在兩臺及以上,咱們仍能夠實現負載均衡。