系统 CentOs7
版本 keeplived 1.4.5
官网下载
keeplived官网下载地址
#解压
tar -zxvf keepalived-1.4.5.tar.gz
#进入目录
cd keepalived-1.4.5
#编译前配置
./configure
#编译并安装
make && make install
此时在keepalived-1.4.5会生成安装后的文件
该目录下有一些配置文件要移动到linux的对应位置
cp keepalived-1.4.5/keepalived /usr/sbin/
cp keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/
cp keepalived-1.4.5/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#创建放配置文件的文件夹
mkdir -p /etc/keepalived
cp keepalived-1.4.5/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
#添加可执行权限
chmod +x /etc/init.d/keepalived
#添加keepalived到开机启动
chkconfig --add keepalived
chkconfig keepalived on
配置
修改/etc/keepalived/keepalived.conf
此处贴上我的简单配置:
功能:
一共两台机器上安装keeplived,一主一备
访问虚拟IP 192.168.23.250
映射到master IP 192.168.23.18
master挂了则映射到backup IP 192.168.23.17
192.168.23.18上的配置:
global_defs {
}
vrrp_instance VI_1 {
#主节点定义
state MASTER
#网卡名称 linux ip addr 查看自己的网卡名称
interface ens33
#虚拟路由Id 两台要一致
virtual_router_id 51
#权重,根据访问成功失败会自行修改
priority 100
advert_int 2
#验证方式,两台一致
authentication {
auth_type PASS
auth_pass 1111
}
#虚拟IP,两台一致
virtual_ipaddress {
192.168.23.250/24
}
}
192.168.23.17上的配置:
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.23.250/24
}
}
服务启动
systemctl start keeplived.service
访问1192.168.23.250 会映射到 192.168.23.18
如果访问失败会继续访问 192.168.23.17
谁是主谁是备在运行中因为访问失败会切换