准备的环境
操作系统 centos7.6
服务器名称 | 外网IP | 内网IP |
---|---|---|
LVS | 10.0.0.100/24 | 172.16.1.100/24 |
web01 | 10.0.0.200/24 | 172.16.1.100/24 |
web02 | 10.0.0.222/24 | 172.16.1.100/24 |
:web01、web02的网关要指向172.16.1.100
部署LVS
1.在LVS01上安装ipvsadm软件包以及相关依赖包:
yum install openssl-devel popt-devel libnl-devel ipvsadm -y
2.在LVS01上新建一个shell脚本文件,如下操作所示:
[root@lvs01 ~]# vim /etc/init.d/lvs-nat
#!/bin/sh
LOCK=/var/lock/ipvsadm.lock
VIP=10.0.0.100
RIP1=172.16.1.200
RIP2=172.16.1.222
. /etc/rc.d/init.d/functions
start() {
PID=`ipvsadm -Ln | grep ${VIP} | wc -l`
if [ $PID -gt 0 ];
then
echo "The LVS-NAT Server is already running !"
else
echo "1" >/proc/sys/net/ipv4/ip_forward
echo "0" >/proc/sys/net/ipv4/conf/all/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/default/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/eth0/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/eth1/send_redirects
/bin/touch $LOCK
#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
/sbin/ipvsadm -At $VIP:80 -s rr
/sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -m -w 1
/sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -m -w 1
echo "starting LVS-NAT Server is ok!"
fi
}
stop() {
echo "0" >/proc/sys/net/ipv4/ip_forward
echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/eth0/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/eth1/send_redirects
/sbin/ipvsadm -C
rm -rf $LOCK
echo "stopping LVS-NAT server is ok!"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-NAT Server is already running !"
else
echo "The LVS-NAT Server is not running !"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $1 {start|stop|restart|status}"
exit 1
esac
exit 0
3.授权并启动该脚本
chmod 777 /etc/init.d/lvs-dr
service lvs-dr start
部署web
1.更改nginx的官方源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
2. 安装nginx
[root@web01 ~]# yum install -y nginx
3.删除默认nginx配置为文件
rm -fr /etc/nginx/conf.d/default.conf
4.新建nginx测试文件
vim /etc/nginx/conf.d/test1.conf
server {
listen 80;
server_name localhost;
root /code;
index index.php index.html;
}
5.重启nginx服务
systemctl restart nginx
6.新建站点/code/index.html
[root@web02 ~]# vim /code/index.html
123
7.做完web01,web02可以用scp直接推送,记得在web02创建站点目录
scp /code/index.html 10.0.0.222:/code/