我们目前正在使用 Apache2.4作为三台 Tomcat 服务器的多主集群的负载均衡器。我们可以使用 Web 浏览器访问此负载均衡器的内部 IP 地址,并动态更改设置(例如http://xx.xx.xx.xx/balancer-manager)。这很有效,我们对此很满意。
启用的模块:
- proxy.load(用于配置proxy.conf)
- proxy_ajp.load
- proxy_balancer.load(用于配置proxy_balancer.conf)
但现在我们将在此 Apache 上托管多个(此设置中为三个)虚拟主机,每个虚拟主机本身代表三个 Tomcat 集群。每个虚拟主机都可通过以下 URL 访问:http://customer.company.tld/app/ui)。 所以每个集群的路径相同!现在我们面临两个问题:
- 这一页平衡器管理器只能通过其虚拟主机访问。因此,我们必须通过随机选择的内部主机名(添加到 ServerAlias)访问平衡器管理器,并将此主机名也添加到/etc/hosts关闭我们的内部计算机才能使用它。
- 此外,我们必须为每个虚拟主机(~cluster)执行此操作。但我们想要一个页面平衡器管理器它呈现了所有虚拟主机和其背后的集群。
以下是一些示例配置:
在/etc/apache2/site-enabled/foo 中# 我们实际上有三个:foo、bar、baz
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName customer-foo.company.tld
ServerAlias customer-foo-balancer customer-foo.company.tld www.customer-foo.company.tld
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
deny from all
</Directory>
<Directory /var/app/app_static>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/app_static_res>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# load balancer
<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
Deny from all
# allows for internal access
Allow from 127.0.0.1 ::1 10.1.21.81 10.1.4.9
Satisfy all
</Location>
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
<Proxy balancer://htg>
ProxySet failonstatus=503
BalancerMember ajp://10.171.23.120:8010/app lbset=0 route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8010/app lbset=0 route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8010/app lbset=0 route=foo003 loadfactor=20
</Proxy>
<Proxy balancer://htgservice>
ProxySet failonstatus=503
BalancerMember ajp://10.171.23.120:8011/wcs_service route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8011/wcs_service route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8011/wcs_service route=foo003 loadfactor=20
</Proxy>
ProxyPass /app balancer://foo stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app balancer://foo
ProxyPass /app_service balancer://fooservice stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app_service balancer://fooservice
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
过去,我们在不同的文件 proxy.conf 和 proxy_balancer.conf 中配置了平衡,这应该允许通过内部 IP 进行访问,并在单个页面上显示所有集群和集群成员。但这不再起作用了,代理的配置似乎只接受路径(例如应用程序和应用服务) 而不是 URL 或主机名。我们不能也不会更改路径。因此我们将代理配置移到了虚拟主机内。
感谢您的帮助!
答案1
那么如何拆分配置呢?将平衡器定义放在任何虚拟主机之外,然后删除上下文:
<Proxy balancer://htg>
ProxySet failonstatus=503
BalancerMember ajp://10.171.23.120:8010 lbset=0 route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8010 lbset=0 route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8010 lbset=0 route=foo003 loadfactor=20
</Proxy>
<Proxy balancer://htgservice>
ProxySet failonstatus=503
BalancerMember ajp://10.171.23.120:8011 route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8011 route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8011 route=foo003 loadfactor=20
</Proxy>
在您的虚拟主机中引用这些平衡器:
ProxyPass /app balancer://htg/app stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app balancer://htg/app
ProxyPass /app_service balancer://htgservice/wtg_service stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app_service balancer://htgservice/wtg_service
要访问您的平衡器管理器,请
<Location /balancer-manager>
在其中创建一个额外的管理虚拟主机。
这样,您就拥有一个定义所有平衡器的地方和一个用于访问平衡器管理器的虚拟主机,该平衡器管理器会显示您的所有平衡器。
答案2
谢谢 Christian!Christian 的解答是正确的!
基本上,你必须将 ProxyPass 和 ProxyPassReverse 放入虚拟主机的配置中,并在代理配置文件。正如 Christian 在其评论中所述,需要使用代理集-Proxy-Directive 中的指令用于配置粘性会话- 它无法与 ProxyPass 配合使用。下面是一个简短但完整的示例:
示例 VirtualHost (foo、bar、baz 各一个):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName customer-foo.company.tld
ServerAlias customer-foo-balancer customer-foo.company.tld www.customer-foo.company.tld
DocumentRoot /var/www/foo
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
deny from all
</Directory>
<Directory /var/www/foo/wcs_static>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/foo/wcs_static_res>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# maintenance
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/wcs_static_res/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/wcs_static_res/maintenance.html
RewriteCond %{REQUEST_FILENAME} !/wcs_static_res/maintenance-time.png
RewriteCond %{REMOTE_HOST} !^10\.1\.0\.203
RewriteRule ^.*$ /wcs_static_res/maintenance.html [L]
# redirect, SSL itself is handled by another server in front of this loadbalancer
RedirectMatch (.*)/$ https://customer-foo.company.tld/wcs/ui
# load balancer
# web-frontend
ProxyPass /wcs balancer://foo
# stickysession can't be set with ProxyPass in this case, see proxy.conf
ProxyPassReverse /wcs balancer://foo
# standalone-clients
ProxyPass /wcs_service balancer://fooservice
ProxyPassReverse /wcs_service balancer://fooservice
# logging
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/foo_error.log
CustomLog ${APACHE_LOG_DIR}/foo_access.log combined
</VirtualHost>
proxy.conf(只有这一个):
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost On
<Proxy balancer://foo>
# stickysessions must be defined here
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.23.120:8010/wcs lbset=0 route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8010/wcs lbset=0 route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8010/wcs lbset=0 route=foo003 loadfactor=20
</Proxy>
<Proxy balancer://fooservice>
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.23.120:8011/wcs_service route=foo001 loadfactor=40
BalancerMember ajp://10.171.23.121:8011/wcs_service route=foo002 loadfactor=40
BalancerMember ajp://10.171.23.122:8011/wcs_service route=foo003 loadfactor=20
</Proxy>
<Proxy balancer://bar>
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.21.110:8010/wcs lbset=0 route=bar001 loadfactor=40
BalancerMember ajp://10.171.21.111:8010/wcs lbset=0 route=bar002 loadfactor=40
BalancerMember ajp://10.171.21.112:8010/wcs lbset=0 route=bar003 loadfactor=20
</Proxy>
<Proxy balancer://barservice>
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.21.110:8011/wcs_service route=bar001 loadfactor=40
BalancerMember ajp://10.171.21.111:8011/wcs_service route=bar002 loadfactor=40
BalancerMember ajp://10.171.21.112:8011/wcs_service route=bar003 loadfactor=20
</Proxy>
<Proxy balancer://baz>
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.45.100:8010/wcs lbset=0 route=baz001 loadfactor=40
BalancerMember ajp://10.171.45.101:8010/wcs lbset=0 route=baz002 loadfactor=40
BalancerMember ajp://10.171.45.102:8010/wcs lbset=0 route=baz003 loadfactor=20
</Proxy>
<Proxy balancer://bazservice>
ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
BalancerMember ajp://10.171.45.100:8011/wcs_service route=baz001 loadfactor=40
BalancerMember ajp://10.171.45.101:8011/wcs_service route=baz002 loadfactor=40
BalancerMember ajp://10.171.45.102:8011/wcs_service route=baz003 loadfactor=20
</Proxy>
</IfModule>
代理平衡器.conf:
<IfModule mod_proxy_balancer.c>
<IfModule mod_status.c>
<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
Deny from all
# store here your internal IPs for direct access
Allow from 127.0.0.1 ::1
Satisfy all
</Location>
</IfModule>
现在您可以通过http://ipofloadbalancer.com/balancer-manager代理服务器后面的所有服务器。无需破解/etc/hosts在客户端和负载均衡器上。缺点是,尤其是使用 Apache 2.4 时,界面很快就会变得混乱,您必须滚动很多次。