keepalived+nginx的主備架構模式配置主主教程

keepalived+nginx的主備架構模式,始終會有一臺服務器處於空閑狀態,可以通過keepalived+nginx主主架構來實現兩臺服務器的合理利用,主主服務器對外有兩個VIP地址來接收客戶端的請求。

環境

192.168.2.118 nginx主/keepalived-master1

192.168.2.119 nginx從/keepalived-master2

192.168.2.112 lap

配置主主

在上一章已經在118和119服部署好瞭keepalived服務。接下來配置雙主結構

備份好118和119的keepalived配置文件

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

118的keepalived配置文件內容如下

! Configuration File for keepalived
 global_defs {
  notification_email {
      abc@163.com
 }
    notification_email_from abc@163.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }
 vrrp_script chk_nginx {
    script "/data/sh/check_nginx.sh"
    interval 2
    weight 2
 }
 vrrp_instance VI_1 {
     state MASTER
     interface eth0
     lvs_sync_daemon_inteface eth0
     virtual_router_id 50
     priority 100
     advert_int 5
     nopreempt
     authentication {
         auth_type  PASS
         auth_pass  1111
     }
virtual_ipaddress { 
         192.168.2.130
     }
     track_script {
     chk_nginx
    }
 }
 vrrp_instance VI_2 {
     state BACKUP
     interface eth0
     lvs_sync_daemon_inteface eth0
     virtual_router_id 51
     priority  90
     advert_int 5
     nopreempt
     authentication {
         auth_type  PASS
         auth_pass  2222
     }
     virtual_ipaddress {
         192.168.2.131
     }
     track_script {
     chk_nginx
    }
 }

119服的keepalived配置文件參考上面的參數,把V1的state改成BACKUP,優先級priority改成90,把V2的state改成MASTER,優先級priority改成100


nginx檢測腳本check_nginx.sh(註意執行權限chmod u+x check_nginx.sh)

#!/bin/bash
#2017年12月5日21:50:40
#auto check nginx
#by lee
###################
killall -0 nginx
if [[ $? -ne 0 ]];then
        /etc/init.d/keepalived stop
fi

啟動118和119的keepalived服務並查看日志

service keepalived start
tail -f /var/log/messages

可以看到進入瞭master


查看ip

ip addr list

118服:

這裡寫圖片描述

119服:

這裡寫圖片描述


瀏覽器分別訪問:

這裡寫圖片描述

這裡寫圖片描述

手動停止118的nginx服務,然後再訪問192.168.2.130,可以正常的打開web界面

查看VIP,可以看到VIP:192.168.2.130移到瞭119服下:

這裡寫圖片描述

再次手動啟動118的服務

[root@mysqlm keepalived]# /usr/local/nginx/sbin/nginx 
[root@mysqlm keepalived]# service keepalived start

可以看到VIP:192.168.2.130又返回到118服下

至此keepalived+nginx雙主結構配置完成。

You May Also Like