CentOS Keepalived配置指南
简介

Keepalived是一个高度可靠的集群管理工具,它可以在多个服务器之间实现负载均衡和高可用性,在CentOS系统中配置Keepalived,可以帮助您实现故障转移和负载均衡,确保服务的连续性和稳定性。
环境准备
在开始配置Keepalived之前,请确保以下环境已经准备就绪:
- 两台CentOS服务器,分别命名为Server1和Server2。
- 服务器之间可以互相通信,且IP地址已配置。
- 服务器上已安装Keepalived软件包。
安装Keepalived
在Server1和Server2上分别执行以下命令安装Keepalived:
sudo yum install keepalived -y
安装完成后,可以使用以下命令查看Keepalived版本:
keepalived --version
配置Keepalived

修改Server1的Keepalived配置文件:
sudo vi /etc/keepalived/keepalived.conf
添加到配置文件中:
! Configuration File for keepalived
global_defs {
notification_email {
admin@example.com
}
notification_email_subject "Keepalived Alert: $ernes"
notification_email_from "Keepalived <admin@example.com>"
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.100/24 dev eth0 label eth0:0
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_method roundrobin
persistence_timeout 50
protocol HTTP
real_server 192.168.1.101 80 {
weight 1
HTTP_GET {
url /index.html
status_code 200
Negotiate
check
}
}
real_server 192.168.1.102 80 {
weight 1
HTTP_GET {
url /index.html
status_code 200
Negotiate
check
}
}
} 修改Server2的Keepalived配置文件:
sudo vi /etc/keepalived/keepalived.conf
添加到配置文件中:
! Configuration File for keepalived
global_defs {
notification_email {
admin@example.com
}
notification_email_subject "Keepalived Alert: $ernes"
notification_email_from "Keepalived <admin@example.com>"
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.100/24 dev eth0 label eth0:0
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_method roundrobin
persistence_timeout 50
protocol HTTP
real_server 192.168.1.101 80 {
weight 1
HTTP_GET {
url /index.html
status_code 200
Negotiate
check
}
}
real_server 192.168.1.102 80 {
weight 1
HTTP_GET {
url /index.html
status_code 200
Negotiate
check
}
}
} 启动和查看Keepalived服务
在Server1和Server2上分别执行以下命令启动Keepalived服务:
sudo systemctl start keepalived
使用以下命令查看Keepalived服务状态:

sudo systemctl status keepalived
相关问答FAQs
Q1:如何查看Keepalived的虚拟IP地址?
A1:在Server1和Server2上分别执行以下命令查看虚拟IP地址:
sudo ip addr show eth0:0
Q2:如何修改Keepalived的优先级?
A2:在Keepalived配置文件中,priority参数用于设置优先级,将Server1的优先级设置为100,Server2的优先级设置为90,可以通过修改以下行实现:
priority 100 priority 90

