大数据

如何使用supervisor监控es

如何使用supervisor监控es

0x00 前言

最近碰到ES集群因JVM崩溃而宕机次数过多,为了能第一时间快速恢复和得到通知,所以打算搭建一个异常重启和告警的运维工具。首先,调研了三个程序:systemd、monit、supervisor,其中systemd是Centos7系统自带的,稳定性很好,而且配置比较熟悉方便上手,但是不支持告警。monit,功能很强大,不仅支持进程监控,还支持进程资源使用、目录、文件等等监控,并且对系统侵入较小,进程不必从monit启动,但其告警方式不支持自定义代码。supervisor,用python写的进程监控框架,只支持前台进程监控,如果一个进程运行在后台,那么是不能使用supervisor,但其告警方式通过event/listener,可以实现自定义代码监控。

由于使用钉钉机器人进行告警,并且ES可以在前台运行,所以选取了supervisor方案。

0x01 安装supervisor

Centos7开启了epel,可以通过yum安装python。如果没开启,使用以下命令安装。

安装python3.6,epel附带的python最高只有3.6,也可以通过本地编译高版本的python。

当python安装完成,通过pip安装supervisor。

0x02 systemd管理supervisor

由于ES集群采用的单节点32GB JVM配置,所以需要对Linux进行系统设置以下4个部分,其中supervisor默认继承的配置是vm.max_map_countnofilememlock,都不会从系统配置读取,所以需要在systemd启动supervisor时设置。

设置方式和supervisrd配置如下,创建/usr/lib/systemd/system/supervisord.service文件,此时还不能启动supervisor,因为没有配置它的配置文件。

0x03 配置supervisor

创建/etc/supervisord.conf文件,supervisor的配置文件大体上分为3部分:

第一部分是supervisr服务的配置,主要设置日志、子进程启动时的系统配置、unix sock等。

第二部分是需要被启动进程的配置,主要设置主目录、重启配置、日志、环境变量等。

第三部分是事件监听器配置,同第2部分。完整的配置如下:

需要注意的几个点:

第一个minfdsminprocs,前面通过systemd设置supervisor进程的fds和mem,此处需要配置supervisor启动的进程的最小文件描述符数和最小进程数。

第二个startsecs=120,该配置表示supervisor启动es后,将es的状态保留在STARTING 120秒,之后es的状态就会进入RUNNING。该配置默认值是1s,而一般来说es启动过程至少超过20秒,所以如果采用默认设置或者停留时间过少,并且autorestart=true,当es启动报错时,supervisor会反复重启es,忽略了startretries=3,进而无法触发报警。原因在于,startsecs过短,会导致进程在STARTINGRUNNING状态反复横跳。而startretries=3触发条件是BACKOFF状态。

第三个stopsignal=TERM,es使用ctrl+c时发送的是SIGTERM信号,可以使es正常退出。supervisor通过echo_supervisord_conf命令会默认生成stopsignal=QUIT导致es无法正常退出。

第四个environment=JAVA_HOME="",有些环境下设置JAVA_HOME,无法使用es自带的JDK,而supervisor不能删除一个环境变量,可以使用将环境变量置空的方式。

0x04 编写事件通知

事件状态一共3种:

NameDescription
ACKNOWLEDGEDThe event listener has acknowledged (accepted or rejected) an event send.
READYEvent notifications may be sent to this event listener
BUSYEvent notifications may not be sent to this event listener.

When an event listener process first starts, supervisor automatically places it into the ACKNOWLEDGED state to allow for startup activities or guard against startup failures (hangs). Until the listener sends a READY\n string to its stdout, it will stay in this state.

When supervisor sends an event notification to a listener in the READY state, the listener will be placed into the BUSY state until it receives an OK or FAIL response from the listener, at which time, the listener will be transitioned back into the ACKNOWLEDGED state.1

简单理解,当事件监听器启动后,首先会处于ACKNOWLEDGED状态,当接收到READY消息,会使用readline阻塞读取supervisor发送的消息,然后处于BUSY状态,直到本次事件处理完毕。

0x05 参考

 

 

留言

您的电子邮箱地址不会被公开。 必填项已用 * 标注