美文网首页
Nginx 学习之旅 | Nginx 高可用集群

Nginx 学习之旅 | Nginx 高可用集群

作者: stamSuper | 来源:发表于2020-03-03 17:29 被阅读0次

    keepalived

    Keepalived的作⽤是检测服务器的状态,如果有⼀台web服务器宕机,或⼯作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使⽤其他服务器代
    替该服务器的⼯作,当服务器⼯作正常后Keepalived⾃动将服务器加⼊到服务器群中,这些⼯作全部⾃动完成,不需要⼈⼯⼲涉,需要⼈⼯做的只是修复故障的服务器。

    ⼀个监控+⾃愈的软件

    Keepalived 安装

    image.png

    keepalived 安装在分发器上(nginx + keepalived)

    http://www.keepalived.org/download.html
    wget http://www.keepalived.org/software/keepalived-2.0.8.tar.gz
    
    #cat keepalived_install.sh
    #!/bin/bash
    pkg=keepalived-2.0.8.tar.gz
    tar xf $pkg
    yum -y install kernel-devel
    ln -s /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/ /usr/src/linux
    cd keepalived-2.0.8/
    yum install openssl-* -y
    ./configure --prefix=/usr/local/keepalived
    make
    make install
    mkdir -pv /etc/keepalived
    cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    ln -s /usr/local/keepalived/sbin/keepalived /sbin/
    

    配置Nginx集群

    upstream web {
        server 192.168.10.42 max_fails=2 fail_timeout=3;
        server 192.168.10.43 max_fails=2 fail_timeout=3;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://web;
        }
    }
    

    配置keepalived

    global_defs {
        router_id NGINX_DEVEL
    }
    #  定义一个脚本
    vrrp_script check_nginx {
        #  脚本路径
        script "/etc/keepalived/nginx_pid.sh"
        #  探针  没两秒运行一次脚本
        interval 2
        #  失败次数
        fall 1
    }
    #  定义一个实例(使用的是vrrp协议)
    vrrp_instance nginx {
        #  定义当前nginx 的状态是  master(主)
        state MASTER
        interface ens33
        mcast_src_ip 192.168.10.40
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
            #  调用上面定义的脚本
            check_nginx
        }
        virtual_ipaddress {
            192.168.10.213/24
        }
    }
    

    关联脚本 nginx_pid.sh

    #chmod 755 /etc/keepalived/nginx_pid.sh
    #cat /etc/keepalived/nginx_pid.sh
    #!/bin/bash
    nginx_kp_check () {
        nginxpid=`ps -C nginx --no-header |wc -l`
        if [ $nginxpid -eq 0 ];then
            /usr/local/nginx/sbin/nginx
            sleep 1
            nginxpid=`ps -C nginx --no-header |wc -l`
            if [ $nginxpid -eq 0 ];then
                systemctl stop keepalived
            fi
        fi
    }
    

    启动keepalived

    systemctl start keepalived
    

    相关文章

      网友评论

          本文标题:Nginx 学习之旅 | Nginx 高可用集群

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