Centos7搭建HAproxy+Nginx+NFS负载均衡实现高可用集群

准备工具:

1.四台centos7服务器,一台客户机

本次实验用到四台服务器,一台服务器安装haproxy实现调度,另两台服务器搭建nginx提供web服务,一台客户机测试访问。

·····················

主机 系统 IP 功能
HAproxy centos7 192.168.254.214 HAproxy
nginx centos7 192.168.254.226 nginx
nginx centos7 192.168.254.227 nginx
Client windows10 192.168.254.135 Edg浏览器
NFS cetos7 192.168.254.228 共享文件

2.一双手和一个脑袋

 如果以上都准备好了,那我们开始正式干活

首先弄好实验需要的环境关闭防火墙和SElinux权限

#关闭防火墙

systemctl stop firewalld.service 
systemctl disable firewalld.service

#永久关闭SElinux权限

sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux


#修改SElinux权限后重启服务器让配置生效,如果嫌麻烦可以使用以下命令临时关闭SElinux

setenforce 0

nginx服务器部署

安装nginx及其依赖包


#安装依赖

yum -y install  gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel

#下载Nginx安装包
cd /usr/local/

wget http://nginx.org/download/nginx-1.9.9.tar.gz  

 解压Nginx安装包

tar -zxvf nginx-1.9.9.tar.gz

 切换到/usr/local/nginx-1.9.9/下

#进行编译安装
./configure
 
make && make install
 

切换到/usr/local/nginx/sbin下,启动nginx服务

./nginx

另一台nginx服务器配置安装同上

验证

用IP到浏览器验证

nginx部署完成

 

HAproxy服务器部署

切换到根目录下cd /

 下载安装包

cd /

wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.19.tar.gz/sha512/f62b0a18f19295986d26c5c60b6b1ad55850a175bed67a359282cc37a4c630a0c2be51d608226b4316f2e69c3008c20a1cb91ce10f86311219355973a050e65b/haproxy-1.8.19.tar.gz

解压

tar -zxf haproxy-1.8.19.tar.gz

编译安装

 make TARGET=linux310 ARCH=x86_64 

#指定安装目录
 make install PREFIX=/usr/local/haproxy

切换到/usr/local/haproxy/下 创建Haproxy配置文件,并写入以下内容

vim haproxy.cfg

global
  #日志
  log 127.0.0.1 local0 info
  #最大连接数
  maxconn 10240
  daemon

defaults
  #应用全局的日志配置
  log global
  mode http
  #超时配置
  timeout connect 5000
  timeout client 5000
  timeout server 5000
  timeout check 2000

listen http_front #haproxy的客户页面
  bind 192.168.254.214:8888         #本机IP地址
  mode http
  option httplog
  stats uri /haproxy
  stats auth admin:admin          #控制面板账号密码admin 账号:admin 
  stats refresh 5s
  stats enable

listen webcluster 
       bind 0.0.0.0:80   
       option httpchk GET /index.html
       balance roundrobin  # 负载均衡模式轮询
       server inst1 192.168.2.226:80 check inter 2000 fall 3  #web服务器IP
       server inst2 192.168.2.227:80 check inter 2000 fall 3

启动Haproxy服务

/haproxy-1.8.19/haproxy -f /usr/local/haproxy/haproxy.cfg

查看服务是否启动成功

lsof -i:8888

浏览器验证

http://192.168.254.214:8888/haproxy

账号是admin 密码admin

 验证轮询是否生效,点击刷新就会切换页面。

 NFS部署

在现实的环境中不可能每次访问页面都不一样,所有要用NFS来实现每次访问页面都能一致,但在现实中用的最多的还是专业存储器。

服务端

在NFS服务器上安装NFS

#安装NFS
yum -y install nfs-utils rpcbind


#启动服务
systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap
systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

创建共享目录

cd ~

mkdir /html

cd /html

#自己创建一个网页文件,随便写点什么
vim index.html



chmod -R 777 /html/

在/etc/exports中插入以下内容

vim /etc/exports

#插入内容

/html   192.168.254.0/24(rw,sync,no_root_squash)  


或

/html   192.168.254.*(rw,sync,no_root_squash)


ro 只读
rw 允许读写
sync 同步写入
no_root_squash 当客户机以root身份访问时,赋予root权限(即超级用户保留权限)
否则,root用户所有请求映射成anonymous用户一样的权限(默认)


#让配置生效
exportfs -a

查看是否共享成功

showmount -e

客户端

两台nginx服务器作为客户端

在nginx服务器上安装nfs客户端,在客户端上并不需要启动nfs服务

yum install nfs-utils

手动挂载共享目录,挂载到nginx页面文件的默认路径下,这样就不用修改默认路径了

mount 192.168.254.228:/html /usr/local/nginx/html/

再去浏览器访问验证

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

)">
下一篇>>