keepalived+nginx+apache双活搭建(双网卡模式)

之前写过一篇文章《keepalived+nginx+apache主备及双活搭建测试》,该测试环境只有一张网卡,双活的ip都在该网卡上。

本文背景:自动化运维平台的前置机部署在云平台,服务器有两张网卡,分别对应带外ip和业务ip;云平台的被管服务器访问带外虚ip,非云平台的物理服务器访问业务的虚ip。


架构图:

图片.png


配置:

主机
ip
操作系统
软件
vip
nginx01

172.27.9.91

172.27.18.127

Centos7.3.1611

nginx 端口82

keepalived

172.27.9.200
nginx02

172.27.9.92

172.27.18.128

Centos7.3.1611

nginx 端口82

keepalived

172.27.18.120
web01
172.27.9.125
Centos7.3.1611 apache 端口1180
/
web02
172.27.9.126
Centos7.3.1611 apache 端口1180

/

vmware版本:12.5.2 build-4638234


1.vmware新增网卡

打开‘虚拟网络编辑器’

图片.png

‘VMnet0’和‘VMnet2’分别对应pc机物理网卡和无线网卡:

图片.png

新增网卡及配置:

图片.png


2.keepalived配置

nginx01上keepalived配置:

[root@nginx01 keepalived]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id proxy1 
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 1
  rise 10
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state BACKUP 
    interface ens37
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.18.120
    }
    track_script {
        chk_nginx
    }
}

nginx02上keepalived配置:

[root@nginx02 keepalived]# more /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id proxy2
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight 20
  fall 2
  rise 1
}
vrrp_instance VI_1 {
    state BACKUP 
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.9.200
    }
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens37
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.27.18.120
    }
    track_script {
        chk_nginx
    }
}

check_nginx.sh脚本配置同之前文章


3.nginx配置

两台nginx服务器配置相同如下:

[root@nginx02 keepalived]# more /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    upstream webser{
             server 172.27.9.125:1180;
             server 172.27.9.126:1180;
             server 172.27.18.127:1180;
             server 172.27.18.128:1180;
           }
    server {
        listen       82;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://webser;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


4.启动服务

启动两台服务器nginx和keepalived服务:

[root@nginx01 ~]# nginx
[root@nginx01 ~]# service keepalived start
Redirecting to /bin/systemctl start  keepalived.service


5.查看vip

图片.png

图片.png


6.页面访问

vip1:http://172.27.9.200:82/

vip2:http://172.27.18.120:82/

图片.png图片.png图片.png图片.png

刷新web,发现vip1和vip2分别以轮询方式访问web服务器


7.高可用测试

之前文章,不再赘述。


测试完成,云平台生产环境也可以按本次测试实施。

猜你喜欢

转载自blog.51cto.com/3241766/2129948
今日推荐