利用haproxy 作webserver(apache)的负载均衡

天大地大妈咪最大 提交于 2020-02-28 17:13:14

1、要解决的问题 (1)后台web server要考虑扩容和负载均衡; (2)要保护后台webserver ,避免webserver 由于负载过高,造成系统不可用 2、试验环境

输入图片说明 haproxy:HA-Proxy version 1.6.0 2015/10/13 apache2 : Server version: Apache/2.4.7 (Ubuntu) Server built: Jan 14 2016 17:45:23 目的:所有访问 192.168.1.2:80 的web 请求,会通过 haproxy 均衡的分配给为 192.168.1.2:8001 192.168.101.2:8002 ,这是两个监听不同端口的apache 服务器; 3、解决思路 让所有 访问haproxy的请求都转发给,8001 和 8002 ,haprxy 要对这些请求进行调度,由8001 和8002 进行实际的处理php 文件 4、配置文件 haproxy :配置文件路径:/etc/haproxy/haproxy.cfg ``` global log 127.0.0.1 local0 #maxconn 20000
#uid 535 #uid和gid按照实际情况进行配置 #gid 520
#chroot /var/chroot/haproxy daemon nbproc 1

defaults log global mode http
option httplog #option httpclose option dontlognull #option forwardfor option redispatch retries 3 balance roundrobin maxconn 1000 stats uri /haproxy-stats timeout connect 5000 timeout client 50000 timeout server 50000

#frontend http-in

bind *:80

default_backend pool1

#backend pool1

option httpchk HEAD / HTTP/1.0

stats refresh 2

server WEB1 127.0.0.1:81 weight 3 maxconn 10000 check

server WEB2 127.0.0.1:82 weight 3 maxconn 10000 check

listen webfarm 192.168.101.129:80 mode http stats enable stats uri /haproxy?stats stats realm Haproxy\ Statistics stats auth haproxy:stats balance roundrobin cookie LBN insert indirect nocache option httpclose option forwardfor server web01 192.168.101.129:8001 cookie node1 check server web02 192.168.101.129:8002 cookie node2 check

   haproxy 操作命令:service haproxy start/stop/restart
  
 apache 配置:
   /etc/apache2/ports.conf 增加监听8001 和 8002 注释掉监听80,因为haproxy 要监听80
    ```
#Listen 80
Listen 8001
Listen 8002

<IfModule ssl_module>
    Listen 443 
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443 
</IfModule>
/etc/apache2/sites-available/8001.conf  主要是端口和web目录,web目录下只有一个php 文件
<VirtualHost *:8001>
  # The ServerName directive sets the request scheme, hostname and port that
  # the server uses to identify itself. This is used when creating
  # redirection URLs. In the context of virtual hosts, the ServerName
  # specifies what hostname must appear in the request's Host: header to
  # match this virtual host. For the default virtual host (this file) this
  # value is not decisive as it is used as a last resort host regardless.
  # However, you must set it for any further virtual host explicitly.
  #ServerName www.example.com

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/html/8001/

  # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  # error, crit, alert, emerg.
  # It is also possible to configure the loglevel for particular
  # modules, e.g.
  #LogLevel info ssl:warn

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/8001_access.log combined

  # For most configuration files from conf-available/, which are
  # enabled or disabled at a global level, it is possible to
  # include a line for only one particular virtual host. For example the
  # following line enables the CGI configuration for this host only
  # after it has been globally disabled with "a2disconf".
  #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

/etc/apache2/sites-available/8002.conf 与8001 类似,除了端口和web目录不同

启动8001 和8002 :sudo a2ensite 8801 ;sudo a2ensite 8002 重启apache :sudo service apache2 restart

验证步骤: (1)从另外一台机器使用 ab -c 10 -n 1000 http://192.168.1.2/,待测试结束后,查看access_log,可以看到相应的增加 (2)调整haproxy.conf 中的maxconn 与ab -c 10 并发数,可以看到这个maxconn 会起到限流作用 比如:maxconn 设置为3 则 ab -c 10 需要花费较长时间完成 若maxconn 为20 ,则 ab -c 10 则较快即可完成 5、主要参考资料 https://linux.cn/article-4765-1.html#2_3510 https://cbonte.github.io/haproxy-dconv/configuration-1.5.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!