Keepalived的相关应用,使用keepalived实现nginx和lvs的高可用负载均衡器
使用keepalived實現HA Cluster集群,下面以示例來演示keepalived的使用和keepalived的相關知識
HA Cluster配置前提:
? ? 1、本機的主機名,要與hostname(uname-n)獲得的名稱保持一致;
?????CentOS6: /etc/sysconfig/network
? ? ? ?CentOS7: hostnamectl set-hostname HOSTNAME
?????各節點要能互相解析主機名;一般建議通過hosts文件進行解析;
?????修改主機名為centos7和centos6.1.com,然后修改hosts文件,能讓兩邊能相互解析到
? ?2、各節點時間同步;ntpdate? 172.16.249.195把這個主機當成時間服務器,然后進行時間同步
????設置ntp時間同步服務器
????修改/etc/ntp.conf文件
????內容如下:
????????driftfile /var/lib/ntp/drift
????????restrict 127.0.0.1
????????restrict ::1
????????restrict 172.16.0.0 mask 255.255.0.0nomodify notrap
????????server 127.127.1.0
????????fudge 127.127.1.0 stratum 8
????????includefile /etc/ntp/crypto/pw
????????keys /etc/ntp/keys
????????disable monitor
?
????3、確保iptables及selinux不會成為服務阻礙;
? ? ? ? nopreempt:非搶占模式;默認為搶占模式;
?
?在調度器上,首先安裝keepalived程序,然后把時間同步成一樣的
ntpdate?172.16.249.195
檢查iptables和selinux是否開啟了
然后在CentOS7中進行實驗
yum?install? keepalived?
?
用兩臺機器一臺CentOS7,一臺CentOS6看是否有問題
CentOS7中
修改位置文件(/etc/keepalived/keepalived.conf)
首先把“virtual_server 192.168.200.100 443 {”這一行以下的全部注釋掉
然后修改內容如下
為了完整,把這后面的信息補全
然后復制這個文件到另一個主機
scp keepalived.conf 172.16.249.147:/etc/keepalived/
然后稍微修改下
這樣就可以了,這樣就可以啟動測試了
systemctl start keepalived.service;ssh172.16.249.147 'service ?keepalivedstart'? 因為我使用的是一個centos7和一個centos6,所以前后的啟動命令不一樣,如果使用的是同樣的就不需這樣了
使用? ps?? aux? 查看keepalived是否啟動
然后在centos7上使用ifconfig進行查看,可以看到地址加上來了
然后我們把centos7下的keepalived關閉,然后查看地址
地址沒有了,去centos6上查看會發現地址在6上看,這就是高可用了,就是當一臺負載均衡器下線時,另一臺能立即啟用,實現高可用
?
沒有日志信息,我們自己定義一個
首先修改/etc/sysconfig/keepalived這個文件,
然后修改/etc/rsyslog.conf這個文件
然后使用systemctl restart rsyslog.service重啟日志服務
另一個主機同樣修改重啟日志服務使用(service rsyslog restart)
然后重啟keepalived服務查看日志就有了
?
然后做兩個虛擬路由,即雙主模式
修改keepalived的配置文件,增加下面一段
然后另一臺主機修改keepalived的配置文件為下面一段
然后重啟服務就可以查看了(如果因為網絡環境比較復雜的話,等的時間可能要長一點)
?
如果停掉一臺的keepalived服務,另一臺會包括所有的地址
?
這樣停服務在生產中,是不行的,下面來添加腳本實現動態切換
修改配置文件,添加下面配置
這一段要加在vrrp_instance 字段外面
?
?
另一個主機也做這樣的配置
這就可以啟用了
然后在172.16.249.195的/etc/keepalived目錄下創建一個down文件,然后查看地址
另一臺主機上就有兩個地址了
定義郵件的發送,需要自行定義,需要自己寫一個腳本
腳本內容:
#!/bin/bash
????#Author: MageEdu <linuxedu@foxmail.com>
????#description: An example of notify script
????#
????vip=172.16.100.88
????contact='root@localhost'
????notify(){
????????mailsubject="`hostname` to be $1: $vipfloating"
????????mailbody="`date '+%F %H:%M:%S'`: vrrptransition, `hostname` changed to be $1"
????????echo $mailbody | mail -s"$mailsubject" $contact
????}
????case"$1" in
? ? ? ?master)
????????notify master
????????exit 0
????????;;
???? ?backup)
????????notify backup
????????exit 0
????????;;
???? ?fault)
????????notify fault
????????exit 0
????????;;
???? ?*)
????????echo 'Usage: `basename $0`{master|backup|fault}'
????????exit 1
????????;;
????esac
在/etc/keepalived/目錄下創建一個名為notify.sh的腳本
給這個腳本一個執行權限chmod +x notify.sh
?
配置文件中vrrp_instance VI_1這一段里寫入下面三行
notify_master"/etc/keepalived/notify.sh master"
notify_backup"/etc/keepalived/notify.sh backup"
notify_fault"/etc/keepalived/notify.shfault"
?
驗證下,
然后把這個腳本復制到另一個負載均衡器上,然后修改配置文件
scp -rp notify.sh172.16.249.147:/etc/keepalived/
這樣郵件的發送就弄好了
另一個虛擬路由就不再演示了,跟這個步驟一樣
?
我們加入lvs和nginx來實現高可用的負載均衡器的實現(做的是雙主模型)
下面先以lvs做負載均衡器,進行高可用負載均衡,以lvs的DR模式為例,規劃如下
首先兩個RS打開web服務,配好測試頁,訪問測試以下,沒問題了,
?
?
然后配置第一個Director(負載均衡器),安裝ipvsadm,配置lvs規則(如果不會配置可以上網搜索,或者查看本人的lvs的博客),首先測試是能正常工作的
首先加入VIP,
用其他主機ping測試一下,這個地址通不通,通了就沒問題了,就差lvs規則了,
?
然后配置兩個RS,首先配置arp_ignore和arp_annoce,然后配置VIP和路由
寫一個腳本實現一些功能,然后直接在兩臺RS上運行一下,就可以了
然后 bash? set.sh? start? 然后cat看一下是否成功了;把這個腳本復制給另一臺RS執行一次
然后添加地址和路由
ifconfig lo:0 172.16.249.18 netmask255.255.255.255 broadcast 172.16.249.18
route add -host 172.16.249.18 dev lo:0
ifconfig lo:1 172.16.249.19 netmask255.255.255.255 broadcast 172.16.249.19
route add -host 172.16.249.19 dev lo:1
?
另一臺RS也添加地址和路由
ifconfig lo:0 172.16.249.18 netmask255.255.255.255 broadcast 172.16.249.18
route add -host 172.16.249.18 dev lo:0
ifconfig lo:1 172.16.249.19 netmask255.255.255.255 broadcast 172.16.249.19
route add -host 172.16.249.19 dev lo:1
因為做的是兩個虛擬路由器的即雙主的,就是通過DNS在前面分流,一部分走172.16.249.18這個地址一部分走172.16.249.19,所以RS要配置兩個VIP
? ? ? ? ? ? ?
然后RS的配置就做完了,然后回到Director上寫入ipvs規則
ipvsadm -A -t 172.16.249.18:80 -s rr
ipvsadm -a -t 172.16.249.18:80 -r172.16.249.116 -g
ipvsadm -a -t 172.16.249.18:80 -r172.16.249.123 -g
然后測試是否能正常輪調
如果用物理機的瀏覽器訪問不輪調,是網絡的影響,開一個虛擬機進行測試,如果沒有問題,就證明lvs的dr模式的負載均衡做好了
另一臺lvs服務器也做一下這個規則,然后測試一下,證明lvs的負載均衡沒有問題了
然后我們把lvs服務器上的規則清掉,地址刪除
然后在兩臺lvs上做sorry? server 啟動httpd服務,然后寫一個頁面,然后訪問
測試沒問題,就可以了
?
下面做keepalived高可用的負載均衡器(安裝keepalived)
直接做成兩臺虛擬路由器的,
修改第一臺lvs主機的keepalived的配置文件如下
global_defs {
??notification_email {
???????? root@localhost
?? }
??notification_email_from kaadmin@localhost
??smtp_server 127.0.0.1
??smtp_connect_timeout 30
??router_id centos7
}
vrrp_script chk_schedown {
???script "[[ -f /etc/keepalived/down?]] && exit 1 || exit 0"
???interval 1
???weight -2
??? }
?
vrrp_instance VI_1 {
???state MASTER
???interface eth0
???virtual_router_id 51
???priority 100
???advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 5b03429a7cf5
??? }
???virtual_ipaddress {
???????? 172.16.249.18/16dev eth0 label eth0:0
??? }
???track_script {
???????chk_schedown
??? }
???notify_master "/etc/keepalived/notify.sh master"
???notify_backup "/etc/keepalived/notify.sh backup"
???notify_fault "/etc/keepalived/notify.sh fault
}
?
vrrp_instance VI_2 {
???state BACKUP
???interface eth0
???virtual_router_id 61
???priority 99
???advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 20b662c6c06d
??? }
???virtual_ipaddress {
???????? 172.16.249.19/16dev eth0 label eth0:1
??? }
}
?
virtual_server 172.16.249.18 80 {
???delay_loop 6
???lb_algo wrr
???lb_kind DR
???nat_mask 255.255.0.0
???protocol TCP
?
???real_server 172.16.249.116 80 {
???????weight 1
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
???real_server 172.16.249.123 80 {
???????weight 2
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
}
?
virtual_server 172.16.249.19 80 {
???delay_loop 6
???lb_algo wrr
???lb_kind DR
???nat_mask 255.255.0.0
???protocol TCP
?
???real_server 172.16.249.116 80 {
???????weight 1
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
???real_server 172.16.249.123 80 {
???????weight 2
???????HTTP_GET {
???????????url {
????????????? path /
???????? ? ????status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
}
然后配置另一臺lvs主機中的的keepalived如下:
global_defs {
??notification_email {
???????? root@localhost
?? }
??notification_email_from kaadmin@localhost
??smtp_server 127.0.0.1
??smtp_connect_timeout 30
??router_id centos6.1.com
}
?
vrrp_script chk_schedown {
???script "[[ -f /etc/keepalived/down?]] && exit 1 || exit 0"
???interval 1
???weight -2
??? }
?
vrrp_instance VI_1 {
???state BACKUP
???interface eth0
???virtual_router_id 51
???priority 99
???advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 5b03429a7cf5
??? }
???virtual_ipaddress {
???????? 172.16.249.18/16dev eth0 label eth0:0
??? }
??track_script {
???????chk_schedown
??? }
???notify_master "/etc/keepalived/notify.sh master"
???notify_backup "/etc/keepalived/notify.sh backup"
???notify_fault "/etc/keepalived/notify.sh fault"
?
}
?
vrrp_instance VI_2 {
???state MASTER
???interface eth0
???virtual_router_id 61
???priority 100
???advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 20b662c6c06d
??? }
???virtual_ipaddress {
???????172.16.249.19/16 dev eth0 label eth0:1
??? }
}
?
virtual_server 172.16.249.18 80 {
???delay_loop 6
???lb_algo wrr
???lb_kind DR
???nat_mask 255.255.0.0
???protocol TCP
?
???real_server 172.16.249.116 80 {
???????weight 1
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
???real_server 172.16.249.123 80 {
???????weight 2
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
??????????? connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
}
?
virtual_server 172.16.249.19 80 {
???delay_loop 6
???lb_algo wrr
???lb_kind DR
???nat_mask 255.255.0.0
???protocol TCP
?
???real_server 172.16.249.116 80 {
???????weight 1
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
? ??real_server 172.16.249.123 80 {
???????weight 2
???????HTTP_GET {
???????????url {
????????????? path /
???????? ????? status_code 200
???????????}
???????????connect_timeout 3
???????????nb_get_retry 3
???????????delay_before_retry 3
???????}
??? }
}
這只在第一個虛擬路由器中做腳本,第二個沒有做,要想做了,跟第一個的一樣
然后就可以啟動keepalived服務了
然后訪問測試一下
可以看到兩個地址都能負載均衡
下面讓一臺lvs的keepalived下線
這樣兩個地址都在一臺lvs上了,訪問測試下
可以看到也沒有問題,
如果把一臺RS下線,所有請求就都在一個RS上了;如果RS都下線,sorry? server就可以發揮作用了。(這里不再演示了)
這樣高可用的lvs的負載均衡就做好了
?
?
下面做下nginx的高可用的負載均衡,就是用nginx做負載均衡器
使用nginx做負載均衡器,架構如下:
安裝nginx,然后啟動起來,配置成負載均衡服務器
修改/etc/nginx/nginx.conf
另一臺負載均衡器同樣這樣配置
?
然后修改這一臺nginx的keepalived的配置文件
global_defs {
??notification_email {
???????? root@localhost
?? }
??notification_email_from kaadmin@localhost
??smtp_server 127.0.0.1
??smtp_connect_timeout 30
??router_id centos7
}
vrrp_script chk_nginx {
???script "killall -0 nginx &> /dev/null"
???interval 1
???weight -10
??? }
?
vrrp_instance VI_1 {
???state MASTER
???interface eth0
???virtual_router_id 51
???priority 100
?? ?advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 5b03429a7cf5
??? }
???virtual_ipaddress {
???????? 172.16.249.18/16dev eth0 label eth0:0
??? }
???track_script {
???????? ?chk_nginx
??? }
???notify_master "/etc/keepalived/notify.sh master"
???notify_backup "/etc/keepalived/notify.sh backup"
???notify_fault "/etc/keepalived/notify.sh fault
}
?
另一臺nginx的keepalived配置文件為
global_defs {
??notification_email {
???????? root@localhost
?? }
??notification_email_from kaadmin@localhost
??smtp_server 127.0.0.1
??smtp_connect_timeout 30
??router_id centos6.1.com
}
?
vrrp_script chk_nginx {
???script "killall -0 nginx &> /dev/null"
???interval 1
???weight -10
??? }
?
vrrp_instance VI_1 {
???state BACKUP
???interface eth0
???virtual_router_id 51
???priority 99
???advert_int 1
???authentication {
???????auth_type PASS
???????auth_pass 5b03429a7cf5
??? }
???virtual_ipaddress {
???????? 172.16.249.18/16dev eth0 label eth0:0
??? }
??track_script {
???????? chk_nginx
??? }
???notify_master "/etc/keepalived/notify.sh master"
???notify_backup "/etc/keepalived/notify.sh backup"
???notify_fault "/etc/keepalived/notify.sh fault"
?
}
然后啟動keepalived
可以看到地址配好了,測試下
可以看到做好了,然后把一臺nginx的nginx服務停掉,看另一臺能否上線
訪問也沒有問題,這樣nginx的高可用就做好了,
?
做雙主模式的,只需要在keepalived配置文件里加入下面這些內容就行了
第一臺(172.16.249.195)
第二臺(172.16.249.147)
然后重啟keepalived服務
查看ip
訪問測試,
讓一臺的nginx服務停掉
可以看到能正常切換,這樣nginx高可用的雙主模式就做好了
?
?
轉載于:https://blog.51cto.com/10530982/1709067
總結
以上是生活随笔為你收集整理的Keepalived的相关应用,使用keepalived实现nginx和lvs的高可用负载均衡器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sqlserver 如何获取每组中的第一
- 下一篇: UIButton拖动响应事件,距离问题