利用keepalived实现nginx调度器高可用(一)

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

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

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

           框架如图:

           QQ截图20180615171527.png

        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 {
                                root@localhost
                     }
                     notification_email_from keepalived@localhost
                     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 {
                                root@localhost
                     }
                     notification_email_from keepalived@localhost
                     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

 
















           

猜你喜欢

转载自blog.51cto.com/yuantianchi/2129943
今日推荐