反向代理负载均衡系列之Apache

反向代理负载均衡
时间:2016.10.17
笔者Q:552408925、572891887
架构师群:471443208
bjstack运维社区:524721466

1.反向代理概述

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

环境准备:

主机名 IP地址 角色 系统
web-node1.com eth0:192.168.90.201 web-node1节点 CentOS7.2
web-node2.com eth0:192.168.90.202 web-node2节点 CentOS7.2
lb-node1.com eth0:192.168.90.203 Apache反向代理 CentOS7.2

2.Node节点部署

在两台web-node节点中均使用Yum安装一个Apache用于做真实机,监听8080端口

web-node1.com部署

  1. [root@web-node1 ~]# rpm -ivh \
  2. http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
  3. [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
  4. ##部署web-node1 httpd服务
  5. [root@web-node1 ~]# yum install -y httpd
  6. [root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
  7. [root@web-node1 ~]# systemctl start httpd
  8. [root@web-node1 ~]# echo "web-node1.com" > /var/www/html/index.html
  9. [root@web-node1 ~]# curl http://192.168.90.201:8080/
  10. web-node1.com

web-node2.com部署

  1. [root@web-node1 ~]# rpm -ivh \
  2. http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
  3. [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
  4. ##部署web-node2 httpd服务
  5. [root@web-node2 ~]# yum install -y httpd
  6. [root@web-node2 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
  7. [root@web-node2 ~]# systemctl start httpd
  8. [root@web-node2 ~]# echo "web-node2.com" > /var/www/html/index.html
  9. [root@web-node2 ~]# curl http://192.168.90.202:8080/
  10. web-node2.com

3.反向代理部署

Apache 源码编译安装,并监听80端口

  1. [root@lb-node1 ~]# yum install -y apr-devel apr-util-devel pcre-devel openssl-devel
  2. [root@lb-node1 ~]# cd /usr/local/src
  3. [root@lb-node1 src]# wget http://www-eu.apache.org/dist/httpd/httpd-2.4.23.tar.gz
  4. [root@lb-node1 src]# tar xf httpd-2.4.23.tar.gz
  5. [root@lb-node1 src]# cd httpd-2.4.23
  6. [root@lb-node1 httpd-2.4.23]# ./configure --prefix=/usr/local/httpd-2.4.23 --enable-so --enable-modules="all"
  7. [root@lb-node1 httpd-2.4.23]# make && make install
  8. [root@lb-node1 httpd-2.4.23]# ln -s /usr/local/httpd-2.4.23/ /usr/local/httpd
  9. ## 测试配置并启动Apache
  10. [root@lb-node1 ~]# sed -i 's@#ServerName www.example.com:80@ServerName 192.168.90.203:80@g' /usr/local/httpd/conf/httpd.conf
  11. [root@lb-node1 ~]# /usr/local/httpd/bin/apachectl -t
  12. Syntax OK
  13. [root@lb-node1 ~]# /usr/local/httpd/bin/apachectl -k start

3.1Apache配置反向代理

1.在/usr/local/httpd/conf/httpd.conf配置引用proxy配置文件

  1. Include conf/extra/httpd-proxy.conf

2.配置proxy反向代理

  1. [root@linux-node1 ~]# cat /usr/local/httpd/conf/extra/httpd-proxy.conf
  2. LoadModule proxy_module modules/mod_proxy.so
  3. LoadModule proxy_connect_module modules/mod_proxy_connect.so
  4. LoadModule proxy_http_module modules/mod_proxy_http.so
  5. LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
  6. LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
  7. LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
  8. LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
  9. LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
  10. ProxyRequests Off
  11. <Proxy balancer://web-cluster>
  12. BalancerMember http://192.168.90.201:8080 loadfactor=1
  13. BalancerMember http://192.168.90.202:8080 loadfactor=2
  14. </Proxy>
  15. ProxyPass /biaoganxu balancer://web-cluster
  16. ProxyPassReverse /biaoganxu balancer://web-cluster
  17. <Location /manager>
  18. SetHandler balancer-manager
  19. Order Deny,Allow
  20. Allow from all
  21. </Location>

3.重载Apache服务

  1. [root@lb-node1 ~]# /usr/local/httpd/bin/apachectl -k graceful

4.测试反向代理

  1. [root@lb-node1 ~]# curl http://192.168.90.203/biaogan/
  2. web-node1.com
  3. [root@lb-node1 ~]# curl http://192.168.90.203/biaogan/
  4. web-node2.com
  5. [root@lb-node1 ~]# curl http://192.168.90.203/biaogan/
  6. web-node2.com
  7. [root@lb-node1 ~]# curl http://192.168.90.203/biaogan/
  8. web-node1.com

5.使用HTTP访问Apache管理页面

访问http://192.168.90.203/manager

Apache

Apache管理页面

3.2APache配置文件详解

proxy代理配置文件注释

  1. #proxy模块
  2. LoadModule proxy_module modules/mod_proxy.so
  3. #链接模块
  4. LoadModule proxy_connect_module modules/mod_proxy_connect.so
  5. #http代理模块
  6. LoadModule proxy_http_module modules/mod_proxy_http.so
  7. #负载均衡模块
  8. LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
  9. #算法默认是byrequest,可以是bytraffic或者bybusyness
  10. #算法模块,根据server的请求量
  11. LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
  12. #算法模块,根据server流量
  13. LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
  14. #算法模块,根据server繁忙
  15. LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
  16. LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
  17. ProxyRequests Off
  18. #LB集群组名称
  19. <Proxy balancer://web-cluster>
  20. #node节点并设置权重(可很多)
  21. BalancerMember http://192.168.90.201:8080 loadfactor=1
  22. BalancerMember http://192.168.90.202:8080 loadfactor=2
  23. </Proxy>
  24. #跳转至LB集群组名称,交由后端WEB节点处理
  25. ProxyPass /biaogan balancer://web-cluster
  26. ProxyPassReverse /biaogan balancer://web-cluster
  27. # Apache管理页面
  28. <Location /manager>
  29. SetHandler balancer-manager
  30. Order Deny,Allow
  31. Allow from all
  32. </Location>

笔者Q:552408925、572891887
架构师群:471443208
bjstack运维社区:524721466