keepalived+lvs(dr)
- 实验环境
lvs1 | lvs2 | rs1 | rs2 | |
---|---|---|---|---|
ip | 10.0.0.81 | 10.0.0.82 | 10.0.0.83 | 10.0.0.149 |
主机名 | c1 | c2 | c3 | c4 |
mvp | 10.0.0.100 | 10.0.0.100 | 10.0.0.100 | 10.0.0.100 |
搭建lvs
- lvs1
## 安装ipvsidm
[root@c1 ~]# yum install -y ipvsadm
##添加虚拟ip
[root@c1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR0=10.0.0.81
PREFIX0=24
IPADDR1=10.0.0.100
PREFIX1=24
GATEWAY=10.0.0.254
DNS1=114.114.114.114
##重启网卡
[root@c1 ~]# ifdown eth0;ifup eth0
root@c1 ~]# ipvsadm -A -t 10.0.0.100:80 -s rr
[root@c1 ~]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.83:80 -g
[root@c1 ~]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.149:80 -g
[root@c1 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
#关闭selinux和防护墙
[root@c1 ~]# systemctl stop firewalld
[root@c1 ~]# setenforce 0
- lvs2
## 安装ipvsidm
[root@c2 ~]# yum install -y ipvsadm
##添加虚拟ip
[root@c2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR0=10.0.0.82
PREFIX0=24
IPADDR1=10.0.0.100
PREFIX1=24
GATEWAY=10.0.0.254
DNS1=114.114.114.114
##重启网卡
[root@c2 ~]# ifdown eth0;ifup eth0
[root@c2 ~]# ipvsadm -A -t 10.0.0.100:80 -s rr
[root@c2 ~]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.83:80 -g
[root@c2 ~]# ipvsadm -a -t 10.0.0.100:80 -r 10.0.0.149:80 -g
[root@c2 ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
#关闭selinux和防护墙
[root@c2 ~]# systemctl stop firewalld
[root@c2 ~]# setenforce 0
rs1
## 编写arp
cat >> /etc/sysctl.conf <<EOF
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
EOF
[root@c3 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
## 搭建apache服务
[root@c3 ~]# yum install -y httpd
[root@c3 ~]# systemctl start httpd
[root@c3 ~]# echo 'rs1' > /var/www/html/index.html
[root@c3 ~]# curl localhost
rs1
##添加ip和路由
[root@c3 ~]# yum install -y net-tools
[root@c3 ~]# ifconfig lo:0 10.0.0.100/32 broadcast 10.0.0.100 up
[root@c3 ~]# route add -host 10.0.0.100 dev lo:0
#关闭selinux和防护墙
[root@c3 ~]# systemctl stop firewalld
[root@c3 ~]# setenforce 0
rs2
## 编写arp
cat >> /etc/sysctl.conf <<EOF
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
EOF
[root@c4 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
## 搭建apache服务
[root@c4 ~]# yum install -y httpd
[root@c4 ~]# systemctl start httpd
[root@c4 ~]# echo 'rs2' > /var/www/html/index.html
[root@c4 ~]# curl localhost
rs1
##添加ip和路由
[root@c4 ~]# yum install -y net-tools
[root@c4 ~]# ifconfig lo:0 10.0.0.100/32 broadcast 10.0.0.100 up
[root@c4 ~]# route add -host 10.0.0.100 dev lo:0
#关闭selinux和防护墙
[root@c4 ~]# systemctl stop firewalld
[root@c4 ~]# setenforce 0
搭建keepalive
- 把ip地址修改为最初状态(之前修改ip只是为了看lvs的实验环境是否正确)
#lvs1
[root@c1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.81
PREFIX=24
GATEWAY=10.0.0.254
DNS1=114.114.114.114
#lvs2
[root@c2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.82
PREFIX=24
GATEWAY=10.0.0.254
DNS1=114.114.114.114
- lvs1
[root@c1 ~]# yum install -y keepalived
## 编写脚本
[root@c1 ~]# mkdir /scripts
[root@c1 ~]# cat /scripts/notify.sh
#!/bin/bash
case "$1" in
master)
ipvsadm -R < /etc/sysconfig/ipvsadm
;;
backup)
ipvsadm -C
;;
*)
echo "Usage:$0 master|backup"
;;
esac
[root@c1 ~]# chmod +x /scripts/notify.sh
##编写配置文件
[root@c1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass hahahaha
}
virtual_ipaddress {
10.0.0.100
}
notify_master "/scripts/notify.sh master"
notify_master "/scripts/notify.sh master"
}
virtual_server 10.0.0.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.0.0.81 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.0.0.82 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
## 启动服务
[root@c1 ~]# systemctl enable --now keepalived
- lvs2
##安装keepalived
[root@c2 ~]# yum install -y keepalived
## 编写脚本
[root@c2 ~]# mkdir /scripts
[root@c2 ~]# cat /scripts/notify.sh
#!/bin/bash
case "$1" in
master)
ipvsadm -R < /etc/sysconfig/ipvsadm
;;
backup)
ipvsadm -C
;;
*)
echo "Usage:$0 master|backup"
;;
esac
[root@c2 ~]# chmod +x /scripts/notify.sh
##编写配置文件
[root@c2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 100
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass hahahaha
}
virtual_ipaddress {
10.0.0.100
}
notify_master "/scripts/notify.sh master"
notify_master "/scripts/notify.sh master"
}
virtual_server 10.0.0.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.0.0.81 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.0.0.82 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
## 启动服务
[root@c1 ~]# systemctl enable --now keepalived
- 验证结果
## ls2上没有vip
[root@c2 ~]# ip a | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 10.0.0.82/24 brd 10.0.0.255 scope global noprefixroute eth0
##将ls1关机模拟宕机
[root@c1 ~]# init 0
## ls2查看结果
[root@c2 ~]# ip a | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 10.0.0.82/24 brd 10.0.0.255 scope global noprefixroute eth0
inet 10.0.0.100/32 scope global eth0 #vip跳转过来了