본문 바로가기
Docker

Docker init 를 이용한 Defunct Processes in Docker 해결

Defunct Processes in Docker ?

Docker container를 이용하여 서비스 중인 호스트에서 아래와 같이 지속적으로 Defunct Processes(=좀비프로세스) 가 상승 하는 이슈가 발생 하였다.



아래와 같이 프로세스들이 <defunct> 상태로 유지 되고 있는 것을 볼 수있다.
사실 docker 환경과 같이 isolation되어 있는 환경이 아니라면 부모 프로세스를 kill 한다면 해결 될 수 있지만,
docker 환경에서 해당 <defunct> 상태의 ppid 는 1 이라는 pid로 해당container 가 실행되는 프로세스이다.
즉, 해당 pid를 kill 할 경우 해당  docker 도 종료 된다.
container내부에서 프로세스
[root@controller02 zabbix]# ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      4652  0.0  0.0  11780  1920 pts/1    Ss   03:18   0:00 bash
root      5246  0.0  0.0  47448  1648 pts/1    R+   03:19   0:00  \_ ps auxf
root        52  0.0  0.0  11780  1876 pts/0    Ss   03:10   0:00 bash
root        80  0.0  0.0   4356   352 pts/0    S+   03:10   0:00  \_ tail -f /dev/null
root         1  0.0  0.0  79804  2180 ?        Ss   03:09   0:00 su zabbix -s /bin/bash -c /usr/sbin/zabbix_agentd --foreground -c /etc/zabbix/zabbix_agentd.conf
zabbix      46  0.0  0.0  73676  3012 ?        Ss   03:09   0:00 /usr/sbin/zabbix_agentd --foreground -c /etc/zabbix/zabbix_agentd.conf
zabbix      47  0.0  0.0  73676  1380 ?        S    03:09   0:00  \_ /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix      48  0.0  0.0  73800  2368 ?        S    03:09   0:00  \_ /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix      49  0.0  0.0  73800  2368 ?        S    03:09   0:00  \_ /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix      50  0.0  0.0  73800  2368 ?        S    03:09   0:00  \_ /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix      51  0.0  0.0  73808  2148 ?        S    03:09   0:00  \_ /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
zabbix    1380  0.2  0.0      0     0 ?        Z    03:12   0:00 [openstack] <defunct>
zabbix    1388  0.2  0.0      0     0 ?        Z    03:12   0:00 [openstack] <defunct>
zabbix    1390  0.0  0.0      0     0 ?        Z    03:12   0:00 [wc] <defunct>
zabbix    2436  0.3  0.0      0     0 ?        Z    03:14   0:00 [openstack] <defunct>
zabbix    2452  0.3  0.0      0     0 ?        Z    03:14   0:00 [openstack] <defunct>
zabbix    2460  0.2  0.0      0     0 ?        Z    03:14   0:00 [openstack] <defunct>
zabbix    2462  0.0  0.0      0     0 ?        Z    03:14   0:00 [wc] <defunct>
zabbix    3540  0.5  0.0      0     0 ?        Z    03:16   0:00 [openstack] <defunct>
zabbix    3541  0.0  0.0      0     0 ?        Z    03:16   0:00 [wc] <defunct>
zabbix    3556  0.5  0.0      0     0 ?        Z    03:16   0:00 [openstack] <defunct>
zabbix    3558  0.0  0.0      0     0 ?        Z    03:16   0:00 [wc] <defunct>
zabbix    4602  1.6  0.0      0     0 ?        Z    03:18   0:01 [openstack] <defunct>
zabbix    4603  0.0  0.0      0     0 ?        Z    03:18   0:00 [grep] <defunct>
zabbix    4618  1.5  0.0      0     0 ?        Z    03:18   0:00 [openstack] <defunct>

Docker init

UNP에서는 아래와 같은 이야기가 있다.
If a process terminates, and that process has childern in the zombie state, the parent process ID of all zombie childern is set to 1(the init process), which will inherit the childern and clean them up(i.e, init will wait for them, which removes the zombie)

즉 , zobmie인 상태의 자식을 거두지 않고 부모가 죽으면 자식 ppid가 init으로 바꿔게 되며, 
Init프로세스가zombie상태인 자식들을 wait를 통해 거둬 줄 것이다. 이말은 init이 wait 를 통하여 zombie를 지워 준다는 것이다.


그렇다면 해당 container안에 init프로세스를 생성 시키면 해결 될것 같다 .
위와같은 문제로 docekr 에서는  tini 기반으로 init  프로세스를 docker 내부에 실행 시켜서서 위 문제점을 해결 할 수 있도록 도와준다.

 위와 같이  -- init 옵션을 추가 하여 실행 할 경우 아래와 같이 pid 1 이  /dev/init 으로 실행 되며 기존의 pid 1 로 실행 되던 프로세스가  pid 5 로 실행 되는 것을 볼 수 있다.
init 추가한 container내부에서 프로세스
[root@controller01 zabbix]# ps auxf
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     21790  0.1  0.0  11780  1876 pts/0    Ss   03:20   0:00 bash
root     21829  0.0  0.0  47448  1644 pts/0    R+   03:20   0:00  \_ ps auxf
root         1  0.0  0.0   1100     4 ?        Ss   May25   0:01 /dev/init -- docker-entrypoint.sh
root         5  0.0  0.0  79804  2176 ?        S    May25   0:00 su zabbix -s /bin/bash -c /usr/sbin/zabbix_agentd --foreground -c /etc/zabbix/zabbix_agentd.conf
zabbix      50  0.0  0.0  73676  3016 ?        Ss   May25   0:00  \_ /usr/sbin/zabbix_agentd --foreground -c /etc/zabbix/zabbix_agentd.conf
zabbix      51  0.0  0.0  73676  1384 ?        S    May25   0:11      \_ /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix      52  0.0  0.0  73800  2428 ?        S    May25   0:31      \_ /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix      53  0.0  0.0  73800  2428 ?        S    May25   0:30      \_ /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix      54  0.0  0.0  73800  2428 ?        S    May25   0:31      \_ /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix      55  0.0  0.0  73808  2152 ?        S    May25   0:03      \_ /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]

그렇기 때문에 위 문제점을 해결 할 수 있도록 해당 프로세스가 wait를 통하여 zombie 를 지워 질 수 있도록  하여 더이상  좀비 프로세스가 상승 하는 것을 찾을 수 없게 되었다.
Ref

https://github.com/krallin/tini


반응형