keepalived 高可用lvs的dr模型(vip与dip不在同一网段)
现在rs1和rs2上面安装httpd并准备测试页
[root@rs1 ~]# yum install httpd -y [root@rs1 ~]# echo "this is r1" > /var/www/html/index.html [root@rs1 ~]# systemctl start httpd [root@rs2 ~]# yum install httpd -y [root@rs2 ~]# echo "this is r2" > /var/www/html/index.html [root@rs2 ~]# systemctl start httpd
在lvs和Lvs2上安装keepalived和ipvsadm (为了测试看效果用)httpd(为了实现sorryserver)
[root@lvs ~]# yum install ipvsadm -y
[root@lvs ~]# yum install keepalived -y
[root@lvs ~]# yum install httpd -y
[root@lvs ~]# echo "this is lvs1" > /var/www/html/index.html
[root@lvs ~]# systemctl start httpd
[root@lvs2 ~]# yum install httpd -y
[root@lvs2 ~]# echo "this is lvs2" > /var/www/html/index.html
[root@lvs2 ~]# systemctl start httpd
[root@lvs2 ~]# yum install ipvsadm -y
[root@lvs2 ~]# yum install keepalived -y
配置router服务器(在router上添加一块外网网卡,并添加两个内网地址)开启路由转发
[root@router network-scripts]# cp ifcfg-ens33 ifcfg-eth1 [root@router network-scripts]# vim ifcfg-eth1 NAME="eth1" DEVICE="eth1" ONBOOT=yes NETBOOT=yes BOOTPROTO=static IPADDR=172.16.14.100 NETMASK=255.255.0.0 TYPE=Ethernet [root@router network-scripts]# systemctl restart network [root@router network-scripts]# ip a a 10.0.0.10/8 dev eth0:1 #:这个是临时加的,如果想永久有效,写在配置文件中 [root@router ~]# vim /etc/sysctl.conf net.ipv4.ip_forward=1 [root@router ~]# sysctl -p
配置client服务器网关指向路由
[root@client ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 NAME="eth0" DEVICE="eth0" ONBOOT=yes NETBOOT=yes BOOTPROTO=static IPADDR=172.16.14.200 NETMASK=255.255.0.0 GATEWAY=172.16.14.100 TYPE=Ethernet
配置rs1和rs2的网关指向路由器
[root@rs1 ~]# vinet NAME="eth0" DEVICE="eth0" ONBOOT=yes NETBOOT=yes UUID="95b18552-6cc5-48c5-b29a-91ed73675c50" BOOTPROTO=static IPADDR=192.168.37.24 NETMASK=255.255.255.0 GATEWAY=192.168.37.22 [root@rs1 ~]# systemctl restart network [root@rs2 ~]# vinet NAME="eth0" DEVICE="eth0" ONBOOT=yes NETBOOT=yes UUID="cdd70dbc-1363-45da-9c7a-0ae369f9aac3" BOOTPROTO=static IPADDR=192.168.37.25 NETMASK=255.255.255.0 GATEWAY=192.168.37.22 [root@rs2 ~]# systemctl restart network
配置lvs和lvs2服务器的网关指向路由器
[root@lvs ~]# vinet NAME="eth0" DEVICE="eth0" ONBOOT=yes NETBOOT=yes UUID="15caec5a-6740-4a81-8c72-5fbcb6fb9d25" BOOTPROTO=static IPADDR=192.168.37.23 NETMASK=255.255.255.0 GATEWAY=192.168.37.22 TYPE=Ethernet
[root@lvs2 ~]# vinet
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="686fb723-2360-4683-bb81-8bc6a20778ff"
BOOTPROTO=static
IPADDR=192.168.37.26
NETMASK=255.255.255.0
GATEWAY=192.168.37.22
在rs1和rs2上分别添加vip 并关闭arp通告
[root@rs1 ~]# ifconfig lo:1 10.0.0.100 netmask 255.255.255.255 [root@rs1 ~]# route add -host 10.0.0.100 dev lo:1 [root@rs1 ~]# vim /etc/sysctl.conf net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 [root@rs1 ~]# sysctl -p [root@rs2 ~]# ifconfig lo:1 10.0.0.100 netmask 255.255.255.255 [root@rs2 ~]# route add -host 10.0.0.100 dev lo:1 [root@rs2 ~]# vim /etc/sysctl.conf net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.lo.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 [root@rs2 ~]# sysctl -p
在lvs和Lvs2服务器配置keepalived
[root@lvs ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.100.100.99
}
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka1
vrrp_mcast_group4 224.100.100.99
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 77
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.100/8 dev eth0 lable eth0:1
}
}
state MASTER
interface eth0
virtual_router_id 77
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.100/8 dev eth0 lable eth0:1
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 192.168.37.23 80
lb_kind DR
protocol TCP
sorry_server 192.168.37.23 80
real_server 192.168.37.24 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.25 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.25 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
}
[root@lvs2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka2
vrrp_mcast_group4 224.100.100.99
}
notification_email {
root@localhost
}
notification_email_from Alexandre.Cassen@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id ka2
vrrp_mcast_group4 224.100.100.99
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 77
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.100/8 dev eth0 lable eth0:1
}
}
state BACKUP
interface eth0
virtual_router_id 77
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.0.0.100/8 dev eth0 lable eth0:1
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 192.168.37.26 80
lb_kind DR
protocol TCP
sorry_server 192.168.37.26 80
real_server 192.168.37.24 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.25 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.37.25 80 {
weight 1
HTTP_GET {
url {
path /
status_code=200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
}
在lvs服务器分别启动keepalived
[root@lvs2 ~]# systemctl start keepalived
[root@lvs ~]# systemctl start keepalived
在client服务器测试
下面是keepalived各项参数
还可以在keepalived 上定义通知脚本
在各lvs服务器配置脚本
[root@lvs keepalived]# vim notify.sh #!/bin/bash # contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1" echo "$mailbody" | mail -s "$mailsubject" $contact } case $1 in master) notify master ;; backup) notify backup ;; fault) notify fault ;; *) echo "Usage: $(basename $0) {master|backup|fault}" exit 1 ;; esac
在各lvs服务器配置
[root@lvs keepalived]# vim keepalived.conf vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 77 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.100/8 dev eth0 lable eth0:1 } notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" }