Keepalived

Keepalived安装部署

  1. 下载地址:
    点我下载
  2. 通过ftp工具上传到linux中,/home/software
    Keepalived_第1张图片
  3. 解压
    tar -zxvf keepalived-2.0.18.tar.gz
  4. 解压后进入到解压出来的目录,看到会有 configure ,那么就可以做配置了(配置安装和nginx一模一样)

配置 Keepalived - 主

  1. 通过命令 vim keepalived.conf 打开配置文件

    global_defs {
    # 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
    router_id keep_171
    }
    vrrp_instance VI_1 {
    # 表示状态是MASTER主机还是备用机BACKUP
    state MASTER
    # 该实例绑定的网卡
    interface ens33
    # 保证主备节点一致即可
    virtual_router_id 51
    # 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
    priority 100
    # 主备之间同步检查时间间隔,单位秒
    advert_int 2
    # 认证权限密码,防止非法节点进入
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    # 虚拟出来的ip,可以有多个(vip)
    virtual_ipaddress {
    192.168.1.161
    }
    }

    附:查看网卡名称
    Keepalived_第2张图片

  2. 启动 Keepalived
    在sbin目录中进行启动(同nginx),如下图:
    Keepalived_第3张图片

配置 Keepalived - 备

  1. 通过命令 vim keepalived.conf 打开配置文件

    global_defs {
    router_id keep_172
    }
    vrrp_instance VI_1 {
    # 备用机设置为BACKUP
    state BACKUP
    interface ens33
    virtual_router_id 51
    # 权重低于MASTER
    priority 80
    advert_int 2
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    # 注意:主备两台的vip都是一样的,绑定到同一个vip
    192.168.1.161
    }
    }
  2. 启动 Keepalived

    # 启动keepalived
    systemctl start keepalived
    # 停止keepalived
    systemctl stop keepalived
    # 重启keepalived
    systemctl restart keepalived
  3. 查看进程

    ps -ef|grep keepalived

    image.png


Keepalived配置Nginx自动重启

  1. 增加Nginx重启检测脚本

    vim /etc/keepalived/check_nginx_alive_or_not.sh
    #!/bin/bash
    A=`ps -C nginx --no-header |wc -l`
    # 判断nginx是否宕机,如果宕机了,尝试重启
    if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    # 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
    sleep 3
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
    fi
    fi

    增加运行权限

    chmod +x /etc/keepalived/check_nginx_alive_or_not.sh
  2. 配置keepalived监听nginx脚本

    vrrp_script check_nginx_alive {
    script "/etc/keepalived/check_nginx_alive_or_not.sh"
    interval 2 # 每隔两秒运行上一行脚本
    weight 10 # 如果脚本运行失败,则升级权重+10
    }
  3. 在 vrrp_instance 中新增监控的脚本

    track_script {
    check_nginx_alive # 追踪 nginx 脚本
    }
  4. 重启Keepalived使得配置文件生效

    systemctl restart keepalived

配置Keepalived双主热备
规则:以一个虚拟ip分组归为同一个路由
主节点配置:

global_defs {
router_id keep_171
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.161
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.162
}
}

备用节点配置

global_defs {
router_id keep_172
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.161
}

你可能感兴趣的:(keepalived)