KeepAlived +haproxy高可用集群教程
KeepAlived高可用集群
????????????? keepalived用于實(shí)現(xiàn)高可用集群
????????????? 它的工作原理就是VRRP(虛擬冗余路由協(xié)議)
配置高可用的web集群
?
????????????? 環(huán)境說明:
–??????????? web1:eth0->192.168.4.100/24
–??????????? web2:eth0->192.168.4.200/24
????????????? 配置keepalived
# 在兩臺web服務(wù)器上安裝keepalived
[root@web1 ~]# yum install -y keepalived httpd
[root@web2 ~]# yum install -y keepalived httpd
# 修改配置文件
[root@web1 ~]# vim /etc/keepalived/keepalived.conf
?12??? router_id web1??? # 設(shè)置本機(jī)在集群中的唯一識別符
?13??? vrrp_iptables???? # 自動配置iptables放行規(guī)則
?... ...
?20 vrrp_instance VI_1 {
?21???? state MASTER?????????? # 狀態(tài),主為MASTER,備為BACKUP
?22???? interface eth0???????? # 網(wǎng)卡
?23???? virtual_router_id 51?? # 虛擬路由器地址
?24???? priority 100?????????? # 優(yōu)先級
?25???? advert_int 1?????????? # 發(fā)送心跳消息的間隔
?26???? authentication {
?27???????? auth_type PASS???? # 認(rèn)證類型為共享密碼
?28???????? auth_pass 1111???? # 集群中的機(jī)器密碼相同,才能成為集群
?29???? }??
?30???? virtual_ipaddress {
?31???????? 192.168.4.80/24??? # VIP地址
?32???? }??
?33 }
# 刪除下面所有行
# 打開一個新的終端監(jiān)控日志,新日志將出持續(xù)顯示在屏幕上。退出按ctrl+c
[root@web1 ~]# tail -f /var/log/messages | grep -i keepalived
# 在前一個終端啟動服務(wù)
[root@web1 ~]# systemctl start keepalived
# 等幾秒服務(wù)完全啟動后,可以查看到vip
[root@web1 ~]# ip a s eth0?? # eth0將會增加額外的4.80地址
# 配置web2
[root@web1 ~]# scp /etc/keepalived/keepalived.conf 192.168.4.200:/etc/keepalived/
[root@web2 ~]# vim /etc/keepalived/keepalived.conf
?12??? router_id web2????????? # 改id
?13??? vrrp_iptables
?... ...
?20 vrrp_instance VI_1 {
?21???? state BACKUP?????????? # 改狀態(tài)
?22???? interface eth0
?23???? virtual_router_id 51
?24???? priority 80??????????? # 改優(yōu)先級
?25???? advert_int 1
?26???? authentication {
?27???????? auth_type PASS
?28???????? auth_pass 1111
?29???? }
?30???? virtual_ipaddress {
?31???????? 192.168.4.80/24
?32???? }
?33 }
# 啟動服務(wù)
[root@web2 ~]# systemctl start keepalived
# 查看地址,eth0不會出現(xiàn)vip
[root@web2 ~]# ip a s eth0
# 測試,現(xiàn)在訪問4.80,看到是web1上的內(nèi)容
[root@client1 ~]# curl http://192.168.4.80/
192.168.2.100
# 在web2上監(jiān)控日志
[root@web2 ~]# tail -f /var/log/messages | grep -i keepalived
# 關(guān)閉web1上的keepalived,觀察web2的日志輸出
[root@web1 ~]# systemctl stop keepalived.service
# 測試,現(xiàn)在訪問4.80,看到是web2上的內(nèi)容
[root@client1 ~]# curl http://192.168.4.80/
apache web server2
# 在web2上查看vip,可以查看到vip 192.168.4.80
[root@web2 ~]# ip a s eth0
配置高可用、負(fù)載均衡的web集群
?
????????????? 環(huán)境說明:LVS-DR模式
–??????????? client1:eth0->192.168.4.10
–??????????? lvs1:eth0->192.168.4.5
–??????????? lvs2:eth0->192.168.4.6
–??????????? web1:eth0->192.168.4.100
–??????????? web2:eth0->192.168.4.200
????????????? 環(huán)境準(zhǔn)備
# 關(guān)閉2臺web服務(wù)器上的keepalived
[root@web1 ~]# systemctl stop keepalived.service
[root@web2 ~]# systemctl stop keepalived.service
[root@web1 ~]# yum remove -y keepalived
[root@web2 ~]# yum remove -y keepalived
# 創(chuàng)建新虛擬機(jī)lvs2
[root@zzgrhel8 ~]# clone-vm7
Enter VM number: 5??? # 虛擬機(jī)編號,
[root@zzgrhel8 ~]# virsh start tedu_node05???? # 啟動虛機(jī)
[root@zzgrhel8 ~]# virsh console tedu_node05?? # 連接虛機(jī)控制臺
localhost login: root
Password: 123456
# 將以下內(nèi)容粘貼到虛擬機(jī),進(jìn)行初始化
hostnamectl set-hostname lvs2
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.6/24
nmcli connection down eth0
nmcli connection up eth0
echo a | passwd --stdin root
# 退出
[root@localhost ~]# exit
# 按ctrl + ]退回到真機(jī)
# 連接測試
[root@zzgrhel8 ~]# ssh 192.168.4.6
配置高可用、負(fù)載均衡
1.????????? 在2臺web服務(wù)器的lo上配置vip
2.????????? 在2臺web服務(wù)器上配置內(nèi)核參數(shù)
3.????????? 刪除lvs1上的eth0:0。因?yàn)関ip將由keepalived接管
[root@lvs1 ~]# ifdown eth0:0
[root@lvs1 ~]# rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:0
1.????????? 刪除lvs1上的lvs規(guī)則。因?yàn)閘vs規(guī)則將由keepalived創(chuàng)建
[root@lvs1 ~]# ipvsadm -Ln?? # 查看規(guī)則
[root@lvs1 ~]# ipvsadm -D -t 192.168.4.15:80
1.????????? 在lvs上配置keepalived
[root@lvs1 ~]# yum install -y ipvsadm keepalived
[root@lvs2 ~]# yum install -y ipvsadm keepalived
[root@lvs1 ~]# vim /etc/keepalived/keepalived.conf
?12??? router_id lvs1?????? # 為本機(jī)取一個唯一的id
?13??? vrrp_iptables??????? # 自動開啟iptables放行規(guī)則
... ...
?20 vrrp_instance VI_1 {
?21???? state MASTER
?22???? interface eth0
?23???? virtual_router_id 51
?24???? priority 100
?25???? advert_int 1
?26???? authentication {
?27???????? auth_type PASS
?28???????? auth_pass 1111
?29???? }??
?30???? virtual_ipaddress {
?31???????? 192.168.4.15?????? # vip地址,與web服務(wù)器的vip一致
?32???? }??
?33 }
?# 以下為keepalived配置lvs的規(guī)則
?35 virtual_server 192.168.4.15 80 {?? # 聲明虛擬服務(wù)器地址
?36???? delay_loop 6???? # 健康檢查延遲6秒開始
?37???? lb_algo wrr????? # 調(diào)度算法為wrr
?38???? lb_kind DR?????? # 工作模式為DR
?39???? persistence_timeout 50? # 50秒內(nèi)相同客戶端調(diào)度到相同服務(wù)器
?40???? protocol TCP???? # 協(xié)議是TCP
?41
?42???? real_server 192.168.4.100 80 {?? # 聲明真實(shí)服務(wù)器
?43???????? weight 1????????? # 權(quán)重
?44???????? TCP_CHECK {?????? # 通過TCP協(xié)議對真實(shí)服務(wù)器做健康檢查
?45???????????? connect_timeout 3 # 連接超時時間為3秒
?46???????????? nb_get_retry 3??? # 3次訪問失敗則認(rèn)為真實(shí)服務(wù)器故障
?47???????????? delay_before_retry 3? # 兩次檢查時間的間隔3秒
?48???????? }
?49???? }
?50???? real_server 192.168.4.200 80 {
?51???????? weight 2
?52???????? TCP_CHECK {
?53???????????? connect_timeout 3
?54???????????? nb_get_retry 3
?55???????????? delay_before_retry 3
?56???????? }
?57???? }
?58 }
# 以下部分刪除
# 啟動keepalived服務(wù)
[root@lvs1 ~]# systemctl start keepalived
# 驗(yàn)證
[root@lvs1 ~]# ip a s eth0??? # 可以查看到vip
[root@lvs1 ~]# ipvsadm -Ln??? # 出現(xiàn)規(guī)則
# 客戶端連接測試
[root@client1 ~]# for i in {1..6}; do curl http://192.168.4.15/; done
apache web server2
apache web server2
apache web server2
apache web server2
apache web server2
apache web server2
# 為了效率相同的客戶端在50秒內(nèi)分發(fā)給同一臺服務(wù)器。為了使用同一個客戶端可以看到輪詢效果,可以注釋配置文件中相應(yīng)的行后,重啟keepavlied。
# 配置LVS2
[root@lvs1 ~]# scp /etc/keepalived/keepalived.conf 192.168.4.6:/etc/keepalived/
[root@lvs2 ~]# vim /etc/keepalived/keepalived.conf
?12??? router_id lvs2
?21???? state BACKUP
?24???? priority 80
[root@lvs2 ~]# systemctl start keepalived
[root@lvs2 ~]# ipvsadm -Ln?? # 出現(xiàn)規(guī)則
1.????????? 驗(yàn)證
# 1. 驗(yàn)證真實(shí)服務(wù)器健康檢查
[root@web1 ~]# systemctl stop httpd
[root@lvs1 ~]# ipvsadm -Ln?? # web1在規(guī)則中消失
[root@lvs2 ~]# ipvsadm -Ln
[root@web1 ~]# systemctl start httpd
[root@lvs1 ~]# ipvsadm -Ln?? # web1重新出現(xiàn)在規(guī)則中
[root@lvs2 ~]# ipvsadm -Ln
# 2. 驗(yàn)證lvs的高可用性
[root@lvs1 ~]# shutdown -h now??? # 關(guān)機(jī)
[root@lvs2 ~]# ip a s eth0?? ??# 可以查看到vip
# 客戶端訪問vip依然可用
[root@client1 ~]# for i in {1..6}; do curl http://192.168.4.15/; done
192.168.2.100
apache web server2
apache web server2
192.168.2.100
apache web server2
apache web server2
HAProxy
????????????? 也是一款實(shí)現(xiàn)負(fù)載均衡的調(diào)度器
????????????? 適用于負(fù)載特別大的web站點(diǎn)
????????????? HAProxy的工作模式:
–??????????? mode http:只適用于web服務(wù)
–??????????? mode tcp:適用于各種服務(wù)
–??????????? mode health:僅做健康檢查,很少使用
配置haproxy
?
????????????? 環(huán)境準(zhǔn)備:
–??????????? client1:eth0 -> 192.168.4.10
–??????????? HAProxy:eth0 -> 192.168.4.5
–??????????? web1:eth0 -> 192.168.4.100
–??????????? web2:eth0 -> 192.168.4.200
????????????? 初始化配置
# 關(guān)閉192.168.4.6
[root@lvs2 ~]# shutdown -h now
# 清理192.168.4.5
[root@lvs1 ~]# yum remove -y ipvsadm keepalived
[root@lvs1 ~]# hostnamectl set-hostname haproxy1
# web服務(wù)器,不需要配置vip,不需要改內(nèi)核參數(shù)。但是存在對haproxy也沒有影響。
????????????? 配置haproxy
# 裝包
[root@haproxy1 ~]# yum install -y haproxy
[root@haproxy1 ~]# vim /etc/haproxy/haproxy.cfg
# 配置文件中,global是全局配置;default是缺省配置,如果后續(xù)有和default相同的配置,default配置將會被覆蓋。
# 配置文件中,frontend描述haproxy怎么和用戶交互;backend描述haproxy怎么和后臺應(yīng)用服務(wù)器交互。這兩個選項(xiàng),一般不單獨(dú)使用,而是合并到一起,名為listen。
# 將61行之后全部刪除,寫入以下內(nèi)容
?61 listen myweb 0.0.0.0:80?? # 定義本機(jī)監(jiān)聽地址
?62???? balance roundrobin??? # 調(diào)度算法為輪詢
?# 對web服務(wù)器做健康檢查,2秒檢查一次,如果連續(xù)2次檢查成功,認(rèn)為服務(wù)器是健康的,如果連續(xù)5次檢查失敗,認(rèn)為服務(wù)器壞了
?63???? server web1 192.168.4.100 check inter 2000 rise 2 fall 5
?64???? server web2 192.168.4.200 check inter 2000 rise 2 fall 5
?65????
?66 listen stats 0.0.0.0:1080? # 定義監(jiān)控地址
?67???? stats refresh 30s????? # 設(shè)置監(jiān)控頁面自動刷新時間為30秒
?68???? stats uri /stats?????? # 定義監(jiān)控地址是/stats
?69???? stats auth admin:admin? # 監(jiān)控頁面的用戶名和密碼都是admin
# 啟服務(wù)
[root@haproxy1 ~]# systemctl start haproxy.service
# 使用firefox訪問監(jiān)控地址 http://192.168.4.5:1080/stats
# 客戶端訪問測試
[root@client1 ~]# for i in {1..6}; do curl http://192.168.4.5/; done
192.168.2.100
apache web server2
192.168.2.100
apache web server2
192.168.2.100
apache web server2
負(fù)載均衡調(diào)度器比較:
????????????? nginx:可以工作在第4層和第7層??梢愿鶕?jù)url進(jìn)行負(fù)載均衡。正則表達(dá)式支持的更廣泛。
????????????? lvs:效率最高。工作在第4層。
????????????? haproxy:可以工作在第4層和第7層??梢愿鶕?jù)url進(jìn)行負(fù)載均衡。支持有限的正則表達(dá)式。
?