keepalived 安装与配置

本文详细介绍了如何在Linux环境下安装和配置keepalived,包括单例和双主模式,以及如何实现HTTP服务的高可用。通过设置虚拟路由ID、优先级和认证密码,确保浮动IP在主从节点间的正确漂移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

keepalived安装
1.安装环境说明
yum -y install ntp ntpdate
ntpdate cn.pool.ntp.org
cat /etc/redhat-release
wget https://www.keepalived.org/software/keepalived-2.0.11.tar.gz
2.安装编译
tar -xf keepalived-2.0.11.tar.gz -C /usr/local/src/
mkdir -p /data/keepalived
yum install openssl-devel gcc gcc-c+=
./configure --prefix=/data/keepalived/
make
make install
keepalived的启动过程并不会对配置文件进行语法检查,keepalived的守护进程 照样能被运行起来,在默认状态下,即不指定配置文件的位置 --keepalived 先查找文件/etc/keepalived/keepalived.conf,如果为了省事,可以手动创建这个文件,然后在这个文件里书写规则,来达到控制keepalived 运行的目的。
3.从解压安装包文件中拷贝启动文件
cd /usr/local/src/keepalived-2.0.11/keepalived/etc
cp -R init /data/keepalived/
cp -R init /data/keepalived/etc/
cp -R init.d /data/keepalived/etc/
ll /data/keepalived/etc/
cp /data/keepalived/etc/init.d/keepalived /etc/init.d/
cp /data/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /data/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /data/keepalvied/sbin/keepalived /usr/sbin/
4.启动服务
/etc/init.d/keepalived start
或者 systemctl start keepalived

keepalived高可用单例配置
主节点:192.168.200.154
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb01
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.200/24 dev ens33 label ens33:10
}
}
重启服务:systemctl restart keepalived
ip a |grep 192.168.200.200
出现了浮动ip
从节点:192.168.200.155
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb02
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {

    192.168.200.200/24 dev ens33 label ens33:10
}

}
重启服务:systemctl restart keepalived
在从节点上看不到浮动ip,如果两边都看到了浮动ip,证明发生了脑裂,首先查看防火墙是否关闭,网络是否畅通,其次检查配置文件的正确与否。

================================
keepalived 双实例双主模式配置
节点1:192.168.200.154
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb01
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {

    192.168.200.200/24 dev ens33 label ens33:10
}

}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 56
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {

    192.168.200.210/24 dev ens33 label ens33:11
}

}
重启服务:systemctl restart keepalived
ip a |grep 192.168.200.*
出现192.168.200.200

节点2:192.168.200.155
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lb02
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {

    192.168.200.200/24 dev ens33 label ens33:10
}

}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 56
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {

    192.168.200.210/24 dev ens33 label ens33:11
}

}
重启服务:systemctl restart keepalived
ip a |grep 192.168.200.*
出现192.168.200.210
!!!注意, priority 的优先级要写正确,优先级越大越优先,填写不正确就有可能会一个机器上两个浮动ip,另一个机器上没有浮动ip
在两个节点上会看到各自的ip,当stop掉一个keepalived后,ip会漂移到另一个上去,恢复时,ip又会回到各自的位置,这里是不抢占模式。有的业务为了防止切换来切换去,配置的是抢占模式。
keepalived实现http服务的高可用配置
两个节点,这里在keepalived上安装httpd
yun install httpd -y
在192.168.200.154和192.168.200.155两个机子上分别做如下操作:
echo “This is a test from 192.168.200.154” > /var/www/html/index.html
echo “This is a test from 192.168.200.155” > /var/www/html/index.html
curl 192.168.200.200
This is a test from 192.168.200.154
curl 192.168.200.210
This is a test from 192.168.200.155
模拟节点1关闭keepalived,再次测试
curl 192.168.200.200
This is a test from 192.168.200.155
此时浮动ip已经漂移到155这个机子了
当节点1keepalived恢复正常时,
再次测试curl 192.168.200.200
This is a test from 192.168.200.154
同样的测试方法再测试一下节点2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值