centos7 haproxy+Nginx+nfs服务搭建

文件目录

环境

实验步骤:

一.两台Nginx服务器配置 

二.HAProxy负载均衡策略

三.nfs搭建

四.验证


环境

  • 四台虚拟服务器模拟搭建一套Web集群;网站使用nginx搭建;
                 主机                  IP地址                                              主要软件
               Haproxy            192.168.89.181                                               haproxy
                 Nginx            192.168.89.192                                         nginx-1.8.1.tar.gz
                 Nginx            192.168.89.204                                         nginx-1.8.1.tar.gz
                  Nfs            192.168.89.213                                          rpc-bind nfs-utils

实验步骤:

所有主机必须关闭防火墙与核心防护!!!!!

systemctl stop firewalld
setenforce 0

一.两台Nginx服务器配置 

 1.依赖包安装

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

 2.下载nginx-1.8.1 并解压

cd  /usr/local/src/     #切换安装路径
wget  http://nginx.org/download/nginx-1.8.1.tar.gz  
tar -zxvf nginx-1.8.1.tar.gz 

3.编译

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 

4.安装

make && make install 

5.常用命令 启动后使用真机浏览器服务

**进入生成目录**
cd  /usr/local/nginx

**测试**
/usr/local/nginx/sbin/nginx -t

**查看编译模块信息**
/usr/local/nginx/sbin/nginx -V

**启动**
/usr/local/nginx/sbin/nginx

**重新载入配置文件**
/usr/local/nginx/sbin/nginx  -s reload

**重启**
/usr/local/nginx/sbin/nginx  -s reopen

**停止**
/usr/local/nginx/sbin/nginx  -s stop

6.修改html配置文件

vim  /usr/local/nginx/html/index.html
!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>  #第一台把Welcome to nginx!改成web1 第二台改为web2
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p >

<p>For online documentation and support please refer to
< a href=" ">nginx.org</ a>.<br/>
Commercial support is available at
< a href="http://nginx.com/">nginx.com</ a>.</p >

<p><em>Thank you for using nginx.</em></p >
</body>
</html>

二.HAProxy负载均衡策略

 1.安装haproxy

yum install haproxy -y 

2.修改配置文件 vi /etc/haproxy/haproxy.cfg

 #设定转发的后台服务器地址并开启对后台服务器的健康检查
 backend app
    balance     roundrobin
    server  app1 192.168.89.192::80 check  #更改为nginx1 ip
    server  app1 192.168.89.204:80 check  #更改为nginx2 ip 
    #前端设置
frontend main
    #把5000修改成80端口进行负载均衡 访问就不需要加端口号
    bind *:80

#以下需要自行添加
#设定监控平台
listen admin_stats
        stats   enable
	#绑定监控端口号
        bind    *:8080    
        mode    http    
        option  httplog
        log     global
        maxconn 10
        stats   refresh 30s   
        stats   uri /admin   
        stats   realm haproxy
	#设定访问权限,用户名和密码
        stats   auth admin:admin  
        stats   hide-version   
        stats   admin if TRUE 

3.启动服务

systemctl start haproxy

netstat -tnlp |grep haproxy #查看启动情况
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      3257/haproxy        
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      3257/haproxy        

4.用真机浏览器中访问haproxy服务器的IP地址192.168.89.181进行验证,能够成功访问并且刷新网页能跳转到另一台nginx服务器的页面代表实验成功

 

5.查看监控情况

三.nfs搭建

 1.安装需要的软件包

yum install -y rpc-bind nfs-utils

2.创建想要共享的文件夹,例如 /wwx并提权

mkdir /wwx
chmod -R 777 /wwx

3.修改NFS服务的主配置文件vim /etc/exports

#添加如下规则
/wwx *(rw,sync)  #*代表所有主机都可以访问

 4.按顺序启动rpcbind和nfs服务

systemctl start rpcbind
systemctl start nfs

5.保存好配置文件后,需要执行以下命令使配置立即生效

exportfs -r

6.创建访问的html

touch /wwx/index.html 
vim /wwx/index.html
wwx000 #在里面写入一个wwx000

 7.我们可以使用 showmount 命令来查看服务端(本机)是否可连接

[root@localhost wwx]# showmount -e localhost
Export list for localhost:
/wwx *

8.挂载wwx目录

mount   localhost:/wwx   /mnt

在两台Nginx服务器上敲 把共享目录挂载到本机

mount -v -t nfs 192.168.89.213:/wwx /usr/local/nginx/html/  #ip是nfs的ip 将服务端的共享目录挂载到本机的 

四.验证

最后用真机浏览器中访问haproxy服务器的IP地址 怎么刷新都会是nfs上的html就实现了

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