大致部署:一台haproxy服务器做调度负载均衡;两台nginx服务器做web服务;一台NFS服务器做文件共享目录。
- 服务器 所需部署的服务 服务器IP地址
- haproxy服务器 haproxy服务 192.168.83.158
- NFS服务器 NSF、RPS服务 192.168.83.140
- nginx服务器1 nginx服务 192.168.83.139
- nginx服务器2 nginx服务 192.168.83.157
实验前先关闭防火墙和修改SELINUX配置文件
1.systemctl stop firewalld.service #停止防火墙服务
2.systemctl disable firewalld.service #开机不自启动防火墙服务
3.vim /etc/sysconfig/selinux #进入SELINUX配置文件
4.在SELINUX配置文件中将SELINUX=enforcing修改为SELINUX=disabled #将selinux的配置改为不启动
也可以使用sed -i 's/enforcing/disabled/' /etc/selinux/config这条命令而不用进入到/etc/sysconfig/selinux文件里面手动更改,执行了这命令也就不用执行上面的3、4步
5.reboot #重启使修改的配置生效
部署NFS服务器,在 NFS服务器上执行以下脚本安装NFS服务
#!/bin/bash #function:安装nfs 创建/nfs/share作为共享文件 #author:zhuzi 20211122 yum install nfs-utils rpcbind -y #yum安装nfs和rpc服务 touch /etc/exports #创建nfs的配置文件 mkdir -p /nfs/share #创建nfs共享文件目录 chown -R nfsnobody.nfsnobody /nfs/share/ #赋予共享目录/nfs/share权限 echo "/nfs/share *(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 /nfs/share/index.html #在共享目录下创建网页文件 echo "zhuzi" >> /nfs/share/index.html #将 zhuzi 追加写入到/nfs/share/index.html这个文件中 echo nfs服务部署完成.
部署nginx服务器1,执行以下脚本安装ningx服务
#!/bin/bash #function: Centos7一键安装nginx #author:zhuzi 20211122 yum -y install gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel #安装nginx依赖的环境 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 #进入解压后的ningx目录 ./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部署完成. #提示部署完成 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 请先将nginx服务器2的nginx服务安装到这一步再进行测试,测试完成后再按任意键继续进行下一步操作 read a #只是为了在这一步停留使用,变量a在其他处没有用到 mount -t nfs 192.168.83.140:/nfs/share /usr/local/nginx/html/ #挂载nfs上的目录到nginx的html目录下 df -lh #查看挂载情况
nginx服务器1挂载前的测试结果:
注意:两台nginx服务器先别进行挂载,等把haproxy服务器安装测试完再挂载
部署nginx服务器2,执行以下脚本安装ningx服务
#!/bin/bash #function: Centos7一键安装nginx #author:zhuzi 20211122 yum -y install gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel #安装nginx依赖的环境 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 #进入解压后的ningx目录 ./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/888/' /usr/local/nginx/html/index.html #将网页中的nginx改为888 /usr/local/nginx/sbin/nginx #启动nginx服务 echo nginx部署完成. #提示部署完成 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 请先进行测试,测试完成后再按任意键进行下一步操作 read a #只是为了在这一步停留使用,变量a在其他处没有用到 mount -t nfs 192.168.83.140:/nfs/share /usr/local/nginx/html/ #挂载nfs上的目录到nginx的html目录下 df -lh #查看挂载情况
nginx服务器2挂载前的测试结果:
注意:两台nginx服务器先别进行挂载,等把haproxy服务器安装测试完再挂载
部署haproxy服务器,在haproxy服务器上执行以下脚本安装haproxy服务
#!/bin/bash #function:安装haproxy修改配置文件并添加两台web #author:zhuzi 20211122 yum install haproxy -y #yum安装haproxy sed -i "82s/127.0.0.1:5001/192.168.83.139:80/" /etc/haproxy/haproxy.cfg #将haproxy配置文件82行的127.0.0.1替换为nginx1的ip sed -i "83s/127.0.0.1:5002/192.168.83.157:80/" /etc/haproxy/haproxy.cfg #将haproxy配置文件82行的127.0.0.1替换为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 systemctl start haproxy.service #开启haproxy程序 echo haproxy部署完成 echo 192.168.83.158:8080/admin为haproxy程序的登录管理页面地址 echo 管理页面登录账户:admin 密码:admin
在两台nginx台服务器未挂载前,访问haproxy服务器的IP地址时,显示的界面是两台nginx服务器的界面,可通过刷新进行切换页面
进入haproxy管理页面:
两台nginx服务器进行挂载后,访问haproxy服务器的IP地址时显示的界面就会发生改变,并且刷新也是相同的界面
到此就实现了负载均衡



