• keepalived


    1.场景描述

    因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用,如下图:

    安装了keepavlived,走了一些弯路,记录下吧,nginx的安装就不多说了,博客已经介绍了好几篇了。

    2. 解决方案

    2.1 安装keepalived

    2.1.1 安装相关依赖包
    [root@t-ruanjianlaowang ~]# yum install -y libnl*
    [root@t-ruanjianlaowang ~]#  yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh
    
    • 1
    • 2

    假如连不了,网上也有这些包的下载,下载后tar解压也行。

    2.1.2 下载并安装keepalived

    官网地址:https://www.keepalived.org/download.html

    我们使用的最新版本:keepalived-2.0.18.tar.gz,993k

    tar -zxvf keepalived-2.0.18.tar.gz
    cd keepalived-2.0.18/
    ./configure --prefix=/usr/local/keepalived
    make
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.1.3 添加keepavlived为系统服务
    [root@t-ruanjianlaowang ~]#  cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 
    
    [root@t-ruanjianlaowang ~]#  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
    
    [root@t-ruanjianlaowang ~]# cp /root/tools/keepalived-2.0.18/keepalived/etc/init.d/keepalived   /etc/init.d/keepalived
    
    #/etc/keepalived下好像有了keepalived文件夹,有了就不用mkdir了,软件老王
    [root@t-ruanjianlaowang ~]# mkdir /etc/keepalived  
    [root@t-ruanjianlaowang ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
    
    2.1.4 设置开机启动
    # 设置开机启动
    [root@t-ruanjianlaowang ~]# chkconfig keepalived on
    
    [root@t-ruanjianlaowang ~]# service keepalived start|stop|restart
    
    • 1
    • 2
    • 3
    • 4

    到这里仅仅是keepalived安装完成,接下来还要根据项目项目配置keepalived.conf信息。

    如果你觉得文章对你有些帮助,欢迎微信搜索「软件老王」第一时间阅读或交流!
    
    • 1

    2.2 配置文件修改

    2.2.1 master的conf配置文件
    vi cd /etc/keepalived/keepalived.conf
    
    • 1
    !Configuration File for keepalived
    global_defs {
       router_id RUANJIANLAOWANG_MASTER
    }
    
    vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    weight -20
    }
    
    vrrp_instance VI_1 {
    state MASTER
    interface ens666    #这里必须非常注意,软件老王刚开始没太注意,这里是你的网卡名称
    virtual_router_id 81  #这个会在backup机器上用到
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    
    track_script {
    chk_nginx
    }
    virtual_ipaddress {
    10.192.168.21
    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    2.2.2 还有个检查nginx是否挂掉的sh,也上传下
    vi nginx_check.sh 
    
    • 1
    #!/bin/bash
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /usr/sbin/nginx
        sleep 2
        counter=$(ps -C nginx --no-heading|wc -l)
        if [ "${counter}" = "0" ]; then
            /etc/init.d/keepalived stop
        fi
    fi
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    2.2.3 backup机器上conf文件
    /bin/bash: Configuration: command not found
    bal_defs {
     router_id RUANJIANLAOWANG2
    }
    vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
    interval 2 ## 检测时间间隔
    weight -20 ## 如果条件成立,权重-20
    }
    
    vrrp_instance VI_1 {
    state BACKUP
    interface ens168
    virtual_router_id 81
    
    priority 90
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 111track_script {
               chk_nginx ## 执行 Nginx 监控的服务,软件老王
     }
    virtual_ipaddress {
    10.192.168.21
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    这里记着也把检查nginx的sh文件上传上,跟master上的一样。

    2.3 启动

    (1)配置完成后开始启动

    [root@t-ruanjianlaowang ~]# service keepalived start
    
    • 1

    (2)启动过程中,打开日志看一下(刚开始可能会有问题,看下日志啥都明白了)

    [root@t-ruanjianlaowang ~]# tail -f  /var/log/messages
    
    • 1

    (3)假如启动不了,可以尝试以下方式。

    [root@t-ruanjianlaowang ~]# systemctl daemon-reload
    
    #上面试过后,不行再重新注册服务
    [root@t-ruanjianlaowang ~]#chkconfig --del keepalived
    [root@t-ruanjianlaowang ~]# cd /usr/lib/systemd/system/
    [root@t-ruanjianlaowang ~]#rm -rf keepalived.service
    [root@t-ruanjianlaowang ~]# chkconfig --add keepalived
    
    

    2.4 验证

    [root@t-ruanjianlaowang ~]#ip addr show ens666
    [root@t-ruanjianlaowang ~]#ip addr show ens168
    ens666、ens168为网卡名称
    
    假如绑定成功,软件老王机器里显示的信息里面会有:10.192.168.21的显示

    转载自:https://blog.csdn.net/meng6098/article/details/104961297
  • 相关阅读:
    大规模机器学习
    机器学习之推荐系统
    SVM实现邮件分类
    机器学习之异常检测
    降维算法学习
    手写数字识别实现
    动态规划训练之十七
    概率期望训练之三
    数据结构训练之四
    绵阳东辰国际test201910.25
  • 原文地址:https://www.cnblogs.com/wugh8726254/p/14040401.html
Copyright © 2020-2023  润新知