keepalive高可用集群(nginx)


一.keepalived服務概念說明


keepalived軟件能干什么?
  Keepalived軟件起初是專為LVS負載均衡軟件設計的,用來管理並監控LVS集群系統中各個服務節點的狀態,后來又加入了可以實現高可用的VRRP功能

Keepalived軟件主要是通過VRRP協議實現高可用功能的。
  VRRP是Virtual Router Redundancy Protocol(虛擬路由器冗余協議)的縮寫,VRRP出現的目的就是為了解決靜態路由單點故障問題的,它能夠保證當個別節點宕機時,整個網絡可以不間斷地運行

keepalived軟件工作原理?(重點)
  原理
  1)VRRP協議,全稱Virtual Router Redundancy Protocol,中文名為虛擬路由冗余協議,VRRP的出現是為了解決靜態路由的單點故障。
  2)VRRP是用過IP多播的方式(默認多播地址(224.0.0.18))實現高可用對之間通信的。
  3)工作時主節點發包,備節點接包,當備節點接收不到主節點發的數據包的時候,就啟動接管程序接管主節點的資源。備節點可以有多個,通過優先級競選,但一般Keepalived系統運維工作中都是一對。

 

二.   環境設置

1.普通web上修改, 其中一台的nginx的配置  3台都一樣

[root@web01 extra1]# cat www.conf  bbs.conf 
  server {
    listen        80;
    server_name    www.augustyang.org;
    root        html/www;
    index        index.html index.htm;
 }
    server {
        listen       80;
        server_name  bbs.augustyang.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
    }

 

 

2.lb 上的nginx.conf的配置 2台的配置是一樣的

worker_processes  1;
events {
   worker_connections  1024;
        }
   http {
      include       mime.types;
      default_type  application/octet-stream;
      sendfile        on;
      keepalive_timeout  65;
      upstream oldboy {
           server 10.0.0.7:80;
           server 10.0.0.8:80;
           server 10.0.0.9:80;
            }

   server {
      listen       80;
      server_name  www.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
        proxy_pass http://oldboy;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
                 }   
    }

   server {
      listen       80;
      server_name  bbs.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
          proxy_pass http://oldboy;
          proxy_set_header host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
                 }
    }   
 
}

 

 

3.在lb1 lb2上都做測試

curl -H host:www.augustyang.org 10.0.0.7
curl -H host:www.augustyang.org 10.0.0.8
curl -H host:www.augustyang.org 10.0.0.9
curl -H host:bbs.augustyang.org 10.0.0.7
curl -H host:bbs.augustyang.org 10.0.0.8
curl -H host:bbs.augustyang.org 10.0.0.9

 

三.安裝部署高可用keepalived服務

1.安裝keepalived服務

  yum install -y keepalived

 

2.修改配置文件

  vim /etc/keepalived/keepalived.conf

   man keepalived.conf   --- 配置文件說明信息

配置文件結構:
  GLOBAL CONFIGURATION --- 全局配置(*)
  VRRPD CONFIGURATION --- vrrp配置(*)
  LVS CONFIGURATION --- LVS服務相關配置

lb01  主負載均衡器配置

! Configuration File for keepalived
 global_defs {
       router_id lb01
       }

 vrrp_instance gorup01 {
      state MASTER
      interface eth0
       virtual_router_id 51
       priority 150
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3/24 dev eth0 label eth0:1
         }
     }

 

 

lb02  從負載均衡器配置

! Configuration File for keepalived
 global_defs {
       router_id lb02
       }
       
 vrrp_instance gorup01 {
      state BACKUP
      interface eth0
       virtual_router_id 51
       priority 100
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress { 
            10.0.0.3/24 dev eth0 label eth0:1
         }
     }

 

 

基本實現高可用負載均衡, 但有缺陷

 

四 部署keepalived高可用問題

同時在keepalived高可用集群中, 出現2個虛擬ip地址信息,這種腦裂情況

腦裂情況出現原因:

    1.心跳線出現問題

     .網卡配置有問題

      交換設備有問題

    線纜連接有問題

2.有防火牆軟件阻止問題

3.virtual_router_id 配置數值不正確

  只要備服務器收不到組播包, 就會成為主, 二主資源沒有釋放,就會出現腦裂

  利用shell腳本實現監控管理

備用設備有vip 就表示不正常

  ① 出現主備切換

  ②出現腦裂情況

 

#!/bin/bash
check_info=$(ip a|grep -c 10.0.0.3)
if [ $check_info -ne 0 ]
then
    echo "keepalived server error!!!"
fi

 

 

五 實現nginx反向代理監控虛擬ip地址

 

1.需要實現監聽本地網卡上沒有的ip地址(lb1  lb2都修改)

echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
sysctl -p

 

 

2.編寫nginx反向代理配置(lb1  lb2都修改)

   server {
      listen      10.0.0.3:80;
      server_name  www.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
        proxy_pass http://oldboy;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
                 }
    }

   server {
      listen       10.0.0.3:80;
      server_name  bbs.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
          proxy_pass http://oldboy;
          proxy_set_header host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
                 }
    }

 

 

 

[root@lb01 conf]# /application/nginx/sbin/nginx -s stop
[root@lb01 conf]#  /application/nginx/sbin/nginx
[root@lb01 conf]# netstat -lntup|grep nginx
tcp        0      0 10.0.0.3:80                 0.0.0.0:*                   LISTEN      63640/nginx    

 

 

六 將keepalived服務和反向代理nginx服務建立聯系

當nginx 停止的時候那個 對應的keepalived也要停止

 

#!/bin/bash
web_info=$(ps -ef|grep [n]ginx|wc -l)
if [ $web_info -lt 2 ]
then
   /etc/init.d/keepalived stop
fi

 

 

2.運行腳本, 實現監控nginx服務

編輯keepalived服務配置文件

! Configuration File for keepalived
 global_defs {
       router_id lb01
       }

 vrrp_script check_web { #定義一個監控腳本,腳本必須有執行權限 script "/server/scripts/check_web.sh"
      #指定腳本間隔時間
      interval 2
      #腳本執行完成,讓優先級值和權重值進行運算,從而實現主備切換
      weight 2 }

 vrrp_instance gorup01 {
      state MASTER
      interface eth0
       virtual_router_id 51
       priority 150
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3/24 dev eth0 label eth0:1
         }


 track_script { check_web }

     }

 

 

七 實現高可用集群架構中雙主配置(互為主備配置)

lb1

! Configuration File for keepalived
 global_defs {
       router_id lb01
       }

 vrrp_script check_web {
      #定義一個監控腳本,腳本必須有執行權限
      script "/server/scripts/check_web.sh"
      #指定腳本間隔時間
      interval 2
      #腳本執行完成,讓優先級值和權重值進行運算,從而實現主備切換
      weight 2
    }

 vrrp_instance gorup01 {
      state MASTER
      interface eth0
       virtual_router_id 51
       priority 150
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3/24 dev eth0 label eth0:1
         }
  track_script {
      check_web
    }

}
 vrrp_instance gorup02 {
      state BACKUP
      interface eth0
       virtual_router_id 52
       priority 100
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.4/24 dev eth0 label eth0:2
         }

  track_script {
      check_web
    }
#
     }

 

 

lb2

! Configuration File for keepalived
 global_defs {
       router_id lb02
       }

 vrrp_script check_web {
      #定義一個監控腳本,腳本必須有執行權限
      script "/server/scripts/check_web.sh"
      #指定腳本間隔時間
      interval 2
      #腳本執行完成,讓優先級值和權重值進行運算,從而實現主備切換
      weight 2
    }

 vrrp_instance gorup01 {
      state BACKUP
      interface eth0
       virtual_router_id 51
       priority 100
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.3/24 dev eth0 label eth0:1
         }
  track_script {
      check_web
    }

}
vrrp_instance gorup02 {
       state MASTER
       interface eth0
       virtual_router_id 52
       priority 150
       advert_int 1
       authentication {
          auth_type PASS
          auth_pass 1111
        }
        virtual_ipaddress {
            10.0.0.4/24 dev eth0 label eth0:2
         }

 track_script {
     check_web
   }
}

 

 

   server {
      listen      10.0.0.3:80;
      server_name  www.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
        proxy_pass http://oldboy;
        proxy_set_header host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
                 }   
    }

   server {
      listen       10.0.0.4:80;
      server_name  bbs.augustyang.org;
      root   html;
      index  index.html index.htm;
      location /{
          proxy_pass http://oldboy;
          proxy_set_header host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
                 }
    }   
 
}

 

 

[root@lb01 scripts]# netstat -tulnp | grep nginx
tcp        0      0 10.0.0.4:80                 0.0.0.0:*                   LISTEN      65733/nginx         
tcp        0      0 10.0.0.3:80                 0.0.0.0:*                   LISTEN      65733/nginx   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM