MASTER 端
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_KEEP01 #主备上要不同的名称
}
vrrp_instance VI_1 { #主备名称要相同
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51 #Id主备名称要相同
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #VIP
10.125.192.100/24 #多个vip另起一行 每行一个
}
}
virtual_server 10.125.192.100 80 {
delay_loop 6 #健康检查时间
lb_algo wrr #设置调度算法
lb_kind DR #选择lvs模式
nat_mask 255.255.255.0
persistence_timeout 50 #连接保持 在设置的时间内来自同一IP的请求发给同一台real server
protocol TCP
real_server 10.125.192.2 80 {
weight 1 #默认为1,0为失效
inhibit_on_failure #在服务器健康检查失效时,将其设为0,而不是直接从ipvs中删除
TCP_CHECK { #realserve的状态检测设置部分
connect_timeout 8 #连接超时时间
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试时间间隔
connect_port 80 #健康检查端口
}
}
real_server 10.125.192.3 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
ipvsadm -Ln --stats
TCP_CHECK也可以换成
HTTP_GET {
url {
path /
status_code 200
}
BACKUP端
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_KEEP02
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.125.192.100/24
}
}
virtual_server 10.125.192.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.125.192.2 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.125.192.3 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}