centos7上部署haproxy+nginx+nfs(脚本部署)

目的:两台nginx服务器做web服务,haproxy服务器做调度负载均衡,为保证提供的内容一致性使用nfs做共享目录,两台nginx服务器使用相同的共享目录文件提供web服务

硬件需求
centos7服务器 haproxy 192.168.102.152
centos7服务器 nginx 192.168.102.166
centos7服务器 nginx 192.168.102.167
centos7服务器 nfs+rps 192.168.102.163

实验环境,请关闭防火墙和selinux再执行后续操作

#关闭防火墙和selinux代码如下
systemctl stop firewalld.service    #停止防火墙服务
systemctl disable firewalld.service  #开机不自启动防火墙服务
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux   #将selinux的配置文件改为不启动
reboot     #重启服务器,修改selinux配置后重启生效

在作为haproxy的服务器上导入以下shell执行haproxy安装

#!/bin/bash
#function:安装haproxy修改配置文件并添加两台web
#author:999vip 2021118
yum install haproxy -y  #yum安装haproxy
echo 请输入nginx服务器1ip  
read a                  #弹出输入框并将输入内容作为变量a
echo 请输入nginx服务器2ip
read b                  #弹出输入框并将输入内容作为变量a
sed -i "82s/127.0.0.1:5001/$a:80/"  /etc/haproxy/haproxy.cfg        #替换haproxy的配置文件82行为nginx1的ip
sed -i "83s/127.0.0.1:5002/$b:80/"  /etc/haproxy/haproxy.cfg        #替换haproxy的配置文件83行为nginx2的ip
sed -i '63s/5000/80/'  /etc/haproxy/haproxy.cfg                     #将haproxy的默认5000端口改为80端口
echo "listen admin_stats" >> /etc/haproxy/haproxy.cfg               #设置haproxy的web管理用户
echo "stats enable" >> /etc/haproxy/haproxy.cfg                     #开启haproxy程序web服务
echo "bind *:8080" >> /etc/haproxy/haproxy.cfg                      #haproxy管理页面端口为8080
echo "mode http" >> /etc/haproxy/haproxy.cfg                        #以下为haproxy系统配置
echo "option httplog" >> /etc/haproxy/haproxy.cfg
echo "log global" >> /etc/haproxy/haproxy.cfg
echo "maxconn 10" >> /etc/haproxy/haproxy.cfg
echo "stats refresh 30s" >> /etc/haproxy/haproxy.cfg
echo "stats uri /admin" >> /etc/haproxy/haproxy.cfg
echo "stats realm haproxy" >> /etc/haproxy/haproxy.cfg
echo "stats auth admin:admin" >> /etc/haproxy/haproxy.cfg
echo "stats hide-version" >> /etc/haproxy/haproxy.cfg
echo "stats admin if TRUE" >> /etc/haproxy/haproxy.cfg              #以上为haproxy系统配置
systemctl start haproxy.service                                     #开启haproxy程序
c=$(ip a | grep "inet "|grep ens33| awk '{print $2}'|awk -F/ '{print $1}')  #变量c等于本机ip
echo haproxy部署完成
echo 访问$c将自动轮询$a和$b的web页面
echo 访问$c:8080/admin为haproxy程序的管理页面
echo 管理页面登录账户为admin密码为admin

执行结果如下,在弹出的提示语后分别输入两台nginx服务器ip地址(根据实际环境输入)

由于nginx服务需要用的nfs共享目录,这里我们先部署nfs在部署nginx

#!/bin/bash
#function:安装nfs 创建/app/file作为共享文件
#author:999vip 20211118
yum install nfs-utils rpcbind -y #yum安装nfs和rpc服务
touch /etc/exports               #创建nfc的配置文件
mkdir -p /app/file               #创建nfc共享文件目录
chown -R nfsnobody.nfsnobody /app/file/ #赋予共享目录nfs权限
echo "/app/file *(rw,sync)" >> /etc/exports #允许所有IP访问nfs共享目录并有可读写权限
exportfs –rv                               #载入配置
systemctl enable nfs                        #开机自启动nfs
systemctl enable rpcbind                    #开机自启动rps
systemctl start nfs                         #启动nfs程序
systemctl start rpcbind                     #启动rps程序
touch /app/file/index.html                  #在共享目录下创建网页文件
echo "<h1>999vip</h1>" >> /app/file/index.html #在网页文件中写入要显示的数据
echo nfs服务部署完成

运行结果如下表示已完成nfs服务部署

 

nginx服务器1部署

#!/bin/bash
#function: Centos7一键安装nginux
#author:999vip 20211117
yum -y install  gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel #安装nginx所需环境
cd  /usr/local/src/                                                                              #切换到安装目录
wget  http://nginx.org/download/nginx-1.8.1.tar.gz                                               #下载nginx到当前目录
tar -zxvf nginx-1.8.1.tar.gz                                                                     #解压nginx安装包
cd  nginx-1.8.1                                                                                  #进入解压后目录
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre                                                     #编译文件
make && make install                                                                             #编译安装
cd  /usr/local/nginx                                                                             #进入nginx安装目录
sed -i '14s/nginx/BDQN/' /usr/local/nginx/html/index.html                                        #方便测试区分将网页中nginx改为BDQN
/usr/local/nginx/sbin/nginx                                                                      #启动nginx服务
echo nginx部署完成,请测试web页面                                                                #屏幕输出内容提醒用户
echo 接下来将部署nfs并映射html,请确认是否继续                                                    #屏幕输出内容提醒用户
echo 键入任意值将继续操作,如需中断请按Ctrl+c                                                    #屏幕输出内容提醒用户
read a                                                                                           #弹出框暂停执行,用户键入任意值继续执行,键入值作为变量a但后续并不调用这个变量
yum install nfs-utils rpcbind  -y                                                                #安装nfs和rpc服务
systemctl enable nfs                                                                             #开机自启动nfs服务
systemctl enable rpcbind                                                                         #开机自启动rps服务
systemctl start nfs                                                                              #启动nfs服务
systemctl start rpcbind                                                                          #启动rpc服务
echo nfs服务安装完成,接下来将进行挂载操作                                                       #屏幕输出内容提醒用户 
echo 键入任意值将继续操作,如需中断请按Ctrl+c                                                    #屏幕输出内容提醒用户
echo 请输入nfs服务器ip                                                                           #屏幕输出内容提醒用户
read b                                                                                           #将键入值作为变量b
mount -t nfs $b:/app/file /usr/local/nginx/html/                                                 #挂载nfs上的目录到nginx的html目录下

注意:运行到下面这里时提示nginx部署完成,按任意键进行下一步时可暂停下,进行nginx2服务器部署

 nginx服务器2部署

#!/bin/bash
#function: Centos7一键安装nginux
#author:999vip 20211117
yum -y install  gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel #安装nginx所需环境
cd  /usr/local/src/                                                                              #切换到安装目录
wget  http://nginx.org/download/nginx-1.8.1.tar.gz                                               #下载nginx到当前目录
tar -zxvf nginx-1.8.1.tar.gz                                                                     #解压nginx安装包
cd  nginx-1.8.1                                                                                  #进入解压后目录
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre                                                     #编译文件
make && make install                                                                             #编译安装
cd  /usr/local/nginx                                                                             #进入nginx安装目录
/usr/local/nginx/sbin/nginx                                                                      #启动nginx服务
echo nginx部署完成,请测试web页面                                                                #屏幕输出内容提醒用户
echo 接下来将部署nfs并映射html,请确认是否继续                                                    #屏幕输出内容提醒用户
echo 键入任意值将继续操作,如需中断请按Ctrl+c                                                    #屏幕输出内容提醒用户
read a                                                                                           #弹出框暂停执行,用户键入任意值继续执行,键入值作为变量a但后续并不调用这个变量
yum install nfs-utils rpcbind  -y                                                                #安装nfs和rpc服务
systemctl enable nfs                                                                             #开机自启动nfs服务
systemctl enable rpcbind                                                                         #开机自启动rps服务
systemctl start nfs                                                                              #启动nfs服务
systemctl start rpcbind                                                                          #启动rpc服务
echo nfs服务安装完成,接下来将进行挂载操作                                                       #屏幕输出内容提醒用户 
echo 键入任意值将继续操作,如需中断请按Ctrl+c                                                    #屏幕输出内容提醒用户
echo 请输入nfs服务器ip                                                                           #屏幕输出内容提醒用户
read b                                                                                           #将键入值作为变量b
mount -t nfs $b:/app/file /usr/local/nginx/html/                                                 #挂载nfs上的目录到nginx的html目录下

同样,在提示nginx部署完成时不按回车,进行haproxy测试,访问haproxy服务器的web页面会自动转到nginx1服务器的web页面,点击刷新后会跳转到nginx2服务器的web页面,多次刷新会顺序跳转

 

 这时测试完成,能正常做到负载均衡,到nginx上敲下回车或者键入任意值回车进行nfs安装,安装完成后提示输入nfs服务器ip(根据实际部署的ip进行输入)

 输入后会自动挂载nfs上脚本创建的目录到本地,这时再去访问haproxy的web页面发现出现的页面发生变化了,而且刷新也是相同界面,到这里就实现了负载均衡加页面统一

 到这里我们就可以去haproxy后台查看转发记录了,浏览器访问haproxy的IP:8080/admin进入后台

账户名:admin 密码:admin

后台显示两台nginx转发次数相差不超过±1即为正常,至此搭建完成,可以将项目移交给开发人员进行网站开发了

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>