V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
silenceshell
V2EX  ›  分享创造

分享一个我写的容器里的 top 命令: topic (top in container)

  •  
  •   silenceshell · 2022-10-01 08:36:09 +08:00 · 3649 次点击
    这是一个创建于 563 天前的主题,其中的信息可能已经有所发展或是发生改变。

    容器里运行的 top 命令是从宿主机获取的指标(比如 uptime ,users ,load ,task ,cpu ,memory ),而在大规格物理机上运行的容器通常会设置 limit (例如 4c8G ),top 命令查看到物理机的运行信息,而不是容器的运行信息,对于用户会有误导。

    topic ( top in container )从容器里获取这些指标,并展示出来,可以将 topic 的二进制打到容器镜像里,这样无论容器运行在哪里,都可以方便(并准确的)的查看容器的 top 运行指标。

    Github:https://github.com/silenceshell/topic

    下图展示了在物理机上的 2c2G 的容器里,运行 stress --cpu 2 ,通过 topic 查看到的输出。

    topic

    19 条回复    2022-10-09 14:20:16 +08:00
    me221
        1
    me221  
       2022-10-01 08:47:38 +08:00
    好东西 Star 了
    zeuss
        2
    zeuss  
       2022-10-01 09:10:34 +08:00
    加到 dockerfile 里试试
    beetlerx
        3
    beetlerx  
       2022-10-01 10:12:16 +08:00
    是不支持 podman 么
    podman 4.3.0-dev
    podman run --name pg --cpus="2.0" -m 1g -d postgres:14.2
    报错
    [img]https://z.run/i/2022/10/01/gpzyfy.png[/img]
    bxb100
        4
    bxb100  
       2022-10-01 11:09:31 +08:00 via Android
    竞品 ctop
    silenceshell
        5
    silenceshell  
    OP
       2022-10-01 11:45:56 +08:00
    @beetlerx topic 引用的 goprocinfo 包是会读 /proc/{pid}/io 的,我在腾讯云上开了台 centos stream 9 上测试了下,能正常运行,是有做一些安全限制,禁止了 /proc 读取吗?
    silenceshell
        6
    silenceshell  
    OP
       2022-10-01 11:53:57 +08:00
    @bxb100 是的,ctop 是很成熟的工具。
    使用场景会有一点小不同:ctop 对于管理员来说会比较适用,而一般的研发角色没有宿主机的权限,直接运行在容器里的 topic 会更适合一些
    beetlerx
        7
    beetlerx  
       2022-10-01 14:56:37 +08:00
    @silenceshell 看了下 的确是,podman 是非 root 权限的,所以没权限读取 /proc
    ospider
        8
    ospider  
       2022-10-05 14:52:56 +08:00
    interesting! 非常有用的工具
    n0bug
        9
    n0bug  
       2022-10-08 14:26:58 +08:00
    @silenceshell 有个问题想请教下。 “top 命令查看到物理机的运行信息,而不是容器的运行信息”。

    我用 docker run --rm -it ubuntu:latest bash 启动容器,stress --cpu 2 ,然后分别用 topic 和 top 查看结果,可是我看到两者的输出几乎一致,似乎 top 命令也同样是显示了容器的运行信息,而非物理机 ?

    截图如下
    ![]( https://imgur.com/Rc4SGuF)

    我的 docker version
    ```
    $docker version
    Client: Docker Engine - Community
    Version: 20.10.18
    API version: 1.41
    Go version: go1.18.6
    Git commit: b40c2f6
    Built: Thu Sep 8 23:11:45 2022
    OS/Arch: linux/amd64
    Context: default
    Experimental: true

    Server: Docker Engine - Community
    Engine:
    Version: 20.10.18
    API version: 1.41 (minimum version 1.12)
    Go version: go1.18.6
    Git commit: e42327a
    Built: Thu Sep 8 23:09:37 2022
    OS/Arch: linux/amd64
    Experimental: false
    containerd:
    Version: 1.6.8
    GitCommit: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
    runc:
    Version: 1.1.4
    GitCommit: v1.1.4-0-g5fd4c4d
    docker-init:
    Version: 0.19.0
    GitCommit: de40ad0
    ```
    silenceshell
        10
    silenceshell  
    OP
       2022-10-08 18:01:33 +08:00
    @n0bug 物理机本身负载如何?如果比较低的话,top 取物理机的 load ,和 topic 是接近的。
    可以在物理机上,再裸跑 stress --cpu 1 ,这样物理机会有 3 个 stess ,top 应该是 3 ,而 topic 是 2 ,这样对比明显一些。
    MintMana
        11
    MintMana  
       2022-10-08 20:44:47 +08:00
    捉个 typo ,:%s/getCgoupValueByPath/getCgroupValueByPath/g
    silenceshell
        12
    silenceshell  
    OP
       2022-10-08 21:47:24 +08:00
    @MintMana oh thanks ,我改下
    n0bug
        13
    n0bug  
       2022-10-09 06:06:16 +08:00
    @silenceshell hmm.... 我试了下你的方法,topic 和 top 显示的始终是一样的,都是 2 个 cpu 占 100%,不会显示在物理机上的第 3 个 stress ,截图跟上个回复一样。

    不知道老哥是啥配置?

    以下是我的配置:
    物理机:Linux 5.4.0-125-generic #141-Ubuntu SMP Wed Aug 10 13:42:03 UTC 2022 x86_64
    Docker Version: 见上一个回复
    Docker Image: ubuntu:latest
    n0bug
        14
    n0bug  
       2022-10-09 06:08:44 +08:00
    1. docker run --rm -it ubuntu:latest bash 启动容器,
    2. stress --cpu 2
    3. 在物理机 stress --cpu 1
    4. 然后分别用 topic 和 top 查看结果
    n0bug
        15
    n0bug  
       2022-10-09 06:09:32 +08:00
    4. 然后在容器内,分别用 topic 和 top 查看结果
    WithLin
        16
    WithLin  
       2022-10-09 09:33:02 +08:00
    直接挂 lxc 不行么?
    silenceshell
        17
    silenceshell  
    OP
       2022-10-09 10:25:18 +08:00
    @n0bug top 看到的进程是容器里的,这个没问题;可以关注下 load/cpu/内存等指标,这些是取的宿主机,而非容器
    silenceshell
        18
    silenceshell  
    OP
       2022-10-09 10:27:29 +08:00
    @WithLin 是指 lxcfs ? lxcfs 功能上更好,java 应用不需要特意关注是在容器环境运行。lxcfs 需要管理员进行配置,对于一般的研发人员没有这个权限。
    julyclyde
        19
    julyclyde  
       2022-10-09 14:20:16 +08:00
    恶意凑词,会导致搜索你的作品的时候遇到困难的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1524 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 17:09 · PVG 01:09 · LAX 10:09 · JFK 13:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.