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/

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容