利用keepalived实现nginx调度器高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用keepalived实现nginx调度器高可用相关的知识,希望对你有一定的参考价值。

利用keepalived实现nginx调度器高可用

    声明:提供四台主机,其中两台nginx做前端调度器(一台做主调度器,一台做备用调度器),

              另外两台主机做web服务器向外提供http服务;

           框架如图:

           技术分享图片

        1.在两台nginx上配置nginx反代服务

            # vim /etc/nginx/nginx.conf

             在http上下文中添加下文:

             upstream webser {
             server 172.16.1.12:80 weight=1;
             server 172.16.1.13:80 weight=1;
             }
             server {
                 listen       80 default_server;

                 location / {
                       proxy_pass http://webser;
                 }

             }

            配置完成后,启动nginx服务,使配置生效;

        

         2.配置keepalived+nginx_master(172.16.1.11主机):(前提,安装keepalived : # yum install keepalived)

           # vim /etc/keepalived/keepalived.conf

              ! Configuration File for keepalived

             global_defs {
                     notification_email {
                                [email protected]
                     }
                     notification_email_from [email protected]
                     smtp_server 127.0.0.1
                     smtp_connect_timeout 30
                     router_id drct1
                    vrrp_mcast_group4 224.0.100.18
             }
             vrrp_script check_httpd {
                    script "killall -0 nginx && exit 0 || exit 1"
                    interval 1
                    weight -20
              }
              vrrp_instance VI_1 {
                    state BACKUP
                    interface ens33
                    virtual_router_id 51
                    priority 100
                    advert_int 1
                    authentication {
                           auth_type PASS
                           auth_pass cxqLUKJh
                    }
                   virtual_ipaddress {
                          172.16.1.254/16
                    }
                   track_script {
                           check_httpd
                    }
               }

            启动keepalived服务:

             # systemctl start keepalived.service


            3.配置keepalived+nginx_backup(172.16.1.13主机):

              

           # vim /etc/keepalived/keepalived.conf

              ! Configuration File for keepalived

             global_defs {
                     notification_email {
                                [email protected]
                     }
                     notification_email_from [email protected]
                     smtp_server 127.0.0.1
                     smtp_connect_timeout 30
                     router_id drct2
                    vrrp_mcast_group4 224.0.100.18
             }
             vrrp_script check_httpd {
                    script "killall -0 nginx && exit 0 || exit 1"
                    interval 1
                    weight -20
              }
              vrrp_instance VI_1 {
                    state BACKUP
                    interface ens33
                    virtual_router_id 51
                    priority 90
                    advert_int 1
                    authentication {
                           auth_type PASS
                           auth_pass cxqLUKJh
                    }
                   virtual_ipaddress {
                          172.16.1.254/16
                    }
                   track_script {
                           check_httpd
                    }
               }

            配置完成后,启动keepalived服务:

                   # systemctl start keepalived


           4.配置后端web服务器

              1)安装httpd

                    # yum install http

              2) 做web页面:

                    webserver1:

                         vim /var/www/html/index.html

                         Real Server 1

             

                    webserver2:

                         vim /var/www/html/index.html                      

                         Real Server 2

              3) 启动http服务:

                     # systemctl start httpd


            5.用一个客户端测试:

                   #  for i in {1..10}; do curl http://172.16.1.254;done
                       Real Server 2
                       Real Server 1
                       Real Server 2
                       Real Server 1
                       Real Server 2
                       Real Server 1
                       Real Server 1
                       Real Server 2
                       Real Server 1
                       Real Server 2

 
















           

以上是关于利用keepalived实现nginx调度器高可用的主要内容,如果未能解决你的问题,请参考以下文章

keepalived双主模式实现nginx高可用及LNAMMP架构

keepalive实验

利用keepalived实现高可用nginx

Nginx LB+keepalived+varnish+NAMP架构部署wordpress实现动静分离

Keepalived+nginx实现高可用

生产环境模拟实现keepalived+Nginx调度器+httpd的高可用集群