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
相关文章
相关标签/搜索