1. 程式人生 > 實用技巧 >keepalived部署+nginx高可用

keepalived部署+nginx高可用

nginx+keepalived搞效能web網路架構實戰配置: 環境準備:
keepalived+nginx-1:     192.168.1.23
keepalived+nginx-2:     192.168.1.25

keepalived環境部署:(需要關閉防火牆與selinux)

yum方式部署:
yum install keepalived* -y
#配置檔案:
    /etc/keepalived/keepalived.conf

原始碼方式部署:

#下載版本keepalived-1.4.5版本/安裝配置:
wget https://www.keepalived.org/software/keepalived-1.4.5.tar.gz
tar xf keepalived
-1.4.5.tar.gz cd keepalived-1.4.5 ./configure --prefix=/usr/local/keepalived/ make make install mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp keepalived-1.4.5/keepalived/keepalived.service /etc/systemd/system/ ln
-s /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/ chmod 755 /etc/init.d/keepalived systemctl enable keepalived systemctl start keepalived ps -ef|gre keepalived

keepalived結合nginx實現高可用:(nginx安裝步驟省略)兩臺keepalived+nginx服務的keepalived配置檔案/nginx程序指令碼 差不多一樣:需要注意的是空格不能多寫 nopreempt不搶佔引數 只對BACKUP生效,對MASTER無效。


keepalived配置檔案內容如下:

! Configuration File for keepalived
global_defs {
  notification_email {
      [email protected]
 }
    notification_email_from wgkgood@163.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
}

vrrp_script chk_nginx {
    script  "/data/sh/auto_check_nginx.vip.sh"
    interval 2
    weight 2
}

#VIP1
vrrp_instance VI_1 {
     state  BACKUP
     interface ens33
     virtual_router_id 151
     priority 100
     nopreempt
     advert_int 5
     authentication {
         auth_type  PASS
         auth_pass  1111

     }
     virtual_ipaddress {
         192.168.1.198
     }
     track_script {
        chk_nginx
     }
}

監控nginx服務程序指令碼:
vim /data/sh/auto_check_nginx.vip.sh

NGX_MUX="$(ps -ef|grep nginx|grep -v "grep"|grep -v "check"|wc -l)"
if [ $NGX_MUX -eq 0 ];then
        systemctl stop keepalived
fi

配置keepalived主主模式:

! Configuration File for keepalived
global_defs {
  notification_email {
      [email protected]
 }
    notification_email_from wgkgood@163.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
}

vrrp_script chk_nginx {
    script  "/data/sh/auto_check_nginx.vip.sh"
    interval 2
    weight 2
}

#VIP1
vrrp_instance VI_1 {
     state  BACKUP
     interface ens33
     virtual_router_id 151
     priority 90
     nopreempt
     advert_int 5
     authentication {
         auth_type  PASS
         auth_pass  1111

     }
     virtual_ipaddress {
         192.168.1.198
     }
     track_script {
        chk_nginx
     }
}

#VIP2
vrrp_instance VI_2 {
     state  MASTER
     interface ens33
     virtual_router_id 152
     priority 100
     nopreempt
     advert_int 5
     authentication {
         auth_type  PASS
         auth_pass  2222

     }
     virtual_ipaddress {
         192.168.1.199
     }
     track_script {
        chk_nginx
     }
}