LVS-DR 群集 && 配置Nginx负载均衡
- 一、LVS-DR 群集
- 1、相关配置环境
- 2、在RS上配置并启动脚本
- 2.1相关脚本
- 2.2 启动脚本,另一台RS同样步骤
- 3、LVS-DR模式配置脚本
- 4、测试
- 二、Nginx负载均衡
- 1、安装Nginx并关闭相应设置
- 2、向主机 node2,node3 写入内容
- 3、在node1上配置
- 4、测试
一、LVS-DR 群集
1、相关配置环境
主机 | 用途 | ip |
---|---|---|
node1 | VIP | 192.168.85.160 |
node2 | RS1 | 192.168.85.161 |
node3 | RS2 | 192.168.85.162 |
2、在RS上配置并启动脚本
2.1相关脚本
[root@node3 ~]# vim /etc/init.d/lvs_rs
#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.85.160
. /etc/rc.d/init.d/functions
start() {PID=`ifconfig | grep lo:10 | wc -l`if [ $PID -ne 0 ];thenecho "The LVS-DR-RIP Server is already running !"else/sbin/ifconfig lo:10 $VIP netmask 255.255.255.255 broadcast $VIP up/sbin/route add -host $VIP dev lo:10echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/ens33/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/ens33/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announce/bin/touch $LOCKecho "starting LVS-DR-RIP server is ok !"fi
}stop() {/sbin/route del -host $VIP dev lo:10/sbin/ifconfig lo:10 down >/dev/nullecho "0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "0" >/proc/sys/net/ipv4/conf/ens33/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/ens33/arp_announceecho "0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" >/proc/sys/net/ipv4/conf/all/arp_announcerm -rf $LOCKecho "stopping LVS-DR-RIP server is ok !"
}status() {if [ -e $LOCK ];thenecho "The LVS-DR-RIP Server is already running !"elseecho "The LVS-DR-RIP Server is not running !"fi
}case "$1" instart)start;;stop)stop;;restart)stopstart;;status)status;;*)echo "Usage: $1 {start|stop|restart|status}"exit 1
esac
exit 0
2.2 启动脚本,另一台RS同样步骤
chmod +x /etc/init.d/lvs_rs
chkconfig --add lvs_rs
service lvs_rs start
3、LVS-DR模式配置脚本
vim /etc/init.d/lvs_dr
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.85.160
RIP1=192.168.85.161
RIP2=192.168.95.162
DipName=ens33. /etc/rc.d/init.d/functions
start() {PID=`ipvsadm -Ln | grep ${VIP} | wc -l`if [ $PID -gt 0 ];thenecho "The LVS-DR Server is already running !"else#Set the Virtual IP Address/sbin/ifconfig ${DipName}:10 $VIP broadcast $VIP netmask 255.255.255.255 up/sbin/route add -host $VIP dev ${DipName}:10#Clear IPVS Table/sbin/ipvsadm -C#Set Lvs/sbin/ipvsadm -At $VIP:80 -s wrr/sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g -w 2/sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g -w 1/bin/touch $LOCK#Run Lvsecho "starting LVS-DR Server is ok !" fi
}stop() {#clear Lvs and vip /sbin/ipvsadm -C/sbin/route del -host $VIP dev ${DipName}:10/sbin/ifconfig ${DipName}:10 down >/dev/nullrm -rf $LOCKecho "stopping LVS-DR server is ok !"
}status() {if [ -e $LOCK ];thenecho "The LVS-DR Server is already running !"elseecho "The LVS-DR Server is not running !"fi
}case "$1" instart)start;;stop)stop;;restart)stopstart;;status)status;;*)echo "Usage: $1 {start|stop|restart|status}"exit 1
esac
exit 0
4、测试
for ((i=1;i<=6;i++)); do curl 192.168.85.160; done
二、Nginx负载均衡
1、安装Nginx并关闭相应设置
仓库配置
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
关闭设置
systemctl stop firewalld setenforce 0
2、向主机 node2,node3 写入内容
systenctl start nginx 启动nginx服务echo "web test page ip is `hostname -I`" > /usr/share/nginx/html/index.html #写入内容systenctl start nginx 再次启动nginx服务
3、在node1上配置
vim /etc/nginx/nginx.conf #在 http模块中写入如下内容server {listen 80;server_name test.ng.test; 定义域名 也可以用iplocation / {proxy_pass http://web_server;}}upstream web_server {server 192.168.85.161:80; nginx服务器地址 server 192.168.85.162:80;}
4、测试
curl 192.168.85.160