今天以DR模式的LVS群集为基础,增加了一台从负载调度器,使用keepalived来实现主、备调度服务器的热备,从而构架具有负载均衡、高可用两种能力的LVS网站群集平台。
网站拓扑架构图
系统环境
主机 | 操作系统 | IP地址 |
---|---|---|
DR1主调度服务器: | CentOS 7.0 x86_64 | 192.168.100.25 |
DR2备调度服务器: | CentOS 7.0 x86_64 | 192.168.100.26 |
web1节点服务器: | CentOS 7.0 x86_64 | 192.168.100.27 |
web2节点服务器: | CentOS 7.0 x86_64 | 192.168.100.29 |
VIP虚拟IP地址: | 192.168.100.100 | |
Win 7 客户端: | Windows 7 | 192.168.100.30 |
开始部署
一、Web1节点服务器
1.安装httpd软件包
yum install -y httpd
2.启动httpd服务
systemctl start httpd.service
3.新建网站首页文件
echo "<h1>this is web1</h1>" > /var/www/html/index.html
4.新建web.sh脚本至服务管理目录,便于service进行管理
vim /etc/init.d/web.sh
#!/bin/bash
VIP=192.168.100.100 #VIP虚拟IP地址
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
5.给web.sh脚本赋执行权限
chmod +x web.sh
6.启动web.sh服务
service web.sh start
7.关闭防火墙及selinux
systemctl stop firewalld.service
setenforce 0
8.本地访问测试
二、Web2节点服务器
1.安装httpd软件包
yum install -y httpd
2.启动httpd服务
systemctl start httpd.service
3.新建网站首页文件
echo "<h1>this is web2</h1>" > /var/www/html/index.html
4.新建web.sh脚本至服务管理目录,便于service进行管理
vim /etc/init.d/web.sh
#!/bin/bash
VIP=192.168.100.100 #VIP虚拟IP地址
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK "
;;
stop)
ifconfig lo:0 down
route del $VIP /dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
5.给web.sh脚本赋执行权限
chmod +x web.sh
6.启动web.sh服务
service web.sh start
7.关闭防火墙及selinux
systemctl stop firewalld.service
setenforce 0
8.本地访问测试
三、DR1主调度服务器
1.安装keepalived、ipvsadm软件包
yum install keepalived ipvsadm -y
2.修改/proc响应参数
vi /etc/sysctl.conf
net.ipv4.ip_forward=1 #开启路由转发功能
net.ipv4.conf.all.send_redirects = 0 #proc响应关闭重定向功能
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
3.立即生效
sysctl -p
4.制作dr.sh脚本文件方便日常管理,主要配置负载分配策略
vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1 #网关地址
VIP=192.168.100.100 #VIP虚拟地址
RIP1=192.168.100.27 #web1节点服务器IP
RIP2=192.168.100.29 #web2节点服务器IP
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm #保存ipvsadm策略
systemctl start ipvsadm #启动ipvsadm服务
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
5.给dr.sh脚本赋执行权限
chmod +x dr.sh
6.启动dr.sh脚本
service dr.sh start
7.查看lvs节点状态
ipvsadm -ln
8.修改keepalived配置文件如下
vim /etc/keepalived/keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 #指向本地
router_id LVS_01 ##指定服务器名称
...
}
vrrp_instance VI_1 {
state MASTER #主调度服务器
interface ens33
virtual_router_id 10 #主备组号相同
priority 100 #优先级备服务器小于主服务器
...
auth_pass abc123 #验证密码,主备相同
...
virtual_ipaddress {
192.168.100.100 #虚拟VIP地址
}
...
virtual_server 192.168.100.100 80 { #虚拟服务器VIP、端口
...
lb_kind DR
...
real_server 192.168.100.27 80 { #真实web1节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.29 80 { #真实web2节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
9.启动keepalived服务
systemctl start keepalived
四、DR2备调度服务器
1.安装keepalived、ipvsadm软件包
yum install keepalived ipvsadm -y
2.修改/proc响应参数
vi /etc/sysctl.conf
net.ipv4.ip_forward=1 #开启路由转发功能
net.ipv4.conf.all.send_redirects = 0 #proc响应关闭重定向功能
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
3.立即生效
sysctl -p
4.制作dr.sh脚本文件方便日常管理,主要配置负载分配策略
vim /etc/init.d/dr.sh
#!/bin/bash
GW=192.168.100.1 #网关地址
VIP=192.168.100.100 #VIP虚拟地址
RIP1=192.168.100.27 #web1节点服务器IP
RIP2=192.168.100.29 #web2节点服务器IP
case "$1" in
start)
/sbin/ipvsadm --save > /etc/sysconfig/ipvsadm #保存ipvsadm策略
systemctl start ipvsadm #启动ipvsadm服务
/sbin/ifconfig ens33:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
/sbin/route add -host $VIP dev ens33:0
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
/sbin/ipvsadm -C
systemctl stop ipvsadm
ifconfig ens33:0 down
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit 1
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
5.给dr.sh脚本赋执行权限
chmod +x dr.sh
6.启动dr.sh脚本
service dr.sh start
7.查看lvs节点状态
ipvsadm -ln
8.修改keepalived配置文件如下
vim /etc/keepalived/keepalived.conf
global_defs {
...
smtp_server 127.0.0.1 #指向本地
router_id LVS_02 #指定服务器名称
...
}
vrrp_instance VI_1 {
state BACKUP #备用调度服务器
interface ens33
virtual_router_id 10 #主备组号相同
priority 100 #优先级备服务器小于主服务器
...
auth_pass abc123 #验证密码,主备相同
...
virtual_ipaddress {
192.168.100.100 #虚拟VIP地址
}
...
virtual_server 192.168.100.100 80 { #虚拟服务器VIP、端口
...
lb_kind DR
...
real_server 192.168.100.27 80 { #真实web1节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.29 80 { #真实web2节点服务器IP、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
9.启动keepalived服务
systemctl start keepalived
测试
win7客户机访问VIP地址,如下
等待几十秒钟后再刷新,不行的话,请删除浏览器的缓存然后再访问即可
至此,高可用的LVS负载均衡群集已经搭建成功了。
原文地址:http://blog.51cto.com/10316297/2133639