1. 程式人生 > >keepalived+nginx-upstream部署高可用反向代理

keepalived+nginx-upstream部署高可用反向代理

proxy route 服務器 宕機 127.0.0.1 trac 訪問 代理 _id

實驗拓撲

技術分享

實驗要求

  1. 兩個web server提供httpd服務,ip地址分別是172.18.27.201、202,掩碼是16
  2. 兩個nginx proxy提供高可用反向代理,ip地址分別是172.18.27.102、200,掩碼是16.
  3. client能夠訪問web server,使用dr模型反向代理。

實驗步驟

    1. 各主機安裝軟件

      ##web server安裝httpd
      yum install -y httpd
      ##nginx proxy安裝nginx和keeplived.
      yum install -y keepalived nginx
    2. 配置web server

      ##兩臺web server配置,兩臺都為centos7.2
      #RS1配置
      vim /var/www/html/index.html
      <h1/>RS1:172.18.27.201</h1>
      #RS2配置
      vim /var/www/html/index.html
      <h1/>RS2:172.18.27.202</h1>
      #RS1和RS2啟動httpd服務
      systemct start httpd
    3. 配置nginx proxy

      ##配置nginx proxy服務器
      #配置nginx upstream
      vim /etc/nginx/nginx.conf
      http {
           .....
        upstream httpdserver{
           server 172.18.27.201:80 weight=2;
           server 172.18.27.202:80 weight=1;
           server 127.0.0.1:8080 backup;
       }
      }
      #配置keeplived
      vim /etc/keepalived/keepalived.conf
      global_defs {
      vrrp_mcast_group4 224.100.27.1
      }
      vrrp_script chk_down {
                   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
                   interval 1
                   weight -5
                           }
      vrrp_script chk_nginx {
                   script "killall -0 nginx && exit 0 || exit 1"
                   interval 1
                   weight -5
                   fall 2
                   rise 1
      }
      vrrp_instance VI_1 {
       state MASTER
       interface eth1
       virtual_router_id 51
       priority 100
       advert_int 1
       nopreempt
       authentication {
           auth_type PASS
           auth_pass 1111
       }
       virtual_ipaddress {
           172.18.27.254/16 dev eth1 label eth1:0
       }
       track_script {
           chk_down
           chk_nginx
       }
       track_interface{
           eth1
       }
      }
      #啟動服務
      service nginx start
      service keepalived start

      配置另一臺nginx proxy

      #配置nginx upstream
      vim /etc/nginx/nginx.conf
      http {
           .....
        upstream httpdserver{
           server 172.18.27.201:80 weight=2;
           server 172.18.27.202:80 weight=1;
           server 127.0.0.1:8080 backup;
       }
      }
      #配置keeplived
      vim /etc/keepalived/keepalived.conf
      global_defs {
      vrrp_mcast_group4 224.100.27.1
      }
      vrrp_script chk_down {
                   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
                   interval 1
                   weight -5
                           }
      vrrp_script chk_nginx {
                   script "killall -0 nginx && exit 0 || exit 1"
                   interval 1
                   weight -5
                   fall 2
                   rise 1
      }
      vrrp_instance VI_1 {
       state BACKUP
       interface eno16777736
       virtual_router_id 51
       priority 98
       advert_int 1
       nopreempt
       authentication {
           auth_type PASS
           auth_pass 1111
       }
       virtual_ipaddress {
           172.18.27.254/16 dev een016777736 label eno16777736:0
       }
       track_script {
           chk_down
           chk_nginx
       }
       track_interface{
           eno16777736
       }
      }
      #啟動服務
      systemctl start nginx keepalived

      試驗效果

      正常狀態

      技術分享

      一個web server 宕機了

      技術分享

      一個nginx proxy服務器宕機了

      技術分享
      技術分享
      能夠正常運行

keepalived+nginx-upstream部署高可用反向代理