您的位置:澳门新葡8455最新网站 > 澳门新葡8455最新网站 > 落到实处双主模型的nginx的高可用,Keepalived达成

落到实处双主模型的nginx的高可用,Keepalived达成

发布时间:2019-12-02 18:02编辑:澳门新葡8455最新网站浏览(174)

    得以完毕双主模型的ngnix高可用(后生可畏卡塔尔

    图片 1

    尝试指标:使用keepalived完成Nginx的双主高可用负载均衡集群。

    准备:主机7台

    client:

    172.18.x.x

    调度器:keepalived+nginx 带172.18.x.x/16 网卡

    192.168.234.27

    192.168.234.37

    real_server

    192.168.234.47

    192.168.234.57

    192.168.234.67

    192.168.234.77

    实施际遇:两台Nginx proxy(双主Nginx,各要求两块网卡,eth0连接内网,eth1连接外网卡塔尔、两台web server(央求的载荷均衡卡塔尔(قطر‎、后生可畏台client用于注脚结果。

    实验结果

      1 [root@234c17 ~]# for i in {1..4};do curl www.a.com;curl www.b.com;sleep 1;done
      2 234.57
      3 234.77
      4 234.47
      5 234.67
      6 234.57
      7 234.77
      8 234.47
      9 234.67
    

    图片 2

    过程:

    注意:为了不影响实验结果,在实验始于前先关闭iptables和selinux

    一、先配置4台real_server,安装好测量试验用的httpd

      1 [root@234c47 ~]# curl 192.168.234.47;curl 192.168.234.57;curl 192.168.234.67;curl 192.168.234.77
      2 234.47
      3 234.57
      4 234.67
      5 234.77
    

    操作步骤:

    二、配置keepalived

    因为是双主模型

    一、配置IP

    1.配置keepalived主机234.27

    [root@234c27 ~]# 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 kpone
        vrrp _mcast_group4 234.10.10.10
     }
     vrrp_instance VI_1 {
         state MASTER
         interface ens33
         virtual_router_id 50
         priority 100
         advert_int 1
         authentication {
             auth_type PASS
             auth_pass 1111
         }
         virtual_ipaddress {
             172.18.0.100/16  //这ip调度 192.168.234.47/57
         }
     }
    vrrp_instance VI_2 {
         state BACKUP
         interface ens33
         virtual_router_id 51
         priority 80
         advert_int 1
         authentication {
             auth_type PASS
             auth_pass 2222
         }
         virtual_ipaddress {
             172.18.0.200/16  //这ip调度 192.168.234.147/157
         }
    }
    

    1.配置A主机的IP

    2.配置keepalived主机234.37

    [root@234c37 ~]# 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 kpone
        vrrp _mcast_group4 234.10.10.10
     }
     vrrp_instance VI_1 {
         state BACKUP
         interface ens33
         virtual_router_id 50
         priority 80
         advert_int 1
         authentication {
             auth_type PASS
             auth_pass 1111
         }
         virtual_ipaddress {
             172.18.0.100/16  //这ip调度 192.168.234.47/57
         }
     }
    vrrp_instance VI_2 {
         state MASTER
         interface ens33
         virtual_router_id 51
         priority 100
         advert_int 1
         authentication {
             auth_type PASS
             auth_pass 2222
         }
         virtual_ipaddress {
             172.18.0.200/16  //这ip调度 192.168.234.147/157
         }
    }
    

    如此双主模型轻便的就搭建好了

    # ip addr add dev eth0 192.168.10.2/24

    3.配置nginx主机234.27/37

    先配置http语块

    http {
        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  /var/log/nginx/access.log  main;
        upstream web1{  //
            server 192.168.234.47:80;
            server 192.168.234.57:80;
            }
        upstream web2{
            server 192.168.234.67:80;
            server 192.168.234.77:80;
            }
    
    /*
    ngx_http_upstream_module
    ngx_http_upstream_module模块
    用于将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令
    进行引用
    1、upstream name { ... }
    定义后端服务器组,会引入一个新的上下文
    默认调度算法是wrr
    Context: http
    upstream httpdsrvs {
    server ...
    server...
    ...
    */
    

    然后配置server

        server {
            listen       80 default_server; //默认监听80端口
            server_name www.a.com //域名
            listen       [::]:80 default_server;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
                    proxy_pass http://web1 ;  //定义访问80端口的请求,以web1提供服务。而指定的web1在http语块中为 192.168.234.47/57:80 提供服务
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
        server {
            server_name www.b.com
            listen 80;
            location / {
                    proxy_pass http://web2 ; //定义访问80端口的请求,以web2提供服务。而指定的web2在http语块中为 192.168.234.147/157:80 提供服务
    
            }
        }
    }
    

    这样访问 www.a.com固然访谈192.168.234.47/57:80

    访问 www.b.com就是访谈192.168.234.67/77:80

    现行反革命顾客机将host增添www.a/b.com

    172.18.0.100 www.a.com
    172.18.0.200
    www.b.com

        顾客端将www.a.com 解析 172.18.0.100

    [root@234c17 ~]# ping www.a.com
    PING www.a.com (172.18.0.100) 56(84) bytes of data.
    64 bytes from www.a.com (172.18.0.100): icmp_seq=1 ttl=64 time=0.358 ms
    64 bytes from www.a.com (172.18.0.100): icmp_seq=2 ttl=64 time=0.376 ms
    64 bytes from www.a.com (172.18.0.100): icmp_seq=3 ttl=64 time=0.358 ms
    64 bytes from www.a.com (172.18.0.100): icmp_seq=4 ttl=64 time=0.366 ms
    

        客商端将www.b.com 解析 172.18.0.200

    [root@234c17 ~]# ping www.b.com
    PING www.b.com (172.18.0.200) 56(84) bytes of data.
    64 bytes from www.b.com (172.18.0.200): icmp_seq=1 ttl=64 time=0.582 ms
    64 bytes from www.b.com (172.18.0.200): icmp_seq=2 ttl=64 time=0.339 ms
    64 bytes from www.b.com (172.18.0.200): icmp_seq=3 ttl=64 time=0.524 ms
    64 bytes from www.b.com (172.18.0.200): icmp_seq=4 ttl=64 time=0.337 ms
    

    结果:

      1 [root@234c17 ~]# for i in {1..4};do curl www.a.com;curl www.b.com;sleep 1;done
      2 234.57
      3 234.77
      4 234.47
      5 234.67
      6 234.57
      7 234.77
      8 234.47
      9 234.67
    

    2.配置B主机的IP

    兑现双主模型的ngnix高可用(二卡塔尔(قطر‎

    图片 3

    # ip addr add dev eth0 192.168.10.23/24

    现行反革命扩张实验

    3.配置C主机的IP

    将192.168.234.47/57主机加ip地址

    [root@234c47 ~]#ip a a dev ens37 192.168.167/24
    
    [root@234c57 ~]#ip a a dev ens37 192.168.177/24
    

    # ip addr add dev eth0 192.168.10.3/24

    编纂http的的布局文件扩张基于FQDN设想主机

    [root@234c47 ~]# vim /etc/httpd/conf.d/vhost.conf
    
    <virtualhost 192.168.234.167:80>
     documentroot /data/web1
     servername www.a.com
    < directory /data/web1>
     require all granted
    < /directory>
    < /virtualhost>
    

    4.配置D主机的IP

    另贰个主机也增进设想主机

    [root@234c57 ~]# vim /etc/httpd/conf.d/vhost.conf
    
    <virtualhost 192.168.234.177:80>
    documentroot /data/web1
    servername www.a.com
    <directory /data/web1>
    require all granted
    < /directory>
    < /virtualhost>
    

    # ip addr add dev eth0 192.168.10.33/24

    重启httpd服务

    结果:访问www.a.com

      1 [root@234c17 ~]# for i in {1..8};do curl www.a.com;done
      2 234.167
      3 234.177
      4 234.47
      5 234.57
      6 234.167
      7 234.167
      8 234.177
      9 234.47
     10 
    

    访问www.b.com

      1 [root@234c17 ~]# for i in {1..8};do curl www.b.com;done
      2 234.67
      3 234.67
      4 234.77
      5 234.67
      6 234.77
      7 234.67
      8 234.77
      9 234.77
    

    二、配置web服务(C和D主机都做同样配备,只需更正暗中认可主页中的IP地址为本机的IP就能够,以示不一致卡塔尔国

    1.安装apache

    # yum -y install apache

    2.创设暗许主页

    # vim /var/www/html/index.html

    <h1>192.168.10.3</h1>

    3.启动apache

    # service httpd start

    三、配置sorry_server(此服务配置于Nginx proxy主机上,两台Nginx proxy都做同样配备,只需校订私下认可主页中的IP地址为本机的IP就可以,以示差异卡塔尔

    1.安装apache

    # yum -y install apache

    2.创建默许主页

    # vim /var/www/html/index.html

    <h1>sorry_server:192.168.10.2</h1>

    3.改善监听端口为8080,避防与nginx所监听的端口冲突

    # vim /etc/httpd/conf/httpd.conf

    Listen 8080

    4.启动apache服务

    四、配置代理(两台Nginx proxy都做相似配备卡塔尔(قطر‎

    1.安装nginx

    # yum -y install nginx

    2.定义upstream集群组,在http{}段中定义;

    # vim /etc/nginx/nginx.conf

            http {

                upstream websrvs {

                    server 192.168.10.3:80;

                    server 192.168.10.33:80;

                    server 127.0.0.1:8080 backup;

                }

            }

    3.调用定义的集群组,在server{}段的location{}段中调用;

    # vim /etc/nginx/conf.d/default.conf

            server {

                location / {

                    proxy_pass http://wersrvs;

                    index index.html;

                }

            }

    4.起动服务

    # service nginx start

    五、配置keepalived

    A主机上操作

    1.安装keepalived

    # yum -y install keepalived

    2.编辑A主机的配备文件/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 CentOS6

        vrrp_mcast_group4 224.0.100.39

        }

        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 ngx {

            state MASTER

            interface eth1

            virtual_router_id 14

            priority 100

            advert_int 1

            authentication {

                auth_type PASS

                auth_pass MDQ41fTp

            }

            virtual_ipaddress {

                192.168.20.100/24 dev eth1

            }

            track_script {

                chk_down

                chk_nginx

            }

        }

        vrrp_instance ngx2 {

            state BACKUP

            interface eth1

            virtual_router_id 15

            priority 98

            advert_int 1

            authentication {

                auth_type PASS

                auth_pass XYZ41fTp

            }

            virtual_ipaddress {

                192.168.20.200/24 dev eth1

            }

            track_script {

                chk_down

            chk_nginx

            }

        }

    B主机也作相近配备,稍作修正就能够,供给修正的地点如下:

        vrrp_instance ngx {

            state BACKUP

            priority 98

        }

        vrrp_instance ngx2 {

            state MASTER

            priority 100

        }

    六、模拟故障,验证结果

    1.起步两台Nginx proxy的keepalived服务

    # service keepalived start

    2.做客192.168.20.100,结果应是后端的web server轮询响应诉求

    图片 4

    3.探访192.168.20.200,结果应是后端的web server轮询响应央求

    图片 5

    4.将后端的web server关闭意气风发台,访谈192.168.20.100或192.168.20.200,响应央浼的将只是另风流洒脱台符合规律运作web server的主机

    图片 6

    5.将后端的web server都关闭,当时访谈192.168.20.100或192.168.20.200,响应诉求的将只是Nginx proxy中定义的主server中的sorry_server

    图片 7

    6.关闭生龙活虎台Nginx proxy 的nginx服务,备server将把IP地址增多到本机,继续提供劳务,当时拜会192.168.20.100或192.168.20.200并不会有任何察觉

    图片 8

    有的关于Keepalived相关学科集合

    CentOS 7下Keepalived + HAProxy 搭建配置精解  http://www.linuxidc.com/Linux/2017-03/141593.htm

    Keepalived高可用集群应用处景与安顿 http://www.linuxidc.com/Linux/2017-03/141866.htm

    Nginx+Keepalived完毕站点高可用  http://www.linuxidc.com/Linux/2016-12/137883.htm

    Nginx+Keepalived实现站点高可用(负载均衡卡塔尔(英语:State of Qatar)  http://www.linuxidc.com/Linux/2016-12/138221.htm

    构建高可用集群Keepalived+Haproxy负载均衡 http://www.linuxidc.com/Linux/2016-12/138917.htm

    CentOS6.5下 Keepalived高可用服务单实例配置 http://www.linuxidc.com/Linux/2016-12/138110.htm

    Keepalived安装与结构 http://www.linuxidc.com/Linux/2017-02/140421.htm

    Linux下Keepalived服务安装文书档案  http://www.linuxidc.com/Linux/2017-03/141441.htm

    正文永世更新链接地址:http://www.linuxidc.com/Linux/2017-05/143738.htm

    图片 9

    本文由澳门新葡8455最新网站发布于澳门新葡8455最新网站,转载请注明出处:落到实处双主模型的nginx的高可用,Keepalived达成

    关键词: