Ubuntu安装keepalived

一、Keepalived是什么鬼东西:

keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。

二、Keepalived 这个鬼东西的工作原理:

keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议

虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级选举一个backup当master。这样的话就可以保证路由器的高可用了。

keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。

以下是keepalived官网上提供的架构图

Ubuntu安装keepalived-LMLPHP

三、环境准备和keepalived的安装

1、通过ip addr list 或者ip addr show 或者是ip a 等命令查询本机ip

Ubuntu安装keepalived-LMLPHP

2、修改网络配置

修改/etc/sysctl.conf配置文件

Ubuntu安装keepalived-LMLPHP

首先找到net.ipv4.ip_forward=1这个配置,并其注释去除,然后在下面插入net.ipv4.ip_nonlocal_bind=1这句配置

Ubuntu安装keepalived-LMLPHP

保存并退出;

查看是否配置成功

Ubuntu安装keepalived-LMLPHP

如果输出不是1而是0就说明可能还没生效,执行sudo sysctl –p使其生效

Ubuntu安装keepalived-LMLPHP

这样网络配置就配置好了

3、安装keepalived和相关包

先安装相关包

Ubuntu安装keepalived-LMLPHP

再安装keepalived

Ubuntu安装keepalived-LMLPHP

输入y继续

Ubuntu安装keepalived-LMLPHP

安装成功

4、配置keepalived.conf

编辑keepalived.conf文件

Ubuntu安装keepalived-LMLPHP

在keepalived.conf中编辑如下内容

global_defs {

    notification_email {

        [email protected]

    }

    notification_email_from [email protected]

    smtp_server smtp.mxhichine.com

    smtp_connect_timeout 30

    router_id server71

}

vrrp_script chk_health {

    script "/opt/keepalived/check_health.sh"

    interval 1

    weight 1

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass puzek

    }

    virtual_ipaddress {

        192.168.100.100

    }

    track_script {

        chk_health

    }

    notify_master "/opt/keepalived/notify.sh master"

    notify_backup "/opt/keepalived/notify.sh backup"

    notify_fault "/opt/keepalived/notify.sh fault"

}

这里可以创建一个keepalived_script用户(可以不用),用于执行keepalived的脚本

sudo useradd keepalived_script

然后创建密码

sudo passwd keepalived_script

输入密码和确认密码就创建完成了

然后创建keepalived需要使用shell脚本后,将脚本的用户和用户组都设置成keepalived_script

sudo chown -R keepalived_script:keepalived_script notify.shsudo

然后就可以启动keepalived了

启动keepalived(停止使用stop)

Ubuntu安装keepalived-LMLPHP

查看是否启动成功

Ubuntu安装keepalived-LMLPHP

必须要有三个keepalived的进程,这三个分别是keepalived父进程,vrrp子进程、check子进程;两个子进程都被系统watchlog看管,两个子进程各自负责复杂自己的事;

然后查看虚拟ip是否起来了

Ubuntu安装keepalived-LMLPHP

如果有其他问题可以查看keepalived的日志(keepalived的日志默认打在系统日志中,所以可以使用grep 过滤一下)

cat /var/log/syslog | grep Keepalived

05-19 10:59