Systemd HTTP健康检查 [英] Systemd http health check

查看:318
本文介绍了Systemd HTTP健康检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Redhat 7.1上有一项服务,该服务使用systemctl启动,停止,重新启动和状态来控制.有一次,systemctl状态恢复为活动状态,但是该服务背后"的应用程序响应的HTTP代码不同于200.

我知道我可以使用Monit或Nagios进行检查并重新启动systemctl-但我想知道在使用systemd时默认情况下是否存在某些东西,因此我不需要安装其他工具./p>

我的首选解决方案是,如果http返回码与200完全不同,而没有systemd本身以外的其他工具,则我的服务将自动重新启动-(并且可能会通知Hipchat房间或发送电子邮件...)

我尝试过搜索该主题-不走运.请帮忙:-)

解决方案

简短答案

systemd具有本机(基于套接字)的运行状况检查方法,但它不是基于HTTP的.您可以编写一个填充程序,该填充程序通过HTTP轮询状态,然后将其转发给本机机制.


长答案

systemd世界中的正确选择是使用sd_notify套接字机制在应用程序完全可用时通知init系统.将Type=notify用于您的服务以启用此功能.

您可以直接使用 sd_notify()写入此套接字.调用,或者您可以检查NOTIFY_SOCKET环境变量以获取名称,并在应用程序返回200s时让自己的代码将READY=1写入该套接字.

如果要将其放到一个单独的进程中,该进程通过HTTP轮询您的进程,然后将其写入套接字,则可以执行此操作-确保NotifyAccess设置正确(默认情况下,仅主进程该服务被允许写入套接字.


由于您有兴趣检测应用程序在完全初始化后失败并触发重新启动的情况,因此在这种情况下sd_notify套接字同样适用:

发送WATCHDOG_USEC=...设置成功测试之间允许的时间,然后在成功进行自测时设置WATCHDOG=1.只要在配置的时间内未成功测试,您的服务就会重新启动.

I have a service on a Redhat 7.1 which I use systemctl start, stop, restart and status to control. One time the systemctl status returned active, but the application "behind" the service responded http code different from 200.

I know that I can use Monit or Nagios to check this and do the systemctl restart - but I would like to know if there exist something per default when using systemd, so that I do not need to have other tools installed.

My preferred solution would be to have my service restarted if http return code is different from 200 totally automatically without other tools than systemd itself - (and maybe with a possibility to notify a Hipchat room or send a email...)

I've tried googling the topic - without luck. Please help :-)

解决方案

The Short Answer

systemd has a native (socket-based) healthcheck method, but it's not HTTP-based. You can write a shim that polls status over HTTP and forwards it to the native mechanism, however.


The Long Answer

The Right Thing in the systemd world is to use the sd_notify socket mechanism to inform the init system when your application is fully available. Use Type=notify for your service to enable this functionality.

You can write to this socket directly using the sd_notify() call, or you can inspect the NOTIFY_SOCKET environment variable to get the name and have your own code write READY=1 to that socket when the application is returning 200s.

If you want to put this off to a separate process that polls your process over HTTP and then writes to the socket, you can do that -- ensure that NotifyAccess is set appropriately (by default, only the main process of the service is allowed to write to the socket).


Inasmuch as you're interested in detecting cases where the application fails after it was fully initialized, and triggering a restart, the sd_notify socket is appropriate in this scenario as well:

Send WATCHDOG_USEC=... to set the amount of time which is permissible between successful tests, then WATCHDOG=1 whenever you have a successful self-test; whenever no successful test is seen for the configured period, your service will be restarted.

这篇关于Systemd HTTP健康检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆