4.2. keepalived Direct Routing 配置
Keepalived 的直接路由配置与 NAT 配置类似。在以下示例中,Keepalived 配置为为在端口 80 上运行 HTTP 的一组实际服务器提供负载平衡。要配置直接路由,将
lb_kind 参数改为 DR。其他配置选项在 第 4.1 节 “基本 Keepalived 配置” 中讨论。
以下示例显示了使用直接路由的 Keepalived 配置中的活跃服务器的
keepalived.conf 文件。
global_defs {
notification_email {
admin@example.com
}
notification_email_from noreply_admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 60
}
vrrp_instance RH_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass passw123
}
virtual_ipaddress {
172.31.0.1
}
}
virtual_server 172.31.0.1 80 {
delay_loop 10
lb_algo rr
lb_kind DR
persistence_timeout 9600
protocol TCP
real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
}
global_defs {
notification_email {
admin@example.com
}
notification_email_from noreply_admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 60
}
vrrp_instance RH_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass passw123
}
virtual_ipaddress {
172.31.0.1
}
}
virtual_server 172.31.0.1 80 {
delay_loop 10
lb_algo rr
lb_kind DR
persistence_timeout 9600
protocol TCP
real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
}
以下示例显示了使用直接路由的 Keepalived 配置中的备份服务器的
keepalived.conf 文件。请注意,state 和 priority 值与活动服务器上的 keepalived.conf 文件不同。
global_defs {
notification_email {
admin@example.com
}
notification_email_from noreply_admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 60
}
vrrp_instance RH_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass passw123
}
virtual_ipaddress {
172.31.0.1
}
}
virtual_server 172.31.0.1 80 {
delay_loop 10
lb_algo rr
lb_kind DR
persistence_timeout 9600
protocol TCP
real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
}
global_defs {
notification_email {
admin@example.com
}
notification_email_from noreply_admin@example.com
smtp_server 127.0.0.1
smtp_connect_timeout 60
}
vrrp_instance RH_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass passw123
}
virtual_ipaddress {
172.31.0.1
}
}
virtual_server 172.31.0.1 80 {
delay_loop 10
lb_algo rr
lb_kind DR
persistence_timeout 9600
protocol TCP
real_server 192.168.0.1 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
real_server 192.168.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 10
connect_port 80
}
}
}