cnetos 脚本部署HAproxy+nginx+nfs

本文章均采用shell脚本搭建,

部署环境:

服务主机名 操作系统 IP地址
HAProxy centos7 192.168.2.212
Nginx1 centos7 192.168.2.206   
Nginx2 centos7 192.168.2.195
nfs centos7 192.168.2.216

 注意,严格安照顺序执行脚本,nginx要最后安装

一,HAProxy安装部署

安装HAProxy的脚本,只需要改ip地址(有注释的,只要改三个IP地址)

登录控制面板​:http://192.168.2.212:8888/haproxy     账号:admin   密码:123456

安装目录在 /usr/local/haproxy  配置的配置文件名:haproxy.cfg(在安装目录下)

#!/bin/bash
#安装部署HAProxy高可以代理
#oyddbc-原创 20211122-9.22

systemctl stop firewalld.service
echo $?
systemctl disable firewalld.service
echo $?
setenforce 0
echo $?
sed -i '7s/.*/SELINUX=disabled/' /etc/sysconfig/selinux

cd /
wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.7.8.tar.gz/sha512/e1d65c8a4607c01d52628f36f8c7342096267130a0f949474746f571158e4f795281f78765004c214a0527f74ce180760f9cc910d3650d30026776076d721c0c/haproxy-1.7.8.tar.gz
tar -zxvf /haproxy-1.7.8.tar.gz
cd /haproxy-1.7.8
make TARGET=linux31
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy/
touch haproxy.cfg

echo global >> haproxy.cfg
echo log 127.0.0.1 local0 info >> haproxy.cfg
echo maxconn 10240 >> haproxy.cfg
echo daemon >> haproxy.cfg
echo >> haproxy.cfg
echo defaults >> haproxy.cfg
echo log global >> haproxy.cfg
echo mode http >> haproxy.cfg
echo timeout connect 5000 >> haproxy.cfg
echo timeout client 5000 >> haproxy.cfg
echo timeout server 5000 >> haproxy.cfg
echo timeout check 2000 >> haproxy.cfg
echo >> haproxy.cfg
echo listen http_front >> haproxy.cfg
echo bind 192.168.2.212:8888 >> haproxy.cfg    #要更改的,为HAProxy的主机IP
echo mode http >>haproxy.cfg
echo option httplog >> haproxy.cfg
echo stats uri /haproxy >> haproxy.cfg
echo stats auth admin:123456 >> haproxy.cfg
echo stats refresh 5s >> haproxy.cfg
echo stats enable >> haproxy.cfg
echo >> haproxy.cfg
echo listen webcluster >> haproxy.cfg
echo bind 0.0.0.0:80 >> haproxy.cfg
echo option httpchk GET /index.html >> haproxy.cfg
echo balance roundrobin >> haproxy.cfg
echo server inst1 192.168.2.195:80 check inter 2000 fall 3 >> haproxy.cfg
#这两行为提供wed服务的nginx服务器IP
echo server inst2 192.168.2.206:80 check inter 2000 fall 3 >> haproxy.cfg

/haproxy-1.7.8/haproxy -f /usr/local/haproxy/haproxy.cfg
echo $?
lsof -i:8888
echo $?

cnetos 脚本部署HAproxy+nginx+nfs_第1张图片

二,NFS的安装配置

运行此脚本,会在根目录生成一个 oyddbc 文件为共享目录,并创建一个index.html网页文件

需要修改的就是最长的那两行,将IP改为nginx服务器的ip地址

#!/bin/bash
#安装Nginx
#oyddbc-原创 20211122-11.41

systemctl stop firewalld.service
echo $?
systemctl disable firewalld.service
echo $?
setenforce 0
echo $?
sed -i '7s/.*/SELINUX=disabled/' /etc/sysconfig/selinux

yum install -y rpc-bind nfs-utils
mkdir /oyddbc
chmod -R 777 /oyddbc
touch /oyddbc/index.html
echo '

oydebadaicai

' >> /oyddbc/index.html echo '/oyddbc/ 192.168.2.195(rw,no_root_squash,no_all_squash,sync)' >> /etc/exports echo '/oyddbc/ 192.168.2.206(rw,no_root_squash,no_all_squash,sync)' >> /etc/exports exportfs -r systemctl start rpcbind systemctl start nfs showmount -e localhost mount localhost:/oyddbc /mnt

运行过程图,那个 0 的意思是上一条命令执行正确,为 1 则是有问题

cnetos 脚本部署HAproxy+nginx+nfs_第2张图片

cnetos 脚本部署HAproxy+nginx+nfs_第3张图片

三,Nginx安装部署脚本

默认网页存放路径:/usr/share/nginx/html

#!/bin/bash
#安装Nginx
#oyddbc-原创 20211122-11.09

systemctl stop firewalld.service
echo $?
systemctl disable firewalld.service
echo $?
setenforce 0
echo $?
sed -i '7s/.*/SELINUX=disabled/' /etc/sysconfig/selinux

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
echo $?
sudo yum install -y nginx
cd /usr/share/nginx/html
sudo systemctl start nginx.service
echo $?
sudo systemctl enable nginx.service
echo $?

mount -t nfs 192.168.2.216:/oyddbc /usr/share/nginx/html   #将nfs共享的文件挂载到网页目录,将其覆盖

cnetos 脚本部署HAproxy+nginx+nfs_第4张图片

 成功后,直接打 HAProxy的IP地址,刷新会一直是这个,因为nfs把Nginx的网页文件都成一样的了,表面没有变,但每一次刷新都会切换让另一台提供服务

 cnetos 脚本部署HAproxy+nginx+nfs_第5张图片

你可能感兴趣的:(反向代理,linux,shell脚本,linux,shell,运维,nginx,负载均衡)