永夜海 发表于 2019-1-2 08:30:45

haproxy实现discuz论坛的动静分离和负载均衡

  一、实验拓扑图
https://s4.运维网.com/wyfs02/M02/96/2E/wKiom1kdoPKAMYzvAADq1jKhXWg197.png-wh_500x0-wm_3-wmp_4-s_3418249106.png
  二、实验步骤
  A、实验环境准备
  1、在node2、node3、node4上安装httpd
# yum -y install httpd  2、在node3和node4上安装php 、php-mysql
# yum -y install php php-mysql  3、在172.18.250.193上安装mariadb,并进行配置
#yum-y install mariadb  启动数据库
#systemctl start mariadb  检查端口
#ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port            
LISTEN   0      50                *:3306                        *:*  配置数据库相关内容
#mysql
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database dz;
MariaDB [(none)]> grant all privileges on dz.* to 'dzadmin'@'%' identified by '123456';
MariaDB [(none)]> grant all privileges on dz.* to 'dzadmin'@'localhost' identified by '123456';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| dz               |
| mysql            |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye  远程登陆测试创建的数据库用户是否正常连接
#mysql -udzadmin -p123456 -h172.18.250.193
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>  4、编辑node2、node3、node4中httpd的配置文件httpd.conf
# vim /etc/httpd/conf/httpd.conf  修改添加如下内容
ServerName node2.byq.com:80
DocumentRoot "/var/www/html/upload"
ServerName node3.byq.com:80
DocumentRoot "/var/www/html/upload"
ServerName node4.byq.com:80
DocumentRoot "/var/www/html/upload"  

  这里配置完毕是无法启动httpd服务的,因为upload目录还未生成,等下面配置完毕再启动httpd服务
  

  5、在node3上下载 Discuz安装文件
#wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip  解压并安装
#mkdir Discuz
#unzip -d /root/Discuz Discuz_X3.3_SC_UTF8.zip  将解压后的所有文件拷贝至/var/www/html
#cp -a /root/Discuz/* /var/www/html/  给所有目录及文件配置权限
#chmod -R 777 /var/www/html/upload/*  安装 Discuz
  这里就做解释了,安装步骤就是确认,下一步即可
  

  将配置好的文件分别scp到node3和node4的对应目录下
#scp -r /var/www/html/* 172.18.249.57:/var/www/html/
#scp -r /var/www/html/* 172.18.10.11:/var/www/html/  

  6、将node3和node4的静态文件存放目录挂载到node2下
#/var/www/html/upload中需要挂载的目录如下
/data   -----数据缓存及附件
/static -----静态文件  

  将node2设置为NFS服务器
# yum install nfs-utils rpcbind
# service rpcbind start  编辑共享文件配置
# vim /etc/exports
#NFS输出目录为data,可以访问这个目录的主机为node3和node4,权限为读写,访问uid为0(root),允许超过1024的端口号>连接
/var/www/html/upload/data/172.18.10.10(rw,all_squash,anonuid=0,insecure)
/var/www/html/upload/data/172.18.10.11(rw,all_squash,anonuid=0,insecure)
#NFS输出目录为data,可以访问这个目录的主机为node3和node 4,权限为读写,访问uid为0(root),允许超过1024的端口号>连接
/var/www/html/upload/static/172.18.10.10(rw,all_squash,anonuid=0,insecure)
/var/www/html/upload/static/172.18.10.11(rw,all_squash,anonuid=0,insecure)  启动nfs服务
  # service nfs start
  查看共享文件目录
  # showmount -e
  Export list for localhost.localdomain:
  /var/www/html/upload/static 172.18.10.11,172.18.10.10
  /var/www/html/upload/data   172.18.10.11,172.18.10.10
  启动httpd服务
  # service httpdstart
  

  7、在node3上挂载共享存储目录
# yum install nfs-utils rpcbind
# service rpcbind start
# service nfs start
#mount 172.18.249.57:/var/www/html/upload/data/ /var/www/html/upload/data/
#mount 172.18.249.57:/var/www/html/upload/static/ /var/www/html/upload/static/
#showmount -e 172.18.249.57
Export list for 172.18.249.57:
/var/www/html/upload/static 172.18.10.11,172.18.10.10
/var/www/html/upload/data   172.18.10.11,172.18.10.10  

  8、在node4上挂载共享存储目录
# yum install nfs-utils rpcbind
# service rpcbind start
# service nfs start
# yum install nfs-utils rpcbind
# service rpcbind start
# service nfs start
#mount 172.18.249.57:/var/www/html/upload/data/ /var/www/html/upload/data/
#mount 172.18.249.57:/var/www/html/upload/static/ /var/www/html/upload/static/
#showmount -e 172.18.249.57
Export list for 172.18.249.57:
/var/www/html/upload/static 172.18.10.11,172.18.10.10
/var/www/html/upload/data   172.18.10.11,172.18.10.10  

  9、在node上安装haproxy
  开启日志功能
# vim /etc/rsyslog.conf
#打开或增加一下几行,让udp514端口处于监听状态,并将日志纪录到指定文件中
$ModLoad imudp
$UDPServerRun 514
local2.*                                                /var/log/haproxy.log
# systemctl restart rsyslog.service
# ss -unl | grep 514
UNCONN   0      0            *:514                      *:*                  
UNCONN   0      0         :::514                     :::*  

  配置haproxy
#yum install haproxy
#cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
#vim /etc/haproxy/haproxy.cfg
global    #全局配置
    log         127.0.0.1 local2    #日志纪录位置
    chroot      /var/lib/haproxy    #haproxy的工作目录
    pidfile   /var/run/haproxy.pid    #pid文件位置
    maxconn   4000                  #最大连接数
    user      haproxy                #运行时使用的用户身份
    group       haproxy                #运行时使用的组身份
    daemon                            #启动为守护进程,不加此处运行在前台
    stats socket /var/lib/haproxy/stats    #本地访问stats统计信息时以套接字方式通信
defaults    #默认配置
    mode                  http    #已http模式运行
    log                     global    #默认日志为全局配置中日志的设置
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8    #除本机外所有发往服务器的请求首部中加入“X-Forwarded-For”首部
    option                  redispatch
    retries               3
    timeout http-request    10s
    timeout queue         1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check         10s
    maxconn               3000    #前端最大并发连接数
listen static    #设置统计报告页面
      bind *:9000    #监听在本机9000端口
      stats enable    #打开
    stats hide-version    #隐藏haproxy版本
    stats uri /haadmin?admin#统计页面路径
    stats realm "HAProxy\ Static"    #打开统计页面的认证功能
    stats auth byq:byq123         #进入统计页面所使用的账号byq和密码byq123
    stats auth administrator:administrator
    stats admin if TRUE            #条件满足时进入管理级别
frontenddz    #前端设置
      bind *:80    #监听在80端口
      acl url_staticpath_beg-i/data /static /images /javascript /stylesheets    #url开头为这些的静态内容
      acl url_staticpath_end-i.jpg .gif .png .css .js .html .ico    #url结尾带为这些的静态内容
      use_backend staserif url_static    #如果静态内容符合url_static的条件,就调度到staser中的服务器
      default_backend             dyser    #其他默认调度到dyser中的服务器
backend dyser    #后端动态内容服务器设置
   cookie srv insert nocache
      balance   roundrobin
      server      node3       172.18.10.10:80 check
      server      node4       172.18.10.11:80 check
backend staser    #后端静态内容服务器设置
    balance   roundrobin
      server      node2       172.18.249.57:80 check
   
# service haproxy start
修改node2、node3、node4的http配置文件,让他们能收到真实访问的ip
# vim /etc/httpd/conf/httpd.conf
#修改一下内容
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
# service httpd restart
#node3和node4执行同样的操作  




页: [1]
查看完整版本: haproxy实现discuz论坛的动静分离和负载均衡