centos 7安装Haproxy+nginx+nfs

本文档详述了一种使用四台虚拟服务器搭建Web集群的方法,包括两台Nginx服务器的配置,HAProxy负载均衡策略的设定,以及NFS的搭建和验证过程。实验中,关闭防火墙,安装并配置Nginx和HAProxy,实现HTTP检查和轮询负载均衡。同时,通过NFS搭建共享存储,确保集群间数据一致性。最后,启用了HAProxy的监控页面并测试了整个集群的功能。
摘要由CSDN通过智能技术生成

文件目录

环境

实验步骤:

一.两台Nginx服务器配置 

二.HAProxy负载均衡策略

三.nfs搭建

四.验证


环境

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

实验步骤:

 关闭防火墙

​
systemctl disable firewalld.service  
systemctl stop firewalld.service

一. 安装nginx两台

 依赖包安装

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

 切换安装路径

cd  /usr/local/src/ 

  下载nginx,如果提示没有wget请自行安装yum -y install wget

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

  解压安装包

tar -zxvf nginx-1.8.1.tar.gz

 打开压缩出来的目录

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
# 测试
/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

 测试nginx1和nginx2 

 二. 编译安装

yum -y install pcre-devel bzip2-devel

 百度网盘提取haproxy包

 百度网盘 ​​​​​​提取码g8n3

 rz上传

tar zxf haproxy-1.5.19.tar.gz -C /usr/src/
cd /usr/src/haproxy-1.5.19/
make TARGET=linux26 && make install

 建立haproxy配置文件

[root@localhost ~]# mkdir /etc/haproxy   //创建配置文件目录
[root@localhost ~]# cd /usr/src/haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/

 根据目前的群集设计,将haproxy.cfg配置文件内容修改如下

global
        log /dev/log    local0 info
        log /dev/log    local0 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
listen  webserver 0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin
        server  inst1   192.168.35.201:80 check inter 2000 fall 3
        server  inst2   192.168.35.202:80 check inter 2000 fall 3

 创建自启动脚本


[root@localhost ~]# cd /usr/src/haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy 
[root@localhost haproxy-1.5.19]# chkconfig --add /etc/init.d/haproxy 
[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
[root@localhost ~]# systemctl stop firewalld

 测试haproxy

  开启haproxy监控页面

  编辑haproxy.cfg  加上下面参数  

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   

 保存退出后

 重起service haproxy restart

 然后访问 http://ip:8080/admin          用户名:admin码:admin 

 

三. 搭建NFS服务器

1. 因为centos7自带了rpcbind,所以不用安装rpc服务,rpc监听在111端口,可以使用

yum -y install nfs-utils                          #安装nsf

 查看rcp是否启动

ss -tnulp | grep 111

  如果没有启动就用一下代码启动

systemctl start rpcbind                               启动rcp

2. 使用 rpm -qa nfs-utils 查看是否安装成功

3. 编辑/etc/exports ,添加以下内容

   /data    192.168.35.0/24(rw,async)

4. 启动nfs服务,systemctl start nfs ,启动后 使用rpcinfo -p 192.168.35.203 查看,如图所示

5. 使用showmount -e localhost

6. 创建/data目录添加文件,更改权限(很重要!!!!!)

 mkdir /data 
 touch /data/1.txt

 echo "hello nfs" >> /data/1.txt

 chown -R nfsnobody.nfsnobody /data

1. 在两台nginx执行 

yum -y intall nfs-utils             #安装nfs

2. 查看rpc是否启动

ss -tnulp | grep 111              

3. 使用showmount -e 192.168.35.203查看

4. 挂载至本地/mnt目录 

[root@localhost ~]# mount -t nfs 192.168.35.203:/data /mnt
[root@localhost ~]# ls /mnt/
1.txt
[root@localhost ~]# echo "2222" >> /mut/1.txt

5.接下来在服务器端执行

   systemctl enable nfs-server.server

   systemctl enable rpcbind

   让nfs,rpcbind开机自动启动,实验完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值