便民 发表于 2018-12-30 09:46:35

keepalived实现nginx的高可用(双主模型)

  实验环境:
      RS1:rip(172.16.125.7),安装httpd软件包;

      RS2:rip(172.16.125.8),安装httpd软件包;
      director1(7-1.lcs.com):vip(172.16.125.100),dip(172.16.125.5),安装nginx、keepalived软件包。
      director2(7-2.lcs.com):vip(172.16.125.110),dip(172.16.125.6),安装nginx、keepalived软件包。
      首先关闭所有节点上iptables和selinux,同时进行时间同步。

  使用双主模型实现nginx的高可用:
  (1)在两台keepalived高可用主机上安装keepalived和nginx软件包,并提供配置文件。
      a.首先使用时间同步,保证节点上的时间是同步的。
      b.首先使用nginx反向代理实现后台RS上的web服务的负载均衡,在nginx的主配置文件(/etc/nginx/nginx.conf)中,进行修改。
        在http段中添加upstream,将后端两台RS加入到该upstream中。
upstream myblancer {
      server 172.16.125.7:80;
      server 172.16.125.8:80;
    }        在server段中location /{}中加入代理,实现负载均衡调度。
location / {
                proxy_pass http://myblancer;
      }      c.进行检测,是否实现了后端RS的轮询调度。
  
http://s3.运维网.com/wyfs02/M02/74/ED/wKiom1YuUPyiYNh5AABqOkG5Czo963.jpg
http://s3.运维网.com/wyfs02/M02/74/EA/wKioL1YuUS-BOV2PAABtt-hkyzo971.jpg
  
  (2)上述说明已经通过nginx的反向代理实现了http服务的负载均衡。在此处就可以开始配置keepalived的主配置文件,实现对nginx的高可用。详细配置文件如下:
      keepalived的配置文件1:
global_defs {
   notification_email {
   root@localhost
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "killall -0 nginx &> /dev/null"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 100
    }
    virtual_ipaddress {
172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
    track_script {
      chk_nginx
   }
}
vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 110
    priority 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 110
    }
    virtual_ipaddress {
172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
    track_script {
      chk_nginx
   }
}      keepalived配置文件2:
global_defs {
   notification_email {
   root@localhost
   failover@firewall.loc
   sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {
    script "killall -0 nginx &> /dev/null"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 100
    priority 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 100
    }
    virtual_ipaddress {
172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
    track_script {
      chk_nginx
   }
}
vrrp_instance VI_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 110
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 110
    }
    virtual_ipaddress {
172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
   track_script {
      chk_nginx
   }
}      使用如下这个脚本检测nginx服务的状态,如果nginx服务发生了问题,那么在该服务器上的vip会自动流动到另一台高可用服务器上。使用下边这个脚本要保证高可用服务器上有killall命令,该命令是由psmisc软件包提供。
vrrp_script chk_nginx {
    script "killall -0 nginx &> /dev/null"
    interval 1
    weight -20
}  
        通过这一步的keepalived的配置,keepalived服务已经配置完成,那么在这两台director上启动keepalived和nginx服务。

        a.查看两台director上vip是否已经添加成功。
  
http://s3.运维网.com/wyfs02/M00/74/EA/wKioL1YuUVbDb7KpAAIaH9Q5ahM879.jpg
http://s3.运维网.com/wyfs02/M01/74/ED/wKiom1YuUSTBUZZYAAHjvD_h5R8330.jpg
        b.分别访问vip(172.16.125.100)和vip(172.16.125.110)。
        http://s3.运维网.com/wyfs02/M01/74/EA/wKioL1YuUYrjUdE4AADFuKwNZRg368.jpg
      c.在director1上关闭nginx服务,检测到vip已经消失,转移到director2上。
  
http://s3.运维网.com/wyfs02/M02/74/ED/wKiom1YuUYCz6I-iAAF45PdWVKI906.jpg
http://s3.运维网.com/wyfs02/M02/74/EA/wKioL1YuUbKy2RVrAAJA2xYx9So321.jpg
  


        

  




页: [1]
查看完整版本: keepalived实现nginx的高可用(双主模型)