apache 如何以编程方式调整 mod_jk 负载均衡器配置中的禁用指令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/766891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:30:46  来源:igfitidea点击:

How to programmatically adjust the disable directive in the mod_jk load balancer configuration?

javaapachetomcatmod-jk

提问by Geo

We have a setup where we have one httpd (apache) with mod_jk talking in a load balance setup to three tomcat servers. We have to recycle each tomcat instance envery three hours. So tomcat1 will restart at 1, and tomcat2 at 2 and ... until tomcat1 recycles again at 4.

我们有一个设置,其中我们有一个带有 mod_jk 的 httpd (apache) 在负载平衡设置中与三个 tomcat 服务器通信。我们必须每三个小时回收每个 tomcat 实例。所以 tomcat1 会在 1 点重启,tomcat2 会在 2 点重启,......直到 tomcat1 在 4 点再次循环。

We want to configure a script or a type of program to disable the worker node that is going through a recylce to minimize session errors at the user using our application.

我们想配置一个脚本或一种程序来禁用正在通过回收的工作节点,以最大限度地减少使用我们应用程序的用户的会话错误。

Any suggestions.

有什么建议。

采纳答案by Geo

Chris thanks or your answer. I am sure it will work, but I wanted to trigger the change at run time, even though the graceful restart is very similar. I was able to accomplish my describe task the following way.

克里斯谢谢或您的回答。我确信它会起作用,但我想在运行时触发更改,即使正常重启非常相似。我能够通过以下方式完成我的描述任务。

In your httpd.conf file you should add the following lines to enable the jkmanager for mod_jk module.

在您的 httpd.conf 文件中,您应该添加以下几行以启用 mod_jk 模块的 jkmanager。

<Location /jkmanager/>
JkMount jkstatus
order deny,allow
allow from <your ip address>
allow from 127.0.0.1
deny from all
</Location>

<IfModule mod_jk.c>
...
JkMount  /jkmanager/* jkstatus
...
</IfModule>

The changes on the "workers.properties" file are:

“workers.properties”文件的变化是:

 worker.list=router,tomcat1,tomcat2,...,tomcatn,jkstatus
 worker.jkstatus.type=status

After these changes are done, you are able to see the jkmanager by typing your url followed by /jkmanager/ at the end. You should get something similar to the following picture.

完成这些更改后,您可以通过输入您的 url 并在末尾输入 /jkmanager/ 来查看 jkmanager。你应该得到类似于下图的东西。

jkmanager screenshot

jkmanager 截图

In order to disable workers at run time just run the following URLs against the jkmanger. You can even read status in an xml format.

为了在运行时禁用工作程序,只需针对 jkmanger 运行以下 URL。您甚至可以读取 xml 格式的状态。

To disable tomcat1 just hit:

要禁用 tomcat1 只需点击:

http://your.web.server/jkmanager/?cmd=update&w=router&opt=256&from=list&att=vwa&val0=1&val1=0&val2=0  

To enable tomcat1 back hit:

要启用 tomcat1 回击:

http://your.web.server/jkmanager/?cmd=update&w=router&opt=256&from=list&att=vwa&val0=0&val1=0&val2=0

I posted a complete article in my blog explaining the setup in case someone needs to know.

我在我的博客中发布了一篇完整的文章,解释了设置,以防有人需要知道。

Cloud Computing Blog

云计算博客

回答by Chris May

mod_jk re-reads workers.properties on an "apachectl graceful", so you if your workers.properties looks like this:

mod_jk 重新读取“apachectl 优雅”上的 workers.properties,所以如果你的 workers.properties 看起来像这样:

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1, tomcat2, tomcat3

... 

You could just write a script which replaces the balanced_workers list with the ones you want, and then graceful's apache

您可以编写一个脚本,用您想要的列表替换 balance_workers 列表,然后使用优雅的 apache

Updatehere's a script to do just that, which I cobbled together from some bits I had lying around. I wouldn't suggest using it in production, but it might give you some ideas for your own version.

更新这里有一个脚本来做到这一点,我从一些我躺着的地方拼凑起来。我不建议在生产中使用它,但它可能会给你一些关于你自己版本的想法。

#!/bin/bash

# set some paths
WORKERS_PROPERTIES="./workers.properties"
APACHECTL="/usr/sbin/apache2ctl"

# what does the loadbalancer config line look like?
WORKER_LINE_START="worker.loadbalancer.balanced_workers="
# full list of workers
ALL_WORKERS="tomcat1 tomcat2 tomcat3"

# first command line arg is the worker to remove. 
remove=

# build up the new line listing the active workers
worker_line=$WORKER_LINE_START
sep=""
for worker in $ALL_WORKERS
do
  if [ ${remove} != ${worker} ]
  then
     worker_line="${worker_line}$sep $worker"
     sep=","
  fi
done

# sed hackery to replace the current line with the one we just built.
# needs gnu sed (or another one that supports in-place editing)
sed -i.bak "s/^$WORKER_LINE_START.*$/$worker_line/" $WORKERS_PROPERTIES

# restart apache
$APACHECTL graceful

回答by Jason Shepherd

gkiragiannis, you answer was interesting, but doesn't seem to work for me. I wanted to only disable one of my subworkers at a time.

gkiragiannis,你的回答很有趣,但似乎对我不起作用。我只想一次禁用我的一个子工人。

Lets assume we are working with the 'agent-lb' load balancer.

假设我们正在使用“agent-lb”负载均衡器。

To view the worker status using this url:

要使用此 url 查看工作人员状态:

server-name/jkmanager/?cmd=list&w=agent-lb

To disable the 'agent-n1' sub worker use this url:

要禁用“agent-n1”子工作程序,请使用以下网址:

server-name/jkmanager/?cmd=update&w=agent-lb&sw=agent-n1&vwa=1

To ensure that the worker is disabled wait for the redirect to the worker status page, and look in the 'Act' field for the sub worker, 'agent-n1'

为确保工作人员被禁用,请等待重定向到工作人员状态页面,然后查看子工作人员的“Act”字段,“agent-n1”

To enable the 'agent-n1' sub worker use this url:

要启用“agent-n1”子工作程序,请使用以下网址:

server-name/jkmanager/?cmd=update&w=agent-lb&sw=agent-n1&vwa=0