项目说明
环境拓扑
环境介绍
| ||
环境配置
后端 WEB 服务器配置
1
2
|
[root@WEB-01 ~]
# echo "web-01" >/var/www/html/index.html
[root@WEB-01 ~]
# service httpd start
|
1
2
|
[root@WEB-02 ~]
# echo "web-02" >/var/www/html/index.html
[root@WEB-02 ~]
# service httpd start
|
1
2
3
4
|
[root@LK-01 ~]
# curl 172.16.4.102
web-01
[root@LK-01 ~]
# curl 172.16.4.103
web-02
|
#!/bin/bash
#
vip1=172.16.4.1
vip2=172.16.4.2
interface1="lo:0"
interface2="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$interface1 $vip1 broadcast $vip1 netmask 255.255.255.255 up
ifconfig$interface2 $vip2 broadcast $vip2 netmask 255.255.255.255 up
route add-host $vip1 dev $interface1
route add-host $vip2 dev $interface2
;;
stop)
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
ifconfig$interface1 down
ifconfig$interface2 down
;;
status)
if ifconfiglo:0 |grep $vip1 &> /dev/null; then
echo"ipvs1 is running."
else
echo"ipvs1 is stopped."
fi
if ifconfiglo:1 |grep $vip2 &> /dev/null; then
echo"ipvs2 is running."
else
echo"ipvs2 is stopped."
fi
;;
*)
echo"Usage: `basename $0` {start|stop|status}"
exit 1
esac
[root@LK-01 ~]# yum -y install keepalived
[root@LK-02 ~]# yum -y install keepalived
[root@LK-01 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_idLVS_DEVEL
}
vrrp_script chk_mt_down {
script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #定义一个脚本,判断down文件是否存在,存在就退出执行,不存在继续执行
interval 1 #1秒检测一次
weight -5 #如果down文件存在,VRRP实例优先级减5
}
vrrp_instance VI_1 { #定义VRRP热备实例
state MASTER #MASTER表示主节点
interfaceeth 0 #承载VIP的物理接口
virtual_router_id 51 #虚拟路由器的ID号
priority 100 #优先级
advert_int 1 #通知间隔秒数(心跳频率)
authentication { #设置主备节点直接的认证信息
auth_type PASS #
auth_pass asdfgh #
}
virtual_ipaddress { #设置集群中的VIP地址
172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0
}
track_script {
chk_mt_down #调用上面定义的脚本,如果这里没有调用,那么上面定义的脚本是无法生效的
}
}
vrrp_instance VI_2 { #定义实例为HK-02的备份节点
state BACKUP
interfaceeth 0
virtual_router_id 52
priority 99 #设置优先级,低于主服务器
advert_int 1
authentication {
auth_type PASS
auth_pass qwerty
}
virtual_ipaddress {
172.16.4.2
}
track_script {
chk_mt_down
}
}
virtual_server 172.16.4.1 80 { #定义一个VIP地址和端口
delay_loop 6
lb_algo rr #轮训算法
lb_kind DR #调度类型
nat_mask 255.255.0.0
# persistence_timeout 50 #设置长连接时长,单位秒
protocolTCP
real_server 172.16.4.102 80 { #定义后端RS服务器的相关信息
weight 1 #设置权重
HTTP_GET { #设置健康状态检测方式
url { #健康检查判断后端服务器首页是否访问正常(返回200状态码),如果正常则认为服务器正常
path /index.html
status 200
}
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 1 #重试时间间隔
}
}
real_server 172.16.4.103 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
}
virtual_server 172.16.4.2 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
# persistence_timeout 50
protocol TCP
real_server 172.16.4.102 80 {
weight 1
HTTP_GET {
url {
path /index.html
status 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.4.103 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
}
[root@LK-02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_idLVS_DEVEL
}
vrrp_script chk_mt_down {
script"[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -5
}
vrrp_instance VI_1 {
state BACKUP
interfaceeth 0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass asdfgh
}
virtual_ipaddress {
172.16.4.1/32 brd 172.16.4.1 dev eth0 label eth0:0
}
track_script {
chk_mt_down
}
}
vrrp_instance VI_2 {
state MASTER
interfaceeth 0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass qwerty
}
virtual_ipaddress {
172.16.4.2
}
track_script{
chk_mt_down
}
}
virtual_server 172.16.4.1 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
# persistence_timeout 50
protocol TCP
real_server 172.16.4.102 80 {
weight 1
HTTP_GET{
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.4.103 80 {
weight 1
HTTP_GET{
url {
path /index.html
status 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
}
virtual_server 172.16.4.2 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
# persistence_timeout 50
protocol TCP
real_server 172.16.4.102 80 {
weight 1
HTTP_GET{
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.4.103 80 {
weight 1
HTTP_GET{
url {
path /index.html
status 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 1
}
}
}
[root@LK-01 ~]# service keepalived start
[root@LK-02 ~]# service keepalived start
验证
Vip 地址和集群规则验证
[root@LK-01 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ff
inet172.16.4.100/16 brd 172.16.255.255 scope global eth0
inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0
inet6fe80::20c:29ff:fe22:c5c2/64 scope link
valid_lft forever preferred_lft forever
[root@LK-01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr
->172.16.4.102:80 Route 1 0 0
->172.16.4.103:80 Route 1 0 0
TCP 172.16.4.2:80 rr
->172.16.4.102:80 Route 1 0 0
->172.16.4.103:80 Route 1 0 0
[root@LK-02 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:f1:dd:b2 brd ff:ff:ff:ff:ff:ff
inet172.16.4.101/16 brd 172.16.255.255 scope global eth0
inet172.16.4.2/32 scope global eth0
inet6fe80::20c:29ff:fef1:ddb2/64 scope link
valid_lft forever preferred_lft forever
[root@LK-02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr
->172.16.4.102:80 Route 1 0 0
->172.16.4.103:80 Route 1 0 0
TCP 172.16.4.2:80 rr
->172.16.4.102:80 Route 1 0 0
->172.16.4.103:80 Route 1 0 0
负载均衡验证
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
高可用验证
[root@LK-02 ~]# touch /etc/keepalived/down
[root@LK-01 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:22:c5:c2 brd ff:ff:ff:ff:ff:ff
inet172.16.4.100/16 brd 172.16.255.255 scope global eth0
inet172.16.4.1/32 brd 172.16.4.1 scope global eth0:0
inet172.16.4.2/32 scope global eth0
inet6fe80::20c:29ff:fe22:c5c2/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# curl 172.16.4.1
web-02
[root@localhost ~]# curl 172.16.4.1
web-01
[root@localhost ~]# curl 172.16.4.2
web-02
[root@localhost ~]# curl 172.16.4.2
web-01
健康检测验证
[root@WEB-02 ~]# service httpd stop
[root@LK-01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr
->172.16.4.102:80 Route 1 0 1
TCP 172.16.4.2:80 rr
->172.16.4.102:80 Route 1 0 1
[root@LK-02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr
->172.16.4.102:80 Route 1 0 0
TCP 172.16.4.2:80 rr
->172.16.4.102:80 Route 1 0 0