Keepalived入门
Keepalived通过VRRP协议(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)实现高可用功能,解决静态路由单点故障问题,保证个别节点宕机时,整个网络能不间断地运行。
1 安装和基本操作
1.1 准备
1、在VMWare上安装带有用户界面的CentOS7系统,下载地址:
https://www.centos.org/download/
版本:CentOS-7-x86_64-Everything-1804.iso
2、Keepalived安装包,下载地址:
http://www.keepalived.org/download.html
版本:keepalived-2.0.8.tar.gz
本文安装两个CentOS虚拟机,分别为keepalived1(IP:192.168.197.146)、keepalived2(IP:192.168.197.147),虚拟IP为192.168.197.148。
1.2 安装
安装Keepalived,将Keepalived安装包上传至Linux服务器,解压:
[root@keepalived1 ~]# tar -zxvf keepalived-2.0.8.tar.gz
[root@keepalived1 ~]# cd keepalived-2.0.8
检查配置:
[root@keepalived1 keepalived-2.0.8]# ./configure --prefix=/usr/local/keepalived
其中,/usr/local/keepalived为安装路径。
检查过程中可能发生报错:
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
该报错由于缺少openssl-devel包导致,安装可解决:
yum install openssl-devel
检查通过后,编译并安装:
[root@keepalived1 keepalived-2.0.8]# make
[root@keepalived1 keepalived-2.0.8]# make install
1.3 配置
安装完成后,在安装目录下存在etc文件夹,其中已经预存放着各种应用场景下的示例配置文件。
本文首先配置以最简单的配置文件,后文将通过逐渐增加配置,演示配置代表的功能,最终完成一份实用的Keepalived配置。
keepalived1服务器:
! Configuration File for keepalived
global_defs {
# 标识本节点的名称,用以告警时进行区分
router_id SERVER_146
}
vrrp_instance VI_1 {
# 初始状态,有MASTER和BACKUP两种状态,需全部大写,其中MASTER为工作状态,BACKUP为备用状态
state MASTER
# 对外提供服务的网卡接口,即虚拟IP绑定的网卡接口,在选择网卡接口时,要核实清楚,可通过ifconfig指令查看本机的网卡情况
interface ens32
# 虚拟路由的ID号,每组中各个节点设置必须一样,可选择IP最后一段使用,相同的 VRID 为一个组,他将决定多播的 MAC 地址
virtual_router_id 148
# 节点优先级,取值范围0~254,MASTER要比BACKUP高
priority 100
# MASTER与BACKUP节点间同步检查的时间间隔,单位为秒
advert_int 1
# 虚拟IP地址池,可以有多个IP,每个IP占一行,不需要指定子网掩码
virtual_ipaddress {
192.168.197.148
}
}
keepalived2服务器:
! Configuration File for keepalived
global_defs {
router_id SERVER_147
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 148
priority 90
advert_int 1
virtual_ipaddress {
192.168.197.148
}
}
1.4 启动停止
进入安装目录的sbin文件夹,直接运行keepalived启动:
[root@keepalived1 sbin]# keepalived