Sysdig容器监控

sysdig 与容器

容器内进程和宿主机操作系统共享内核,这意味着你能直接在宿主机上观察到容器内进程的运行及资源使用。但目前来说我们常用的ps、top等工具还不能很好地区分这些进程,而sysdig则做到了这点,并做得很全面。

简而言之,sysdig使用了一个能够捕获到内核和容器间交流的内核模块,使得它能够利用来源操作系统或容器的生成的事件数据流

CPU监控

licontainers 能检查在机器上运行的所有容器

$ sudo sysdig -c lscontainers
container.type container.image container.name      container.id
-------------- --------------- ------------------- ------------
docker         tutum/haproxy   haproxy             49a74cb89f61
docker         wordpress       wordpress2          9bcff18fc4b4
lxc                            test                test
docker         wordpress       wordpress1          0c34fe20f1fd
docker         mysql           mysql               a0188c8bbf51

topcontainers_cpu 能检查所有运行中容器的CPU使用情况

$ sudo sysdig -c topcontainers_cpu
CPU%            container.name
-----------------------------------------------------------------------
90.13%          mysql
15.93%          wordpress1
7.27%           haproxy

-pc(or -pcontainer)命令行参数能让sysdig输出容器的上下文信息
sysdig提供了topprocs_cpu的chisel工具来展示CPU使用率最高的进程,同时使用-pc 选项,就能看到每一个进程属于那个容器
$ sudo sysdig -pc -c topprocs_cpu
注意:-pc 选项会在所有的sysdig输出中添加容器上下文信息(因为其本质就是一种信息的输出格式)
如果我们想专注于某个容器的进程信息,可以在原来命令的基础上使用sysdig的filter过滤器

$ sudo sysdig -pc -c topprocs_cpu container.name=client
CPU%            Process         container.name
----------------------------------------------
02.69%         bash            client
31.04%         curl            client

$ sudo sysdig -pc -c topprocs_cpu container.name contains wordpress
CPU%            Process         container.name
--------------------------------------------------
6.38%          apache2         wordpress3
7.37%          apache2         wordpress2
5.89%          apache2         wordpress4

网络监控

topcontainers_net 可以查看容器的网络使用情况

$ sudo sysdig -pc -c topcontainers_net
Bytes           container.name
------------------------------------------
8.48KB          mysql
5.30KB          haproxy
4.27KB          wordpress3

topprocs_net 可以查看容器进程的网络使用情况

$ sudo sysdig -pc -c topprocs_net
Bytes        Process      Host_pid     Container_pid   container.name
---------------------------------------------------------------
72.06KB      haproxy      7385         13           haproxy
56.96KB      docker.io    1775         7039         host
44.45KB      mysqld       6995         99           mysql
29.36KB      apache2      7893         124          wordpress1

topconns 可以查看容器使用最高的连接情况(socket呈现)

$ sudo sysdig -pc -c topconns
Bytes        container.name  Proto        Conn
--------------------------------------------------------------------------------
22.23KB      wordpress3     tcp         172.17.0.5:46955->172.17.0.2:3306
22.23KB      wordpress1     tcp         172.17.0.3:47244->172.17.0.2:3306
22.23KB      mysql          tcp         172.17.0.5:46971->172.17.0.2:3306

同理我们也可以使用filter进一步的筛选
sudo sysdig -pc -c topconns container name=mysql
还可以更复杂地使用sysdig,以下命令会实时打印mysql容器建立的新连接

$ sysdig -p"%fd.name" container.name=mysql and evt.type=accept
172.17.0.4:54167->172.17.0.2:3306
172.17.0.5:43257->172.17.0.2:3306
172.17.0.6:50011->172.17.0.2:3306

显示wordpress1容器在端口80发送和接收到的数据

$ sudo sysdig -A -cecho_fds container.name=wordpress1 and fd.port=80
------ Read 103B from   172.17.0.7:53430->172.17.0.3:80 (apache2)
GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: 172.17.0.7
Accept: */*
X-Forwarded-For: 172.17.0.8

------ Write 346B to   172.17.0.7:53430->172.17.0.3:80 (apache2)
HTTP/1.1 302 Found
Date: Sat, 21 Feb 2015 00:23:37 GMT
Server: Apache/2.4.10 (Debian) PHP/5.6.6
X-Powered-By: PHP/5.6.6
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://172.17.0.7/wp-admin/install.php
Content-Length: 0
Content-Type: text/html; charset=UTF-8

磁盘I/O监控

topcontainers_file 可以查看Disk I/O 最多的几个容器

$ sudo sysdig -c topcontainers_file
Bytes           container.name
--------------------------------------------------------------------
6.79KB          mysql
4.11KB          haproxy
2.13KB          wordpress4

topprocs_file 可以查看Disk I/O 最多的几个进程

$ sudo sysdig -pc -c topprocs_file
Bytes           Process         Host_pid        Container_pid   container.name
--------------------------------------------------------------------------------
63.21KB         mysqld          11126           83              mysql
29.25KB         curl            47169           11400           client
29.25KB         curl            47167           11398           client

topfiles_bytes 可以查看Disk I/O 最多的几个文件

$ sudo sysdig -pc -c topfiles_bytes
Bytes           container.name  Filename
--------------------------------------------------------------------------------
63.21KB         mysql           /tmp/#sql_1_0.MYI
6.50KB          client          /lib/x86_64-linux-gnu/libc.so.6
3.25KB          client          /lib/x86_64-linux-gnu/libpthread.so.0

查看某个文件的活动(无论它属于那个容器)
sudo sysdig -pc -c echo_fds "fd.name=/etc/passwd"

可视化容器文件I/O的活动情况
sudo sysdig -c spectrogram fd.type=file and container.name=mysql

sg.gif

关于用户跟踪,日志和容器间请求流的小技巧

监控容器内的用户活动 spy_users
$ sudo sysdig# ./sysdig -pc -c spy_users
43760 13:35:18 root@client) sleep 0.1
43760 13:35:18 root@client) curl 172.17.0.7
43760 13:35:18 root@client) sleep 0.1

使用filter过滤器筛选 $sudo sysdig# ./sysdig -pc -c spy_users container.name=mysql

查看多个容器的末尾日志(tail)

Docker官方鼓励容器进程将日志输出到标志输出stdout,但部分进程仍会输出到某个文件。
spy_logs 可以查看这些日志输出。它查看所有容器内外以.log 和 _log 结尾的文件的缓冲区

$ sudo sysdig -pc -cspy_logs
wordpress3 apache2 /var/log/apache2/access.log 172.17.0.7 - - [21/Feb/2015:21:49:00 +0000] "GET / HTTP/1.1" 302 346 "-" "curl/7.35.0"
wordpress4 apache2 /var/log/apache2/access.log 172.17.0.7 - - [21/Feb/2015:21:49:01 +0000] "GET / HTTP/1.1" 302 346 "-" "curl/7.35.0"

$ sudo sysdig -pc -cspy_logs container.name=wordpress1
wordpress1 apache2 /var/log/apache2/access.log 172.17.0.7 - - [21/Feb/2015:21:55:18 +0000] "GET / HTTP/1.1" 302 346 "-" "curl/7.35.0"
观察容器间的应用请求数据流

以下内容中,filter 过滤器限制显示了两个应用间的流量,去除了多余信息。

$ sudo sysdig -pc -A -c echo_fds "fd.ip=172.17.0.3 and fd.ip=172.17.0.7"

------ Write 103B to  [haproxy] [d468ee81543a]  172.17.0.7:37557->172.17.0.3:80 (haproxy)
GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: 172.17.0.7
Accept: */*
X-Forwarded-For: 172.17.0.8


------ Read 103B from  [wordpress1] [12b8c6a04031]  172.17.0.7:37557->172.17.0.3:80 (apache2)
GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: 172.17.0.7
Accept: */*
X-Forwarded-For: 172.17.0.8


------ Write 346B to  [wordpress1] [12b8c6a04031]  172.17.0.7:37557->172.17.0.3:80 (apache2)
HTTP/1.1 302 Found
Date: Sat, 21 Feb 2015 22:19:18 GMT
Server: Apache/2.4.10 (Debian) PHP/5.6.6
X-Powered-By: PHP/5.6.6
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://172.17.0.7/wp-admin/install.php
Content-Length: 0
Content-Type: text/html; charset=UTF-8


------ Read 346B from  [haproxy] [d468ee81543a]  172.17.0.7:37557->172.17.0.3:80 (haproxy)
HTTP/1.1 302 Found
Date: Sat, 21 Feb 2015 22:19:18 GMT
Server: Apache/2.4.10 (Debian) PHP/5.6.6
X-Powered-By: PHP/5.6.6
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Location: http://172.17.0.7/wp-admin/install.php
Content-Length: 0
Content-Type: text/html; charset=UTF-8

参考资料:
https://sysdig.com/blog/let-light-sysdig-adds-container-visibility/

你可能感兴趣的:(Sysdig容器监控)