前提准备:
- 两台nginx,一台haproxy
- nginx1:192.168.180.120
- nginx2:192.168.180.130,NFS
- haproxy:192.168.180.110
nginx(两台nginx的操作是一样的):
1. 安装nginx
#先安装这个
yum install -y epel-release
yum install -y nginx
2. 分别编写网页
echo "server1 192.168.180.120" >/usr/share/nginx/html/index.html
echo "server2 192.168.180.130" >/usr/share/nginx/html/index.html
3. 开启nginx
systemctl start nginx
4. 关闭防火墙
systemctl stop firewalld
setenforce 0
haproxy:
1. 安装haproxy(通过源码包安装)
yum install -y gcc gcc-c++ make lrzsz
tar zxf haproxy-2.9.9.tar.gz
cd haproxy-2.9.9
make TARGET=linux-glibc && make install
2. 移动主配置文件
mkdir /etc/haproxy
cp addons/ot/test/sa/haproxy.cfg /etc/haproxy/
3. 修改主配置文件
vim /etc/haproxy/haproxy.cfg
#将端口改为8080#注释#添加内容
frontend http_frontbind *:80default_backend servers-backendbackend servers-backendmode httpserver inst1 192.168.180.120:80 check inter 80 fall 3server inst2 192.168.180.130:80 check inter 80 fall 3 backup
4. 创建自启动脚本
cp ~/haproxy-2.9.9/examples/haproxy.init /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
/etc/init.d/haproxy start
5. 关闭防火墙
systemctl stop firewalld
setenforce 0
NFS-192.168.180.130:
1. 在nginx上均安装
yum install -y nfs-utils rpcbind
2. 创建共享目录
mkdir -p /opt/wwwroot
vim /etc/exports
/opt/wwwroot 192.168.180.0/24(rw,sync,no_root_squash)
3. 分别启动
systemctl start nfs
systemctl start rpcbind
4. 查看NFS共享了什么目录
showmount -e 192.168.180.130
5. nginx均挂载NFS共享目录
mount 192.168.180.130:/opt/wwwroot /usr/share/nginx/html/
6. 创建测试页面
echo "nginx-NFS" > /usr/share/nginx/html/index.html
7. 访问测试