经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
使用sysdig查看容器里的系统调用
来源:cnblogs  作者:人生的哲理  时间:2024/5/31 16:14:18  对本文有异议

一.系统环境

本文主要基于Kubernetes1.22.2和Linux操作系统Ubuntu 18.04。

服务器版本 docker软件版本 Kubernetes(k8s)集群版本 CPU架构
Ubuntu 18.04.5 LTS Docker version 20.10.14 v1.22.2 x86_64

Kubernetes集群架构:k8scludes1作为master节点,k8scludes2,k8scludes3作为worker节点。

服务器 操作系统版本 CPU架构 进程 功能描述
k8scludes1/192.168.110.128 Ubuntu 18.04.5 LTS x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master节点
k8scludes2/192.168.110.129 Ubuntu 18.04.5 LTS x86_64 docker,kubelet,kube-proxy,calico k8s worker节点
k8scludes3/192.168.110.130 Ubuntu 18.04.5 LTS x86_64 docker,kubelet,kube-proxy,calico k8s worker节点

二.前言

在现代的微服务架构中,容器化技术已经成为了标准。然而,随着服务的复杂性增加,监控和调试也变得越来越困难。Sysdig是一种强大的系统和网络安全监控工具,它可以帮助我们深入理解容器内部的系统调用。本文将介绍如何使用Sysdig来查看容器内的系统调用。

使用sysdig查看容器里的系统调用的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Ubuntu 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/17632858.html。

三.系统调用简介

系统调用是操作系统提供的接口,应用程序可以通过它来请求操作系统的服务。例如,读写文件、创建进程等。通过分析系统调用,我们可以更好地理解应用程序的行为。

四.Sysdig简介

Sysdig是一个强大的系统和网络安全监控工具,它可以捕获和分析系统和网络活动。Sysdig可以提供实时的系统和网络视图,以及详细的事件信息。它支持多种平台,包括Linux、macOS和Windows。

默认情况下,sysdig 在一行中打印每个事件的信息,格式如下:

  1. root@k8scludes3:~# sysdig
  2. 34378 12:02:36.269753803 2 echo (7896) > close fd=3(/usr/lib/locale/locale-archive)
  3. 34379 12:02:36.269754164 2 echo (7896) < close res=0
  4. 34380 12:02:36.269781699 2 echo (7896) > fstat fd=1(/dev/pts/3)
  5. *%evt.num %evt.time %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args
  • evt.num 是增量事件编号
  • evt.time 是事件时间戳
  • evt.cpu 是捕获事件的 CPU 编号
  • proc.name 是生成事件的进程的名称
  • thread.tid 是产生事件的TID,对应单线程进程的PID
  • evt.dir 是事件方向,> 表示进入事件,< 表示退出事件
  • evt.type 是事件的名称,例如 ‘open’ 或 ‘read’
  • evt.args 是事件参数的列表。

可以使用sysdig -p修改输出格式:

  1. sysdig -p "*%evt.num %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.args"

五.使用sysdig查看容器里的系统调用

5.1 以二进制包的形式安装sysdig

在博客《在kubernetes里使用seccomp限制容器的系统调用》中,我们使用strace查看执行命令时有哪些系统调用,那如何查看pod或者容器里执行了哪些系统调用呢?我们可以使用sysdig工具。

创建目录存放文件。

  1. root@k8scludes1:~# mkdir systemsafe
  2. root@k8scludes1:~# cd systemsafe/

下载DRAIOS-GPG-KEY.public。

  1. root@k8scludes1:~/systemsafe# wget https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public

导入key。

  1. root@k8scludes1:~/systemsafe# cat DRAIOS-GPG-KEY.public | sudo apt-key add -
  2. OK

下载draios.list。

  1. root@k8scludes1:~/systemsafe# curl -s -o /etc/apt/sources.list.d/draios.list https://s3.amazonaws.com/download.draios.com/stable/deb/draios.list
  2. root@k8scludes1:~/systemsafe# ll -h /etc/apt/sources.list.d/draios.list
  3. -rw-r--r-- 1 root root 59 May 16 20:50 /etc/apt/sources.list.d/draios.list

更新软件源。

  1. root@k8scludes1:~/systemsafe# apt-get update
  2. Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
  3. Get:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]
  4. Hit:3 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease
  5. Hit:4 https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic InRelease
  6. Get:5 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
  7. Get:6 https://download.sysdig.com/stable/deb stable-amd64/ InRelease [1,301 B]
  8. Get:7 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
  9. Get:8 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
  10. Get:9 http://mirrors.aliyun.com/ubuntu bionic-security/main Sources [272 kB]
  11. Get:10 http://mirrors.aliyun.com/ubuntu bionic-security/main i386 Packages [1,171 kB]
  12. Get:11 https://download.sysdig.com/stable/deb stable-amd64/ Packages [11.1 kB]
  13. Get:12 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages [2,225 kB]
  14. Get:13 http://mirrors.aliyun.com/ubuntu bionic-security/universe amd64 Packages [1,198 kB]
  15. Get:14 http://mirrors.aliyun.com/ubuntu bionic-security/universe i386 Packages [1,018 kB]
  16. Fetched 6,390 kB in 8s (781 kB/s)
  17. Reading package lists... Done

安装Linux 头。

  1. root@k8scludes1:~/systemsafe# apt-get -y install linux-headers-$(uname -r)
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. linux-headers-4.15.0-112-generic is already the newest version (4.15.0-112.113).
  6. linux-headers-4.15.0-112-generic set to manually installed.
  7. 0 upgraded, 0 newly installed, 0 to remove and 213 not upgraded.

安装sysdig。

  1. root@k8scludes1:~/systemsafe# apt-get -y install sysdig

现在sysdig就安装好了。

  1. root@k8scludes1:~/systemsafe# which sysdig
  2. /usr/bin/sysdig

5.2 使用sysdig查看容器里的系统调用

创建nginx容器,并进行端口映射,-p 80:80表示物理机端口:容器端口。关于docker容器的详细操作,请查看博客《一文搞懂docker容器基础:docker镜像管理,docker容器管理》。

  1. root@k8scludes1:~/systemsafe# docker run -dit --name=nginxweb --restart=always -p 80:80 nginx
  2. a74aff1e39a2d9ff7cece775f6fec770d99801b61041bd108c427276e9ddf168

查看容器。

  1. root@k8scludes1:~/systemsafe# docker ps | grep nginxweb
  2. a74aff1e39a2 nginx "/docker-entrypoint.…" 13 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginxweb

在执行sysdig -p "*%evt.time,%proc.name,%evt.type" container.id=a74aff1e39a2的时候,浏览器访问http://192.168.110.128/ ,就显示了系统调用,应该把启动/停止/重启/访问nginx容器的系统调用总结出来,这样才好配置seccomp profile,以便使用seccomp限制容器的系统调用,详情请查看博客《在kubernetes里使用seccomp限制容器的系统调用》。

  1. #evt.time 是事件时间戳,proc.name 是生成事件的进程的名称,evt.type 是事件的名称,例如 ‘open’ 或 ‘read’即系统调用
  2. root@k8scludes1:~/systemsafe# sysdig -p "*%evt.time,%proc.name,%evt.type" container.id=a74aff1e39a2
  3. 23:51:12.309668357,nginx,epoll_wait
  4. 23:51:12.309715073,nginx,accept
  5. 23:51:12.309729527,nginx,accept
  6. 23:51:12.309755667,nginx,epoll_ctl
  7. 23:51:12.309760249,nginx,epoll_ctl
  8. 23:51:12.309762753,nginx,epoll_wait
  9. ......
  10. 23:51:12.326853019,nginx,close
  11. 23:51:12.326854453,nginx,close
  12. 23:51:12.326860121,nginx,setsockopt
  13. 23:51:12.326924377,nginx,setsockopt
  14. 23:51:12.326931469,nginx,epoll_wait
  15. 23:51:12.326938835,nginx,switch
  16. 23:52:12.368369580,nginx,epoll_wait
  17. 23:52:12.368385842,nginx,close
  18. 23:52:12.368388496,nginx,close
  19. 23:52:12.368516088,nginx,epoll_wait
  20. 23:52:12.368522695,nginx,switch
  21. 23:52:17.329270467,nginx,epoll_wait
  22. 23:52:17.329293917,nginx,close
  23. 23:52:17.329297553,nginx,close
  24. 23:52:17.329453581,nginx,epoll_wait
  25. 23:52:17.329461491,nginx,switch

删除容器。

  1. root@k8scludes1:~/systemsafe# docker rm -f nginxweb
  2. nginxweb

现在有一个pod,audit-pod运行在k8scludes3节点上,可以去k8scludes3节点查看pod audit-pod的容器ID。

  1. root@k8scludes1:~/systemsafe# kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. audit-pod 1/1 Running 0 5h8m 10.244.1.110 k8scludes3 <none> <none>

查询到audit-pod的容器ID,就可以使用sysdig查看容器里的系统调用了。

  1. root@k8scludes1:~/systemsafe# sysdig -p "*%evt.time,%proc.name,%evt.type" container.id=a61ae9de68a9

5.3 以容器的方式运行sysdig

这次在k8scludes3节点上,使用容器的方式运行sysdig。

拉取sysdig/sysdig镜像。

  1. root@k8scludes3:~# docker pull sysdig/sysdig

创建容器,以容器的方式运行sysdig,因为相关数据卷已经把宿主机的目录映射到容器目录,容器里执行sysdig,相当于在宿主机里执行sysdig。

  1. #挂载数据卷:-v 本地目录:容器目录
  2. #使用--privileged标志来运行容器时,容器内的进程将具有对主机系统的完全访问权限,包括访问设备、挂载文件系统等,表示特权
  3. root@k8scludes3:~# docker run -i -dt --name sysdig --restart=always --privileged -v /var/run/docker.sock:/host/var/run/docker.sock -v /dev:/host/dev -v /proc:/host/proc:ro -v /boot:/host/boot:ro -v /lib/modules:/host/lib/modules:ro -v /usr:/host/usr:ro sysdig/sysdig
  4. 62a295df7f57892cf5379e8acfebc9aa889a8fdd9148292d94c918b6d1641052

查看容器。

  1. root@k8scludes3:~# docker ps | grep sysdig
  2. 62a295df7f57 sysdig/sysdig "/docker-entrypoint.…" About a minute ago Up About a minute sysdig

创建一个别名,现在执行sysdig命令相当于在容器里执行sysdig。

  1. root@k8scludes3:~# alias sysdig='docker exec -it sysdig sysdig'

现在有一个pod。

  1. root@k8scludes3:~# docker ps | grep audit-pod
  2. a61ae9de68a9 a6838e9a6ff6 "/http-echo '-text=j…" 5 hours ago Up 5 hours k8s_test-container_audit-pod_systemsafe_0e4d967b-14e6-47c3-9038-95385d316d96_0
  3. 154ef7c62e24 registry.aliyuncs.com/google_containers/pause:3.5 "/pause" 5 hours ago Up 5 hours k8s_POD_audit-pod_systemsafe_0e4d967b-14e6-47c3-9038-95385d316d96_0

使用sysdig查看容器里的系统调用,但是报错了,没有驱动。

  1. root@k8scludes3:~# sysdig -p "*%evt.time,%proc.name,%evt.type" container.id=a61ae9de68a9
  2. Unable to load the driver
  3. error opening device /host/dev/scap0. Make sure you have root credentials and that the scap module is loaded.

下载DRAIOS-GPG-KEY.public,导入key。

  1. root@k8scludes3:~# curl -s https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public | sudo apt-key add -
  2. OK

下载draios.list。

  1. root@k8scludes3:~# curl -s -o /etc/apt/sources.list.d/draios.list https://s3.amazonaws.com/download.draios.com/stable/deb/draios.list
  2. root@k8scludes3:~# ll -h /etc/apt/sources.list.d/draios.list
  3. -rw-r--r-- 1 root root 59 May 16 20:50 /etc/apt/sources.list.d/draios.list

更新软件源。

  1. root@k8scludes3:~# apt-get update

安装Linux 头。

  1. root@k8scludes3:~# apt-get -y install linux-headers-$(uname -r)

使用sysdig查看容器里的系统调用。

  1. root@k8scludes3:~# sysdig -p "*%evt.time,%proc.name,%evt.type" container.id=a61ae9de68a9
  2. 18:53:19.713884315,container:a61ae9de68a9,container
  3. 18:53:52.943490750,http-echo,epoll_wait
  4. 18:53:52.943525821,http-echo,futex
  5. 18:53:52.943550469,http-echo,futex
  6. 18:53:52.943553208,http-echo,accept
  7. 18:53:52.943568162,http-echo,accept
  8. 18:53:52.943618359,http-echo,epoll_ctl
  9. ......
  10. 18:53:52.945825176,http-echo,futex
  11. 18:53:52.945831674,http-echo,switch

六.总结:

通过使用Sysdig,我们可以深入理解容器内部的系统调用,从而更好地监控和调试我们的应用程序。Sysdig的强大功能和灵活性使其成为现代微服务架构中的必备工具。

原文链接:https://www.cnblogs.com/renshengdezheli/p/18224760

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号