HAProxy+Keepalived 高可用負載均衡
阿新 • • 發佈:2018-11-24
link ipad 配置 make type 負載均衡 pro ext figure
轉自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy實現高可用負載均衡 Master 192.168.0.69 haproxy、keepalived Centos7.4 backup 192.168.0.70 haproxy、keepalived Centos7.4 vip(虛擬IP) 192.168.0.180 192.168.0.181 1、兩臺都安裝haproxy,參照安裝haproxy文檔 2、 兩臺都安裝Keepalived yum -y install gcc automake autoconf libtool gcc-c++ gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel pcre pcre-devel yum install -y libnl libnl-devel libnfnetlink-devel popt-devel cd /usr/local/src/ tar -zxvf keepalived-1.2.18.tar.gz cd keepalived-1.2.18 ./configure --prefix=/usr/local/keepalived make && make install 3、將keepalived安裝成Linux系統服務 mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ chmod+x /etc/init.d/keepalived ln -s /usr/local/sbin/keepalived /usr/sbin/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ ln -s /usr/local/keepalived/sbin/keepalived /usr/local/sbin/ chkconfig keepalived on 4、修改keepalived配置文件 接下來就是配置了,很簡單,之前的刪除,直接復制下面配置文件 先是主服務器: vi /etc/keepalived/keepalived.conf global_defs { notification_email #通知email,根據實際情況配置 { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id node1 #節點名標識,主要用於通知中 } vrrp_script chk_http_port { script "/etc/keepalived/chk_haproxy.sh" #在這裏添加腳本鏈接 interval 3 #腳本執行間隔 weight 2 #腳本結果導致的優先級變更 } vrrp_script chk_http_port { script "/etc/keepalived/chk_haproxy.sh" #在這裏添加腳本鏈接 interval 3 #腳本執行間隔 weight 2 #腳本結果導致的優先級變更 } vrrp_instance VI_NODE_1 { state MASTER #配置為主服務器 interface ens33 #通訊網卡 virtual_router_id 100 #路由標識 priority 200 #優先級,0-254 advert_int 5 #通知間隔,實際部署時可以設置小一點,減少延時 authentication { auth_type PASS auth_pass 123456 #驗證密碼,用於通訊主機間驗證 } track_script { chk_http_port #添加腳本執行 } virtual_ipaddress { 192.168.0.180 #虛擬ip,可以定義多個 } } vrrp_instance VI_NODE_2 { state MASTER #配置為主服務器 interface ens33 #通訊網卡 virtual_router_id 101 #路由標識 priority 100 #優先級,0-254 advert_int 5 #通知間隔,實際部署時可以設置小一點,減少延時 authentication { auth_type PASS auth_pass 123456 #驗證密碼,用於通訊主機間驗證 } track_script { chk_http_port #添加腳本執行 } virtual_ipaddress { 192.168.0.181 #虛擬ip,可以定義多個 } } 接下是從服務器設置: vi /etc/keepalived/keepalived.conf global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id node2 } vrrp_script chk_http_port { script "/etc/keepalived/chk_haproxy.sh" #在這裏添加腳本鏈接 interval 3 #腳本執行間隔 weight 2 #腳本結果導致的優先級變更 } vrrp_script chk_http_port { script "/etc/keepalived/chk_haproxy.sh" #在這裏添加腳本鏈接 interval 3 #腳本執行間隔 weight 2 #腳本結果導致的優先級變更 } vrrp_instance VI_NODE_1 { state BACKUP #與主服務器對應 interface ens33 #從服務器的通信網卡 virtual_router_id 100 #路由標識,和主服務器相同 priority 100 #優先級,小於主服務器即可 advert_int 5 #這裏是接受通知間隔,與主服務器要設置相同 authentication { auth_type PASS auth_pass 123456 #驗證密碼,與主服務器相同 } track_script { chk_http_port #添加腳本執行 } virtual_ipaddress { 192.168.0.180 #虛擬IP,也要和主服務器相同 } } vrrp_instance VI_NODE_2 { state BACKUP #與主服務器對應 interface ens33 #從服務器的通信網卡 virtual_router_id 101 #路由標識,和主服務器相同 priority 100 #優先級,小於主服務器即可 advert_int 5 #這裏是接受通知間隔,與主服務器要設置相同 authentication { auth_type PASS auth_pass 123456 #驗證密碼,與主服務器相同 } track_script { chk_http_port #添加腳本執行 } virtual_ipaddress { 192.168.0.181 #虛擬IP,也要和主服務器相同 } } 5 兩個節點配置check_ngixn腳本 當腳本檢測到haproxy沒有運行的時候會嘗試去啟動haproxy以此,如果失敗則停掉keepalived進程 vi /usr/local/keepalived/haproxy.sh tatus=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l) if [ "${status}" = "0" ]; then /usr/local/haproxy/sbin -f /usr/local/haproxy/conf/haproxy.cfg status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l) if [ "${status2}" = "0" ]; then /etc/init.d/keepalived stop fi fi 6、啟動keepalived [root@bogon ~]# service keepalived start Starting keepalived (via systemctl): [ OK ] 7、查看虛擬ip在哪臺上, 註意過兩分鐘才出現vip [root@bogon ~]# ip a [root@bogon ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:24:21:c4 brd ff:ff:ff:ff:ff:ff inet 192.168.0.69/24 brd 192.168.0.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.0.180/32 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.0.181/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::d675:3ae7:5113:ad71/64 scope link noprefixroute valid_lft forever preferred_lft forever 9、訪問:http://192.168.0.181:1080/stats http://192.168.0.181:1080/stats 賬號密碼: admin/admin 10、高可用切換 在192.168.0.69停止keepalived , 查看192.168.0.180飄到了192.168.0.70上 service keepalived stop 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:ba:67:74 brd ff:ff:ff:ff:ff:ff inet 192.168.0.70/24 brd 192.168.0.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.0.180/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::6c33:a5d6:2ea9:d781/64 scope link noprefixroute valid_lft forever preferred_lft forever inet6 fe80::d675:3ae7:5113:ad71/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever
HAProxy+Keepalived 高可用負載均衡