Appearance
Hello Keepalived
Keepalived 是基于 VRRP (Virtual Router Redundancy Protocol) 协议实现的一个高可用性解决方案,主要用于确保服务的连续性。它能够监控服务器的健康状态,并在发现故障时自动进行故障转移。
主要特点:
- 高可用性:通过 VRRP 协议实现服务器之间的自动故障转移
- 健康检查:支持多种方式检查服务状态
- 负载均衡:提供了基于 LVS (Linux Virtual Server) 的负载均衡功能
1. 安装 Keepalived
RHEL
bashsudo yum install keepalivedDebian
bashsudo apt-get install keepalived
2. Keepalived 配置
2.1. 配置介绍
Keepalived 的主配置文件位于 /etc/keepalived/keepalived.conf。以下是一个基本的配置示例:
Nginx
global_defs {
router_id LVS_DEVEL
enable_script_security
notification_email {
admin@example.com
}
notification_email_from keepalived@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}配置说明:
global_defs:全局配置部分vrrp_instance:定义 VRRP 实例state:初始状态(MASTER 或 BACKUP)interface:监听的网络接口virtual_router_id:虚拟路由器 ID,范围 0~255priority:优先级,越高越优先成为 MASTERadvert_int:VRRP 通告间隔时间(秒)authentication:认证配置virtual_ipaddress:虚拟 IP 地址列表
通过以下命令检查 VRRP 状态:
bash
ip addr show可以看到伪装的 IP 地址 192.168.1.100。
2.2. 高可用配置
主服务器配置
Nginxvrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }1
2
3
4
5
6
7
8
9
10
11
12
13
14备份服务器配置
Nginxvrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
2.3. 健康检查配置
Keepalived 支持多种健康检查方式,包括:
脚本检查
Nginxvrrp_script check_nginx { script "/usr/local/bin/check_nginx.sh" interval 2 weight -10 } vrrp_instance VI_1 { ... track_script { check_nginx } ... }1
2
3
4
5
6
7
8
9
10
11
12
13HTTP 检查
Nginxvrrp_script check_http { script "curl -f http://localhost/ || exit 1" interval 2 weight -10 }1
2
3
4
5检查 TCP 连接
Nginxvrrp_script check_tcp { script "echo > /dev/tcp/127.0.0.1/80 || exit 1" interval 2 weight -10 }1
2
3
4
5
2.4. 负载均衡配置
结合 LVS 实现负载均衡:
Nginx
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.101 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.102 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}2.5. 通知配置
配置邮件通知:
Nginx
global_defs {
notification_email {
admin@example.com
}
notification_email_from keepalived@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
}3. 高级配置
3.1. 多个 VRRP 实例
Nginx
vrrp_instance VI_1 {
...
}
vrrp_instance VI_2 {
...
}3.2. 多个虚拟 IP
Nginx
vrrp_instance VI_1 {
...
virtual_ipaddress {
192.168.1.100
192.168.1.101
192.168.1.102
}
...
}4. 实际应用场景
4.1. Nginx 高可用
Nginx
vrrp_script check_nginx {
script "pidof nginx || exit 1"
interval 2
weight -10
}
vrrp_instance VI_1 {
...
track_script {
check_nginx
}
...
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
4.2. MySQL 高可用
Nginx
vrrp_script check_mysql {
script "mysqladmin ping -h 127.0.0.1 -u root -p 'password' || exit 1"
interval 2
weight -10
}
vrrp_instance VI_1 {
...
track_script {
check_mysql
}
...
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13