keepalived 的安装和配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived 的安装和配置相关的知识,希望对你有一定的参考价值。

安装前准备:
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

一、下载keepalived
方法一:
yum -y install keepalived
方法二:
下载二进制源码包:http://www.keepalived.org/download.html
#下载安装文件
wget http://www.keepalived.org/software/keepalived-1.4.0.tar.gz -C /user/locl

        #解压文件
        tar -zxvf keepalived-1.40.0.tar.gz

        #编译
        cd keepalived-1.40.0/
        #--prefix 指定安装地址
        #/usr/local/keepalived/ 安装的目录,不要和自己安装文件一个目录,不然报错
        ./configure --prefix=/usr/local/keepalived/

        #编译并安装
        make && make install

二、编辑/etc/keepalived/keepalived.conf

yum安装编辑此文件
    vim /ect/keepalived/keepalived.conf
源码包安装编辑此文件
    vim /usr/local/keepalived/etc/keepalived/keepalived.conf 
global_defs {
    notification_email {
        #[email protected]       # 指定keepalived在发生切换时需要发送email到的对象,一行一个
        #[email protected]
    }
    notification_email_from [email protected]   # 指定发件人
    smtp_server [email protected]              # smtp 服务器地址
    smtp_connect_timeout 30               # smtp 服务器连接超时时间
    router_id LVS_1 # 标识本节点的字符串,通常为hostname,但不一定非得是hostname,故障发生时,邮件通知会用到
    }

vrrp_instance VI_1 {  # 实例名称
    state MASTER      # 可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER
    interface eth0    # 节点固有IP(非VIP)的网卡,用来发VRRP包做心跳检测
    mcast_src_ip 192.168.0.179 #本机的ip,需要修改
    virtual_router_id 51 # 虚拟路由ID,取值在0-255之间,用来区分多个instance的VRRP组播,同一网段内ID不能重复;主备必须为一样;
    priority 100      # 用来选举master的,要成为master那么这个选项的值最好高于其他机器50个点,该项取值范围是1-255(在此范围之外会被识别成默认值100)
    advert_int 1      # 检查间隔默认为1秒,即1秒进行一次master选举(可以认为是健康查检时间间隔)
    authentication {  # 认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)
        auth_type PASS  # 默认是PASS认证
        auth_pass MrUse # PASS认证密码
    }
    virtual_ipaddress {
        192.168.0.219    # 虚拟VIP地址,允许多个
    }
}

三、开启服务
(1)systemctl start keepalived(在yum安装时直接使用此命令启动服务)
(2)源码包安装启动服务还需进行如下步骤
#创建keepalived配置文件目录
mkdir /etc/keepalived

    #拷贝配置文件到/etc/keepalived目录下
    cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

    #复制keepalived脚本到/etc/init.d/ 目录
    cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ #(可忽略) 
    #拷贝keepalived脚本到/etc/sysconfig/ 目录
    cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

    #建立软连接
    ln -s /usr/local/keepalived/sbin/keepalived /sbin/

    #添加到开机启动
    chkconfig keepalived on
    #查看服务状况
    systemctl status keepalived

四、启用ip转发

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
    sysctl -p

五、防火墙添加规则

firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface enp196 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

success
# firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 \--out-interface enp196 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
success
# firewall-cmd --reload
success

以上是关于keepalived 的安装和配置的主要内容,如果未能解决你的问题,请参考以下文章

keepalived 的安装和配置

Linux下 keepalived 的安装和配置

Keepalived详解

Keepalived 的安装和配置

Keepalived 的安装和配置

CentOS8安装keepalived和lvs遇到的坑