侧边栏壁纸
博主头像
阿里灰太狼博主等级

You have to believe in yourself . That's the secret of success.

  • 累计撰写 104 篇文章
  • 累计创建 50 个标签
  • 累计收到 12 条评论

目 录CONTENT

文章目录

Keepalived 安装部署

阿里灰太狼
2022-02-09 / 0 评论 / 4 点赞 / 606 阅读 / 3,091 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-02-11,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、下载地址:

Keepalived for Linux

2、通过 ftp 工具上传到 linux 中, /home/software

3、解压

tar -zxvf keepalived-2.0.18.tar.gz

4、解压后进入到解压出来的目录,看到会有 configure,那么就可以做配置了(配置安装和nginx一模一样)

5、使用 configure 命令配置安装目录与核心配置文件所在位置

./configure --prefix=/usr/local/keepalived --sysconf=/etc

· prefix: keepalived 安装的位置

· syscon: keepalived 核心配置文件所在位置,固定位置,改成其他位置则 keepalived启动不了, /var/log/messages 中会报错

5.1、配置过程中可能会出现警告信息,如下所示

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

5.2、安装 libnl/libnl-3 依赖

yum -y install libnl libnl-devel  

5.3、重新 configure 一下,此时 oK。

6、安装 keepalived

make && make install

7、进入到 /etc/keepalived, 该目录下为 keepalived 核心配置文件

keepalived0

如果忘记安装配置的目录,则通过如下命令找到:

keepalived1

8、配置 Keepalived-主

8.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
    }
}

附:查看网卡名称

keepalived2

8.2、启动 Keepalived

在 sbin 目录中进行启动(同 nginx),如下图

keepalived3

8.3、查看进程

ps -ef|grep keepalived

8.4、查看 vip

在网卡 ens33 下,多了一个 192.1681.161,这个就是虚拟 ip

keepalived4

9、配置 Keepalived-备

9.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
    }
}

9.2、启动 Keepalived

#启动 keepalived

systemctl start keepalived

#停止 keepalived

systemctl stop keepalived

#重启 keepalived

systemctl restart keepalived

9.3、查看进程

ps -ef|grep keepalived

10、Keepalived 配置 Nginx 自动重启

10.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

10.2、配置 keepalived 监听 nginx 脚本

vrrp_script check_nginx_alive {
    script "/etc/keepalived/check_nginx_alive_or_not.sh"
    interval 2 # 每隔两秒运行上一行脚本
    weight 10 # 如果脚本运行成功,则升级权重 +10

#weight -10 # 如果脚本运行失败,则升级权重 -10

}

10.3、在 vrrp_instance 中新增监控的脚本

track_script {
    check_nginx_alive   # 追踪 nginx 脚本
}

10.4、重启 Keepalived 使得配置文件生效

systemctl restart keepalived

11、配置 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
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.162
    }
}

别忘记分别重启 Keepalived

#重启 Keepalived

systemctl restart keepalived
4

评论区