1ã³ã³ããä¸ã§ãnginxã¨fluentdãåããã話ã
ã©ããã¦è¤æ°ããã»ã¹ãèµ·åãã¦ã¯ãããªãã®
Dockerã³ã³ããã§ã¯ããã»ã¹ã1ã¤ã ãèµ·åããããã¨ã¯æåãªContainerãã¹ããã©ã¯ãã£ã¹ã§ãããããã¯ã©ããã¦ã§ããããããããçç±ãããã¨æãã¾ãããæ確ãªæè¡ççç±ã¨ãã¦ãDockerã§ã¯CMDãªãã·ã§ã³ãENTRYPOINTãªãã·ã§ã³ã§èµ·åããããã»ã¹ãã³ã³ããä¸ã®PID1ã«ãªãããã ãã¨æã£ã¦ãã¾ãã
試ãã«ãnginxã®ã³ã³ãããèµ·åãã¦ããã»ã¹ã確èªãã¦ã¿ãã¨ãPID1ã¯ãnginxã®master processã¨ãªã£ã¦ãã¾ãã
$ docker run --rm -d --name nginx nginx $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d4d0754f57a nginx "nginx -g 'daemon ofâ¦" 6 seconds ago Up 4 seconds 80/tcp nginx $ docker exec -it 1d4d0754f57a bash root@1d4d0754f57a:/# apt update && apt install -y procps root@1d4d0754f57a:/# ps -aef UID PID PPID C STIME TTY TIME CMD root 1 0 0 06:28 ? 00:00:00 nginx: master process nginx -g daemon off; nginx 6 1 0 06:28 ? 00:00:00 nginx: worker process root 427 0 0 06:35 pts/0 00:00:00 bash root 676 427 0 06:37 pts/0 00:00:00 ps -aef
ã¤ã¾ãinitããã°ã©ã ãããããSupervisorã®æ©è½ããªãã®ã§ãè¤æ°ããã»ã¹ãèµ·åãããå ´åã«ã¯UNIXã®ä¸çã§ã®ã«ã¼ã«éåã«ãªã£ã¦ãã¾ããã¨ã以ä¸ã®ããã°ã§è©³ãã説æãã¦ããã¦ãã¾ãã
Docker and the PID 1 zombie reaping problem
ãã®ããããåãããããè¨è¿°ã§ããããã
Let's look at a concrete example. Suppose that your container contains a web server that runs a CGI script that's written in bash. The CGI script calls grep. Then the web server decides that the CGI script is taking too long and kills the script, but grep is not affected and keeps running. When grep finishes, it becomes a zombie and is adopted by the PID 1 (the web server). The web server doesn't know about grep, so it doesn't reap it, and the grep zombie stays in the system.
ã¤ã¾ããPID1ãinitãããªãããã親ããã»ã¹ã殺ãããå«ããã»ã¹ã¯ã¾ã³ãããã»ã¹ã«ãªã£ã¦ãã¾ãã¾ããã¾ããinitããªãã®ã«ãããã»ã¹ãforkãspawnãã¾ããã®ã¯å±éºã§ãããã
runitã«ã¤ãã¦
ãããè¤æ°ããã»ã¹ãèµ·åããããå ´åã«ã¯ã©ãããã°ããã®ããã¨ãã話ã§ãããã³ã³ããä¸ã§initçãªãã®ãPID1ã§åããã¦ãããã°è¯ã訳ã§ããããã«ããããã®ã¨ã㦠runit
ã¨ãããã®ãããã¾ãã
å説æãµã¤ããã¡ã
runit - a UNIX init scheme with service supervision
phusion/baseimage
ã«ã¤ãã¦
phusion/baseimage
ã¨ã¯
ã§ããã® runit
ãå梱ãã¦ãããã³ã³ããã¤ã¡ã¼ã¸ããããããã phusion/baseimage
ã§ãã
phusion/baseimage
ã¯ã runit
以å¤ã«ã以ä¸ã®ãããªãµã¼ãã¹ãã³ã³ããä¸ã§èµ·åãã¦ããã¾ãã
- Ubuntu 16.04 LTS as base system
- A correct init process
- Fixes APT incompatibilities with Docker
- syslog-ng
- The cron daemon
- An optional SSH server (disabled by default)
- Runit for service supervision and management
ã³ã³ããã¨ãããããä»®æ³ãµã¼ãã£ã¦æããã
Dockerfileæ§æ
ãã® phusion/baseimage
ãå©ç¨ãã¦ã1ã³ã³ããä¸ã§nginxã¨fluentdãåããã¦ã¿ã¾ããä¸è¨ã®ãããªã³ã³ãããä½æãã¾ãã
- nginxããã»ã¹ãèµ·å
- ä¸è¬çãªwebãµã¼ãã¼ã¨ãã¦åä½
- flutendããã»ã¹ãèµ·å
- nginxã¢ã¯ã»ã¹ãã°ãs3ã«è»¢é
folder/file structure
. âââ Dockerfile âââ service â  âââ nginx â  â  âââ run â  âââ td-agent â  âââ run âââ td-agent âââ td-agent.conf
Dockerfile
FROM phusion/baseimage:0.11 ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn ENV DEBIAN_FRONTEND=noninteractive # install nginx # https://nginx.org/en/linux_packages.html#Ubuntu RUN apt-get update && \ apt-get install -y --no-install-recommends curl=7.58.0-2ubuntu3.7 gnupg2=2.2.4-1ubuntu1.2 ca-certificates=20180409 lsb-release=9.20170808ubuntu1 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* RUN echo "deb http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" > /etc/apt/sources.list.d/nginx.list RUN curl -o nginx_signing.key -fsSL https://nginx.org/keys/nginx_signing.key && \ apt-key add nginx_signing.key RUN apt-get update && \ apt-get install -y --no-install-recommends nginx=1.14.0-0ubuntu1.2 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* RUN rm /etc/init.d/nginx EXPOSE 80 # install td-agent # https://toolbelt.treasuredata.com/sh/install-ubuntu-bionic-td-agent3.sh RUN curl -o GPG-KEY-td-agent -fsSL https://packages.treasuredata.com/GPG-KEY-td-agent && \ apt-key add GPG-KEY-td-agent RUN echo "deb http://packages.treasuredata.com/3/ubuntu/bionic/ bionic contrib" > /etc/apt/sources.list.d/treasure-data.list RUN apt-get update && \ apt-get install -y --no-install-recommends td-agent=3.4.1-0 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* COPY td-agent /etc/td-agent/ RUN rm /etc/init.d/td-agent # upload runit service script COPY service /etc/service RUN chmod 755 /etc/service/*/run
以ä¸ã¯runitã®ã¦ã¼ã¶ã¼ã¬ãã«ãµã¼ãã¹ãå®ç¾©ãã¦ãããã¡ã¤ã«ãinitã«ããããinit.dã¿ãããªãã®ãããã«ãããå¦çããèµ·åæã« runsvdir
ã¨ããããã°ã©ã ãèµ·åãã¦ããã¾ãã
service/nginx/run
#!/bin/sh exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "daemon off;"
service/td-agent/run
#!/bin/sh exec /usr/sbin/td-agent
以ä¸ã¯fluentdã®ã³ã³ãã£ã°ã
td-agent/td-agent.conf
<source> @type tail path /var/log/nginx/access.log pos_file /var/log/nginx/access.log.pos format nginx tag nginx </source> <match *> @type s3 aws_key_id XXXXXXXXXXXXXXXXXXXXXX aws_sec_key XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX s3_bucket fluentd-sample-kiritan path sample/ s3_region ap-northeast-1 time_slice_format %Y%m%d%H%M </match>
ã³ã³ããèµ·å
ãã«ããã¦èµ·åã
$ docker build -t baseimage:latest . $ docker run --rm --name baseimage -p 8000:80 baseimage:latest /sbin/my_init
ããã»ã¹ã確èªããã¨ã/sbin/my_init(runitã®svã³ãã³ããèªãã§ãå¦ç)ãPID1ã¨ãªã£ã¦ããã runsvdir
ã«ããåãµã¼ãã¹ãèµ·åãã¦ãã¾ãã
$ docker exec -it bb2d2028160b ps -ef awxf UID PID PPID C STIME TTY STAT TIME CMD root 55 0 0 13:12 pts/0 Rs+ 0:00 ps -ef awxf root 1 0 0 12:30 ? Ss 0:00 /usr/bin/python3 -u /sbin/my_init root 13 1 0 12:30 ? S 0:00 /usr/sbin/syslog-ng --pidfile /var/run/syslog-ng.pid -F --no-caps root 21 1 0 12:30 ? S 0:00 /usr/bin/runsvdir -P /etc/service root 22 21 0 12:30 ? Ss 0:00 \_ runsv sshd root 23 21 0 12:30 ? Ss 0:00 \_ runsv cron root 26 23 0 12:30 ? S 0:00 | \_ /usr/sbin/cron -f root 24 21 0 12:30 ? Ss 0:00 \_ runsv nginx root 28 24 0 12:30 ? S 0:00 | \_ nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf -g daemon www-data 31 28 0 12:30 ? S 0:00 | \_ nginx: worker process root 25 21 0 12:30 ? Ss 0:00 \_ runsv td-agent root 27 25 0 12:30 ? S 0:00 \_ /bin/sh ./run root 29 27 0 12:30 ? Sl 0:01 \_ /opt/td-agent/embedded/bin/ruby /usr/sbin/td-agent root 34 29 0 12:30 ? Sl 0:02 \_ /opt/td-agent/embedded/bin/ruby -Eascii-8bit:ascii-8bit /usr/sbin
dockerãã¹ãã®IPã確èªãã¦WEBãµã¤ãã«ã¢ã¯ã»ã¹ããã¨ã
$ docker-machine ip 192.168.99.100 $ curl http://192.168.99.100:8000
ãã°ããããå¾ãs3ä¸ã«nginxã®ã¢ã¯ã»ã¹ãã°ã転éããã¦ãã¾ãã
$ aws s3 ls s3://fluentd-sample-kiritan/sample/ 2019-05-25 10:16:01 172 201905250105_0.gz