一、Keepalived简介

  • Keepalived是HA cluster的一种实现方案,其以vrrp协议为实现基础,在N台具有相同功能的路由器或服务器中选出一个master或多个backup,这些路由器或服务器会共用一个对外提供服务的VIP。在此HA cluster集群中,master主机会发送组播来跟backup主机进行通信确认双方是否存活。当backup主机在指定的时间内没有收到vrrp组播报文则认为master主机宕机了,此时就会在所有的backup主机中根据vrrp协议来选举出新的master主机来保证服务的高可用性。

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

二、Keepalived安装

1、yum安装

[root@localhost ~]# yum install -y keepalived    //安装keepalived

2、源码包安装

1)、可从官网下载keepalived的最新版本,下载网址:https://www.keepalived.org/download.html,进行解压、编译及安装。

[root@localhost ~]# yum install -y wget gcc openssl-devel libnl-devel net-snmp-devel    //安装依赖包
[root@localhost ~]# cd /data/software/
[root@localhost software]# wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
[root@localhost software]# tar -zxvf keepalived-2.0.19.tar.gz
[root@localhost software]# cd keepalived-2.0.19/
[root@localhost software]# ./configure --prefix=/usr/local/keepalived
[root@localhost software]# make && make install
[root@localhost software]# /usr/local/keepalived/sbin/keepalived -v   //查看安装版本

2)、keepalived初始化

# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 
 
# 将keepalived主程序加入到环境变量(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
 
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
[root@localhost /]# cp /data/software/keepalived-2.0.19/keepalived/etc/init.d/keepalived  /etc/init.d/keepalived
[root@localhost /] cd /etc/init.d/
[root@localhost init.d] chkconfig –add keepalived   //添加到系统服务
[root@localhost init.d] chkconfig keepalived on   //添加keepalived到开机启动,或者使用命令systemctl enable keepalived

# 将配置文件放到默认路径下
[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

3、keepalived相关命令

[root@localhost ~]# systemctl start keepalived   //启动keepalived
[root@localhost ~]# systemctl enable keepalived  //加入开机启动keepalived
[root@localhost ~]# systemctl restart keepalived  //重新启动keepalived
[root@localhost ~]# systemctl stop keepalived  //停止keepalived
[root@localhost ~]# systemctl status keepalived   //查看keepalived状态
[root@localhost ~]# ps -aux |grep keepalived   //查看keepalived启动情况

三、Keepalived主备节点配置

1、主节点配置

[root@localhost /]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id  CentOS33 #标识本节点的字条串,通常为hostname
}
 
vrrp_instance VI_1 {
    state MASTER         #角色是master
    interface eth0       #vip 绑定网卡端口
    virtual_router_id 51 #让master 和backup在同一个虚拟路由里,id 号必须相同;
    priority 150         #优先级,谁的优先级高谁就是master ;
    advert_int 1         #心跳间隔时间
    authentication {
        auth_type PASS   #认证
        auth_pass 1111   #密码
    }
    virtual_ipaddress {
        172.16.0.50    #虚拟vip
    }
}

2、备节点配置

[root@localhost /]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id  CentOS34 #标识本节点的字条串,通常为hostname
}
 
vrrp_instance VI_1 {
    state BACKUP         #角色是BACKUP
    interface eth0       #vip 绑定网卡端口
    virtual_router_id 51 #让master 和backup在同一个虚拟路由里,id 号必须相同;
    priority 100         #优先级,谁的优先级高谁就是master,比master优化级150低;
    advert_int 1         #心跳间隔时间
    nopreempt            #非抢占式,MASTER从挂掉到恢复,不再将服务抢占过来
    authentication {
        auth_type PASS   #认证
        auth_pass 1111   #密码
    }
    virtual_ipaddress {
        172.16.0.50    #虚拟vip
    }
}

注:nopreempt:禁止抢占服务。默认情况,当MASTER服务挂掉之后,BACKUP自动升级为MASTER并接替它的任务,当MASTER服务恢复后,升级为MASTER的BACKUP服务又自动降为BACKUP,把工作权交给原MASTER。当配置了nopreempt,MASTER从挂掉到恢复,不再将服务抢占过来。

文章作者: 阳光•雨
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阳光•雨
Linux keepalived安装与配置
喜欢就支持一下吧