美文网首页
nginx + keepalived

nginx + keepalived

作者: a9104fed92a0 | 来源:发表于2018-09-25 14:35 被阅读0次
        nginx作为反向代理和负载均衡服务器,存在单点问题。一般要部署两台(master + slave)做高可用。master挂掉之后,可通过keepalived自动使用slave来提供服务。keepalived 基于vrrp协议,对外提供一个虚拟ip。
    
       其他负载均衡服务器LVS(4层),Haproxy(4层和7层),都可使用keepalived做高可用。现在以nginx和keepalived, 来介绍高可用负载均衡服务器。
    

    Nginx

    安装必备库

    yum install gcc-c++ 
    yum install pcre pcre-devel  
    yum install zlib zlib-devel 
    yum install openssl openssl--devel
    

    下载指定版本nginx
    wget 'http://nginx.org/download/nginx-1.9.9.tar.gz'
    解压安装

    tar -zxvf nginx-1.9.9.tar.gz
    cd nginx-1.9.9
    ./configure  $默认安装在/usr/local/nginx 
    make 
    make install
    

    查看是否安装成功
    /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    安装成功

    启动nginx
    /usr/local/nginx/sbin/nginx
    重启
    /usr/local/nginx/sbin/nginx -s reload
    设置开机启动
    vi /etc/rc.local
    加入
    /usr/local/nginx/sbin/nginx

    keepalived

    下载

    cd /usr/local/src
    wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
    

    解压安装

    # tar -zxvf keepalived-2.0.7.tar.gz 
    # cd keepalived-2.0.7
    # ./configure --prefix=/usr/local/keepalived
    # make && make install
    

    将 keepalived 安装成 Linux 系统服务

    # mkdir /etc/keepalived
    # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
    # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
    # ln -s /usr/local/sbin/keepalived /usr/sbin/
    # ln -s /usr/local/keepalived/sbin/keepalived /sbin/
    

    启动
    service keepalived start

    设置keepalived开机启动
    `

    chkconfig keepalived on

    修改keepalived配置文件
    Master(192.138.33.10)

    # vi /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
        ## keepalived 自带的邮件提醒需要开启 sendmail 服务。 建议用独立的监控或第三方 SMTP
        router_id for_nginx## 标识本节点的字条串,通常为 hostname
    } 
    ## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。
    vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
        interval 2 ## 检测时间间隔
        weight -20 ## 如果条件成立,权重-20
    }
    ## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
    vrrp_instance VI_1 {
        state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP
        interface eth0 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth0
        virtual_router_id 33 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用, 相同的 VRID 为一个组,他将决定多播的 MAC 地址
        mcast_src_ip 192.168.33.10 ## 本机 IP 地址
        priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高
        nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
        advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s
        ## 设置验证信息,两个节点必须一致
        authentication {
            auth_type PASS
            auth_pass 1111 ## 真实生产,按需求对应该过来
        }
        ## 将 track_script 块加入 instance 配置块
        track_script {
            chk_nginx ## 执行 Nginx 监控的服务
        } #
        # 虚拟 IP 池, 两个节点设置必须一样
        virtual_ipaddress {
            192.168.33.20 ## 虚拟 ip,可以定义多个
        }
    }
    
    

    `

    相关文章

      网友评论

          本文标题:nginx + keepalived

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