美文网首页
nginx、keepalive实现负载均衡

nginx、keepalive实现负载均衡

作者: zhaoyanping | 来源:发表于2020-03-20 16:30 被阅读0次

    1、准备

    两台相同配置的web

    功能机器 IP
    MASTER 192.168.1.92
    BACKUP 192.168.1.98

    2、安装

    两台服务器分别安装Nginx和keepalive

    安装依赖包

    yum -y install gcc pcre-devel zlib-devel openssl-devel
    yum -y install popt-devel
    

    下载

    wget http://nginx.org/download/nginx-1.2.4.tar.gz
    wget https://www.keepalived.org/software/keepalived-1.2.7.tar.gz
    

    安装nginx、安装keepalive

    tar zxvf keepalived-1.2.7.tar.gz
    cd keepalived-1.2.7
    ./configure
    make && make install
    
    cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
    cp /usr/local/etc/sysconfig/keepalived  /etc/rc.d/sysconfig/
    mkdir /etc/keepalived
    cp /usr/local/etc/keepalived/keepalived.conf  /etc/keepalived/
    cp /usr/local/sbin/keepalived  /usr/sbin/
    

    启动服务,写入开机启动的脚本里面去

    echo  '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
    echo  '/etc/init.d/keepalived start' >> /etc/rc.local
    

    3、配置

    配置nginx

    两台服务器nginx的配置完全一样,主要配置 /usr/local/nginx/conf/nginx.conf 中的http。其中多个域名指向是通过虚拟主机(配置 server)实现,同一域名的不同虚拟目录通过每个server下面的 localtion 实现,在后端服务器 http下面配置 upstream,然后在server或localtion 下面通过 proxypass 引用。
    http配置如下

    http {
        include mime.types;
        default_type applecation/octet-stream;
    
        sendfile      on;
    
        upstream www.ping.cn {
            server  50.1.1.21:80;
        }
        
        upstream dev.jie.cn {
            ip_hash;
            server 192.168.1.100:80;
            server 192.168.1.101:80;
            server 192.168.1.102:80;
        }
    
        server {
            listen    80;
            server_name    dev.jie.cn;
            location / {
                proxy_pass    http://dev.jie.cn;
            }
        }
    }
    

    验证:
    先用IP访问各个应用服务器的url
    在用域名和路径访问各个应用系统

    配置Keepalived

    从上面安装的情况,keepalived的配置文件在 /etc/keepalived/keepalived.conf。
    主、从服务器的配置相关联但也有不同。如下

    Master:
    ! Configuration File for keepalived
    
    global_defs {
        router_id    NGINX_DEVEL
    }
    
    vrrp_instance VI_1 {
        state    MASTER
        interface    eth0
        virtual_router_id 51
        priority 101
        advert_int 1
        
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.92
        }
    }
    
    Backup:
    ! Configuration File for keepalived
    
    global_defs {
        router_id    NGINX_DEVEL
    }
    
    vrrp_instance VI_1 {
        state    BACKUP
        interface    eth0
        virtual_router_id 51
        priority 99
        advert_int 1
        
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.92
        }
    }
    

    验证:
    在主从服务器上都启动 keepalived: /etc/init.d/keepalived start
    在主服务器上查看是否已经绑定了虚拟IP:ip addr
    停掉主服务器上的keepalived /etc/init.d/keepalived stop
    然后在从服务器上看是否已经绑定了虚拟IP
    再启动主服务器上的 keepalived, 查看主服务器是否重新结果虚拟IP

    让Keepalived监控Nginx的状态

    相关文章

      网友评论

          本文标题:nginx、keepalive实现负载均衡

          本文链接:https://www.haomeiwen.com/subject/qagjyhtx.html