디버그 대상의 용기에 도구를 넣지 않고 tcpdump/strace 등의 쓰기를 사용합니다

9330 단어 Docker

개시하다


Docker로 그런 용기와 이런 용기를 이동하면 잘 돌아가지 못할 것 같아서 디버깅을 위해 tcpdump와strace 등의 도구를 사용하는 경우는 드물다.
이럴 때 디버깅 대상의 용기에 도구를 잠시 설치하면 간단하고 이해하기 쉬우나 디버깅 대상의 용기를 더럽히지 않도록 한다.
Docker의 용기 분리는 리눅스 자원의 명칭 공간의 분리라고 할 수 있으며, 반대로 같은 명칭 공간을 공유하면 디버깅용 인접 용기에서 디버깅 대상의 용기의 네트워크와 과정의 상태를 관찰할 수 있다.
또한 docker build도 표준 입력에서 Docker file을 받을 수 있기 때문에, 나는 쓰기 디버깅용 컨테이너를 빠르게 불러내려고 했다.

TL;DR


결론만 알고 싶은 사람은 이쪽을 보세요.<target>의 위치는 디버그 대상의 용기 이름이나 용기 ID로 다시 읽으십시오.
tcpdump
echo 'FROM alpine\nRUN apk add --no-cache tcpdump' \
 | docker build -t debug -f - . \
 && docker run -it --rm --net container:<target> debug tcpdump -nn -X port 80
strace
$ echo 'FROM alpine\nRUN apk add --no-cache strace' \
| docker build -t debug -f - . \
&& docker run -it --rm --pid container:<target> --cap-add sys_ptrace debug strace -fp 1
하나의 기록기로 디버깅용 도구를 설치하고 시험용 용기를 세우는 방법은 사용하고 싶은 도구를 바꾸면 다양한 응용이 효과가 있기 때문에 조개껍질의 역사와 스니 애완동물에 저장해 빠르게 부르는 것이 좋다.

해설


컨디션


작동 확인 후 손 옆에 있는 Docker의 버전은 19.03.0-rc2입니다.
$ docker version
Client: Docker Engine - Community
 Version:           19.03.0-rc2
 API version:       1.40
 Go version:        go1.12.5
 Git commit:        f97efcc
 Built:             Wed Jun  5 01:37:53 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.0-rc2
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       f97efcc
  Built:            Wed Jun  5 01:42:10 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

사전 준비


여기에 디버그 대상으로 target의 이름으로 적당한nginx 용기를 시작합니다.
$ docker run -it --rm -p 8080:80 --name target nginx:alpine

tcpdump를 시험적으로 사용하다


tcpdump만 넣은 Docker file을 만들고 debug라는 탭을 붙여서 구축합니다.
$ cat Dockerfile
FROM alpine
RUN apk add --no-cache tcpdump

$ docker build -t debug .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
 ---> 3f53bb00af94
Step 2/2 : RUN apk add --no-cache tcpdump
 ---> Using cache
 ---> a000cadec8f5
Successfully built a000cadec8f5
Successfully tagged debug:latest
docker run --network container:<name|id> 대상 용기에 연결할 수 있는 네트워크.
시험용 컨테이너를 target의 컨테이너 네트워크에 연결하고 tcpdump를 실행합니다.
$ docker run -it --rm --network container:target debug tcpdump -nn -X port 80                           tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
손 옆으로 컬을 해볼게요.
$ curl http://localhost:8080/
tcpdump로 그룹을 포착할 수 있다는 것을 알고 있습니다.
15:01:16.352790 IP 172.17.0.1.58354 > 172.17.0.2.80: Flags [S], seq 178914825, win 29200, options [mss 1460,sackOK,TS val 13258980 ecr 0,nop,wscale 7], length 0
        0x0000:  4500 003c 56ed 4000 4006 8ba9 ac11 0001  E..<V.@.@.......
        0x0010:  ac11 0002 e3f2 0050 0aaa 0609 0000 0000  .......P........
        0x0020:  a002 7210 5854 0000 0204 05b4 0402 080a  ..r.XT..........
        0x0030:  00ca 50e4 0000 0000 0103 0307            ..P.........
15:01:16.352899 IP 172.17.0.2.80 > 172.17.0.1.58354: Flags [S.], seq 1290103227, ack 178914826, win 28960, options [mss 1460,sackOK,TS val 13258980 ecr 13258980,nop,wscale 7], length 0
        0x0000:  4500 003c 0000 4000 4006 e296 ac11 0002  E..<..@.@.......
        0x0010:  ac11 0001 0050 e3f2 4ce5 69bb 0aaa 060a  .....P..L.i.....
        0x0020:  a012 7120 5854 0000 0204 05b4 0402 080a  ..q.XT..........
        0x0030:  00ca 50e4 00ca 50e4 0103 0307            ..P...P.....

Stace로 보자.


또한 strace가 설치된 Docker file을 준비하여 구성합니다.
$ cat Dockerfile
FROM alpine
RUN apk add --no-cache strace

$ docker build -t debug .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM alpine
 ---> 3f53bb00af94
Step 2/2 : RUN apk add --no-cache strace
 ---> Using cache
 ---> b357653376b3
Successfully built b357653376b3
Successfully tagged debug:latest
strace의 경우 docker run --pid container:<name|id>에 객체 컨테이너에 연결된 PID 이름 공간입니다.
어쨌든 포토샵을 하면 절차를 알 수 있어요.
$ docker run -it --rm --pid container:target debug ps
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process nginx -g daemon off;
    6 101       0:00 nginx: worker process
    7 101       0:00 nginx: worker process
    8 101       0:00 nginx: worker process
    9 101       0:00 nginx: worker process
   16 root      0:00 ps
strace만 시작하면 권한 오류가 발생합니다.
$ docker run -it --rm --pid container:target debug strace -fp 1
strace: attach: ptrace(PTRACE_SEIZE, 1): Operation not permitted
Docker는 기본적으로 일부 특권 조작을 허용하지 않습니다. strace는 명확한 허가가 필요합니다. docker run --cap-add sys_ptrace
$ docker run -it --rm --pid container:target --cap-add sys_ptrace debug strace -fp 1
strace: Process 1 attached
rt_sigsuspend([], 8
이번에 strace가 성공적으로 시작되었다.nginx -s reload로 과정에 신호를 보내 보세요.
$ docker exec -it target nginx -s reload
2019/07/13 15:26:45 [notice] 87#87: signal process started
strace를 통해 시스템 호출 추적을 할 수 있습니다.
strace: Process 1 attached
rt_sigsuspend([], 8)                    = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGHUP {si_signo=SIGHUP, si_code=SI_USER, si_pid=87, si_uid=0} ---
clock_gettime(CLOCK_REALTIME, {tv_sec=1563031605, tv_nsec=559042300}) = 0
rt_sigreturn({mask=[HUP INT QUIT USR1 USR2 ALRM TERM CHLD WINCH IO]}) = -1 EINTR (Interrupted system call)
clock_gettime(CLOCK_REALTIME, {tv_sec=1563031605, tv_nsec=559133400}) = 0
clock_gettime(CLOCK_REALTIME, {tv_sec=1563031605, tv_nsec=559208800}) = 0
clock_gettime(CLOCK_REALTIME, {tv_sec=1563031605, tv_nsec=559260400}) = 0
uname({sysname="Linux", nodename="976678564bed", ...}) = 0
open("/etc/nginx/nginx.conf", O_RDONLY) = 3

(참조) 디버깅용 이미지 nicolaka/netshoot


디버깅을 위한 도구를 설치할 때마다 번거롭다면 디버깅을 할 때 자주 사용하는 지령을 모두 넣는 니콜라/netshot의 Docker라는 인상을 준다.
안에 Docker file이 들어 있습니다.
  • apache2-utils
  • bash
  • bind-tools
  • bird
  • bridge-utils
  • busybox-extras
  • calicoctl
  • conntrack-tools
  • ctop
  • curl
  • dhcping
  • drill
  • ethtool
  • file
  • fping
  • iftop
  • iperf
  • iproute2
  • iptables
  • iptraf-ng
  • iputils
  • ipvsadm
  • libc6-compat
  • liboping
  • mtr
  • net-snmp-tools
  • netcat-openbsd
  • netgen
  • nftables
  • ngrep
  • nmap
  • nmap-nping
  • openssl
  • py-crypto
  • py2-virtualenv
  • python2
  • scapy
  • socat
  • strace
  • tcpdump
  • tcptraceroute
  • util-linux
  • vim
  • 그나저나 README에 붙어 있는 어느 층에 어떤 도구가 필요한지 조사하는 이미지의 원형이 바로 이것입니다.이 사이트는 정말 대단하다.
    참조 소스: http://www.brendangregg.com/linuxperf.html

    안감 하나를 조립하다


    글쎄요. 업무 중에 사용하는 환경에 대해 공식적인 인상이 아닌 야량의 인상도 저촉되기 때문에 제가 자주 사용하는 것만 넣어서 사용하는 게 좋을 것 같아요.
    다만 저는 개인적으로 이미지를 유지하고 싶지 않아서 필요에 따라 구축하는 기교를 생각해봤습니다.
    docker build도 표준 입력에서 Docker file을 수신할 수 있기 때문에, 하나의 기록기로 tcpdump의 예를 쓰면 이런 느낌입니다.
    $ echo 'FROM alpine\nRUN apk add --no-cache tcpdump' \
     | docker build -t debug -f - . \
     && docker run -it --rm --net container:target debug tcpdump -nn -X port 80
    
    Sending build context to Docker daemon  2.607kB
    Step 1/2 : FROM alpine
     ---> 3f53bb00af94
    Step 2/2 : RUN apk add --no-cache tcpdump
     ---> Using cache
     ---> a000cadec8f5
    Successfully built a000cadec8f5
    Successfully tagged debug:latest
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
    
    
    strace가 이런 느낌인가?
    $ echo 'FROM alpine\nRUN apk add --no-cache strace' \
    | docker build -t debug -f - . \
    && docker run -it --rm --pid container:target --cap-add sys_ptrace debug strace -fp 1
    
    Sending build context to Docker daemon  2.607kB
    Step 1/2 : FROM alpine
     ---> 3f53bb00af94
    Step 2/2 : RUN apk add --no-cache strace
     ---> Using cache
     ---> b357653376b3
    Successfully built b357653376b3
    Successfully tagged debug:latest
    strace: Process 1 attached
    rt_sigsuspend([], 8
    

    끝말


    하나의 기록기로 디버깅용 도구를 설치하고 시험용 용기를 세우는 방법은 사용하고 싶은 도구를 바꾸면 다양한 응용이 효과가 있기 때문에 조개껍질의 역사와 스니 애완동물에 저장해 빠르게 부르는 것이 좋다.
    이렇게 하면 그런 용기와 이런 용기를 무한정 조정할 수 있다.

    좋은 웹페이지 즐겨찾기