nginx作負載均衡,能達到分發請求的目的,可是不能很好的避免單點故障。html
Keepalived的做用是檢測服務器的狀態,若是有一臺web服務器宕機,或工做出現故障,Keepalived將檢測到,並將有故障的服務器從系統中剔除,同時使 其餘服務器代替該服務器的工做,當服務器工做正常後Keepalived自動將服務器加入到服務器羣中,這些工做所有自動完成,不須要人工干涉,須要人工作的只是修復故障的服務器。
總結來講:Keepalived軟件是一個監控+自愈的軟件。
運行協議是VRRP,主分發器的keepalived會向網絡中發組播,宣告本身還活着,組播地址:224.0.0.18。linux
$ tcpdump -nn -vvv -i ens33 vrrp #查看組播的包 192.168.31.40 > 224.0.0.18: vrrp 192.168.31.40 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 100, authtype simple, intvl 1s, length 20, addrs: 192.168.31.213 auth "1111^¥##" 輸出解析:192.168.31.40是master,目標地址是224.0.0.18,使用的協議是VRRPv2版本,虛擬id是51,優先級是100,簡單驗證類型,一秒發一次,長度20字節,虛Ip是192.168.31.213, 驗證的密碼是111#$Z%#
keepalived官網下載地址
linux下載keepalived軟件:nginx
$ wget http://www.keepalived.org/software/keepalived-2.0.8.tar.gz
使用以下shell腳本keepalived_instll.sh安裝keepalived軟件。web
$ sh keepalived_install.sh #keepalived安裝腳本 #!/bin/bash pkg=keepalived-2.0.8.tar.gz tar xf $pkg yum -y install kernel-devel ln -s /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/ /usr/src/linux cd keepalived-2.0.8/ yum install openssl-* -y ./configure --prefix=/usr/local/keepalived make make install mkdir -pv /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/
$ systemctl start keepalived
主機名 | IP | 角色 | 系統 | 配置 | 軟件 |
---|---|---|---|---|---|
Master.ayitula.com | 192.168.31.40 | 主分發器 | centos7.5 | 2核4G | Nginx+keepalived |
Backup.ayitula.com | 192.168.31.41 | 備分發器 | centos7.5 | 2核4G | Nginx+keepalived |
Web01.ayitula.com | 192.168.31.42 | 數據服務器1 | centos7.5 | 2核4G | Nginx |
Web02.ayitula.com | 192.168.31.43 | 數據服務器2 | centos7.5 | 2核4G | Nginx |
1)分發器:nginx+keepalived
2)數據服務器:Nginx
3)配置Nginx分發器
4)配置數據服務器頁面(web01 web02)
5)配置keepalived.conf
6)測試shell
upstream web { server 192.168.31.42 max_fails=2 fail_timeout=3; # 超時時間3秒內失敗2次認爲服務器死了 server 192.168.31.43 max_fails=2 fail_timeout=3; } server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_script check_nginx { # 定義一個腳本 script "/etc/keepalived/nginx_pid.sh" # 腳本路徑 interval 2 # 探針,此處是每兩秒執行一次腳本 fall 1 # 失敗次數 1 } vrrp_instance nginx { # 定義名爲nginx實例 state MASTER # 主機狀態 interface ens33 # 網卡 mcast_src_ip 192.168.31.40 # 發組播 virtual_router_id 51 # 虛擬id priority 100 # 優先級 advert_int 1 # 探針,一秒發一次組播 authentication { auth_type PASS auth_pass 1111 # 密碼 } track_script { check_nginx } virtual_ipaddress { # 虛ip 192.168.31.213/24 } }
#cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_script check_nginx { script "/etc/keepalived/nginx_pid.sh" interval 2 fall 1 } vrrp_instance nginx { state BACKUP interface ens33 mcast_src_ip 192.168.31.41 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.31.213/24 } }
#!/bin/bash nginx_kp_check () { nginxpid=`ps -C nginx --no-header |wc -l` if [ $nginxpid -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 1 nginxpid=`ps -C nginx --no-header |wc -l` if [ $nginxpid -eq 0 ];then systemctl stop keepalived fi fi } nginx_kp_check
腳本原理:該腳本檢查nginx進程是否存在,若是進程沒了,說明分發器掛了,嘗試啓動分發器,1秒後檢查分發器啓動沒有,若是沒有啓動直接關閉keepalived,中止發組播,備就自動開始工做了。centos
分發器由keepalived來管理bash
$ watch -n1 killall nginx # 每隔一秒殺死一次全部nginx
數據服務器則由nginx來管理。使用以下兩個參數就可實現對數據服務器管理:服務器
upstream web { server 192.168.31.42 max_fails=2 fail_timeout=3; # 超時時間3秒內失敗2次認爲服務器死了 server 192.168.31.43 max_fails=2 fail_timeout=3; }