配置前提:各节点时间同步,关闭iptables和selinux;
DR1:
1.配置keepalived
[root@localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@loaclhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.100.100.20
}
//定义虚拟路由实例1
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ck2384
}
virtual_ipaddress {
172.18.20.80/16 dev ens33
}
}
//定义虚拟路由实例2
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass ck2384zw
}
virtual_ipaddress {
172.18.20.81/16 dev ens33
}
}
//定义集群服务20.80
virtual_server 172.18.20.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
//定义RealServer
real_server 172.18.20.5 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.20.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
}
//定义集群服务20.81
virtual_server 172.18.20.81 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
//定义RealServer
real_server 172.18.20.5 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.20.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
2.配置本机为sorry_server
DR2上操作:
1.配置keepalived服务
[root@localhost html]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@loaclhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node2
vrrp_mcast_group4 224.100.100.20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass ck2384
}
virtual_ipaddress {
172.18.20.80/16 dev eth0
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass ck2384zw
}
virtual_ipaddress {
172.18.20.81/16 dev eth0
}
}
virtual_server 172.18.20.80 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.18.20.5 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.20.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
}
}
}
virtual_server 172.18.20.81 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.18.20.5 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.18.20.8 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
}
}
}
2.配置本机为sorry server
在RS1上操作:(RS2上相同操作)
1.设置内核参数,配置两个vip地址,脚本实现
cat setparm.sh
#!/bin/bash
vip1='172.18.20.80'
vip2=’172.18.20.81’
netmask='255.255.255.255'
iface1='lo:0'
iface2=’lo:1’
case $1 in
start)
echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
echo 2 >/proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $iface1 $vip1 netmask $netmask broadcast $vip1 up
ifconfig $iface 2$vip2 netmask $netmask broadcast $vip2 up
route add -host $vip1 dev $iface1
route add -host $vip2 dev $iface2
;;
stop)
ifconfig $iface1 down
Ifconfig $iface2 down
echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce
echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce
;;
*)
Usage:start|stop
esac
2.配置web服务
##启动各节点的web服务,启动keepalived服务;
查看ipvs规则:在DR1上
[root@localhost keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.18.20.80:80 rr
-> 172.18.20.5:80 Route 1 0 0
-> 172.18.20.8:80 Route 1 0 0
TCP 172.18.20.81:80 rr
-> 172.18.20.5:80 Route 1 0 0
-> 172.18.20.8:80 Route 1 0 0
当DR1宕机,DR2能正常调度