keepalived+nginx高可用雙擊主備配置說明

實現要點

  1. 須要安裝keepalived和nginx,不須要haproxy
  2. keepalived實現兩個功能,一是浮動ip,二是nginx的高可用
  3. nginx實現zuul的負載均衡
  4. keepalived和nginx必須安裝在同一臺機器
  5. keepalived的啓動必須用root,而nginx的高可用是經過keepalived,因此keepalived和nginx都用root安裝和啓動快一點
  6. 至少兩臺機器安裝keepalived和nginx並啓動着,但同一個時間使用的是其中一臺,當這臺掛了,纔會使用另外一臺,感受可能兩臺就夠了,不須要三臺以上
  7. 浮動ip須要選擇同一個網段的、還沒使用的隨便一個ip就能夠

總述

21機器是vip,1八、19兩臺機器主備。nginx

測試

  1. 訪問http://1.13.22.21/vacorder/api/Subscribe,18和19的zuul輪流打印出日誌,nginx負載成功
  2. 停掉18的nginx,幾秒後自動啓動,18的keepalived自動啓動nginx成功
  3. 停掉18的keepalived,訪問http://1.13.22.21/api/Subs成功,19keepalived接手成功,雙機高可用成功

keepalived和nginx的經驗少,後續繼續分享。api

keepalived配置

18keepalived配置

[root[@test](https://my.oschina.net/azibug) keepalived]# more keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id test1
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx_pid.sh"
    interval 5
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 110
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
        1.13.22.21
    }
    track_script {
        chk_nginx
    }
}
[root[@test](https://my.oschina.net/azibug) keepalived]#

19keepalived配置

與18基本同樣,除了:bash

  1. router_id要改成本機名
  2. state要改成BACKUP
[root[@test2](https://my.oschina.net/u/1253032) keepalived]# more keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id test2
}

vrrp_script chk_nginx {
    script "/etc/keepalived/check_nginx_pid.sh"
    interval 5
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        1.13.22.21
    }
    
}

nginx配置

18nginx配置

[root[@test](https://my.oschina.net/azibug) nginx]# more nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    upstream upstream_nginx_zuul {
        server 1.13.22.18:8181;
        server 1.13.22.19:8181;
    }


    server {
        listen 80;
        location / {
           proxy_set_header Host $host:$server_port;
           proxy_pass http://upstream_nginx_zuul;
        }
    }

    include /etc/nginx/conf.d/*.conf;
}
[root@test1 nginx]#

19nginx配置

和18配置徹底同樣。app

[root@test nginx]# more nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    upstream upstream_nginx_zuul {
        server 1.13.22.18:8181;
        server 1.13.22.19:8181;
    }

    server {
        listen 80;
        location / {
           proxy_set_header Host $host:$server_port;
           proxy_pass http://upstream_nginx_zuul;
        }
    }

    include /etc/nginx/conf.d/*.conf;
}

高可用檢查腳本

兩臺機器同樣。負載均衡

[root@test2 keepalived]# more check_nginx_pid.sh 
#!/bin/bash
LOG_DIR="/etc/keepalived"  
echo $(date "+%Y-%m-%d %H:%M:%S") "check nginx status" >> $LOG_DIR/log_nginx.log 
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/sbin/nginx                #重啓nginx
      echo  $(date "+%Y-%m-%d %H:%M:%S") "restarting nginx..." >> $LOG_DIR/restart.log
      sleep 2
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重啓失敗,則停掉keepalived服務,進行VIP轉移
              #killall keepalived                    
              echo $(date "+%Y-%m-%d %H:%M:%S") "killing keepalived..." >> $LOG_DIR/restart.log
      fi
fi
相關文章
相關標籤/搜索