다음과 같이 관리자와 함께 도커 컨테이너를 실행합니다.
도커 파일
CMD ["/run.sh"]
run.sh
#!/usr/bin/env bash
exec supervisord -n
supervisor-serf.conf
[group:job]
programs=serf,producer
[program:serf]
command=/start-serf-agent.sh
numprocs=1
autostart=true
autorestart=unexpected
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
start-serf-agent.sh
#!/bin/bash
exec serf agent --join=serf:7946 -tag role=producer
supervisor-servce.conf
[program:producer]
command=/start.sh
numprocs=1
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
start.sh
#!/bin/bash
exec /producer --project=${NAME}
생산자가 중단 된 후
producer_1 | 2016/02/29 21:59:50 [INFO] serf: EventMemberLeave: 7c4fbc80af97 172.19.0.2
producer_1 | 2016/02/29 21:59:51 INF 1 stopping
producer_1 | 2016/02/29 21:59:51 INF 1 exiting router
producer_1 | 2016-02-29 21:59:51,281 INFO exited: producer (exit status 0; expected)
producer_1 | 2016/02/29 21:59:51 [INFO] agent: Received event: member-leave
그러나 serf-agent는 컨테이너를 실행 상태로 유지합니다. 생산자가 상태 0으로 작업을 올바르게 완료하면 Docker 컨테이너를 중지하고 싶습니다. 프로세스를 한 그룹에 결합하려고 시도했지만 작동하지 않는 것 같습니다. 얘들 아, 내가 뭘 건너 뛰었 어? 도와주세요, 제발!
답변
감독자 이벤트 리스너 관련 문제를 해결했습니다 .
[program:worker]
command=/start.sh
priority=2
process_name=worker
numprocs=1
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[eventlistener:worker_exit]
command=/kill.py
process_name=worker
events=PROCESS_STATE_EXITED
kill.py
#!/usr/bin/env python
import sys
import os
import signal
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def main():
while 1:
write_stdout('READY\n')
line = sys.stdin.readline()
write_stdout('This line kills supervisor: ' + line);
try:
pidfile = open('/var/run/supervisord.pid','r')
pid = int(pidfile.readline());
os.kill(pid, signal.SIGQUIT)
except Exception as e:
write_stdout('Could not kill supervisor: ' + e.strerror + '\n')
write_stdout('RESULT 2\nOK')
if __name__ == '__main__':
main()
import sys
main issue I forgot to point to **process_name**
답변
다음은 파이썬 스크립트 대신 쉘 스크립트를 사용하고 여러 서비스를 다루며 약간의 실패로 인해 전체 감독자를 죽이는 약간 간소화 된 버전입니다.
supervisord.conf
$ cat /etc/supervisord.conf
[supervisord]
nodaemon=true
loglevel=debug
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
childlogdir=/var/log/supervisor
[program:service1]
command=/usr/sbin/service1
user=someone
autostart=true
autorestart=true
startsecs=30
process_name=service1
[program:service2]
command=/usr/sbin/service2
user=root
autostart=true
autorestart=true
startsecs=30
process_name=service2
[eventlistener:processes]
command=stop-supervisor.sh
events=PROCESS_STATE_STOPPED, PROCESS_STATE_EXITED, PROCESS_STATE_FATAL
stop-supervisor.sh
$ cat stop-supervisor.sh
#!/bin/bash
printf "READY\n";
while read line; do
echo "Processing Event: $line" >&2;
kill -3 $(cat "/var/run/supervisord.pid")
done < /dev/stdin
참고 문헌
답변
Docker에 대한 간단한 솔루션은 다음과 같습니다. 에서 다음 supervisord.conf
을 교체하십시오.
[program:something]
command = something
이것으로 :
[program:something]
command = sh -c 'something && kill 1'