V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
silenceshell

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

  •  
  •   silenceshell · Oct 1, 2022 · 4936 views
    This topic created in 1306 days ago, the information mentioned may be changed or developed.

    容器里运行的 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 replies    2022-10-09 14:20:16 +08:00
    me221
        1
    me221  
       Oct 1, 2022
    好东西 Star 了
    zeuss
        2
    zeuss  
       Oct 1, 2022
    加到 dockerfile 里试试
    beetlerx
        3
    beetlerx  
       Oct 1, 2022
    是不支持 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  
       Oct 1, 2022 via Android
    竞品 ctop
    silenceshell
        5
    silenceshell  
    OP
       Oct 1, 2022
    @beetlerx topic 引用的 goprocinfo 包是会读 /proc/{pid}/io 的,我在腾讯云上开了台 centos stream 9 上测试了下,能正常运行,是有做一些安全限制,禁止了 /proc 读取吗?
    silenceshell
        6
    silenceshell  
    OP
       Oct 1, 2022
    @bxb100 是的,ctop 是很成熟的工具。
    使用场景会有一点小不同:ctop 对于管理员来说会比较适用,而一般的研发角色没有宿主机的权限,直接运行在容器里的 topic 会更适合一些
    beetlerx
        7
    beetlerx  
       Oct 1, 2022
    @silenceshell 看了下 的确是,podman 是非 root 权限的,所以没权限读取 /proc
    4BVL25L90W260T9U
        8
    4BVL25L90W260T9U  
       Oct 5, 2022
    interesting! 非常有用的工具
    n0bug
        9
    n0bug  
       Oct 8, 2022
    @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
       Oct 8, 2022
    @n0bug 物理机本身负载如何?如果比较低的话,top 取物理机的 load ,和 topic 是接近的。
    可以在物理机上,再裸跑 stress --cpu 1 ,这样物理机会有 3 个 stess ,top 应该是 3 ,而 topic 是 2 ,这样对比明显一些。
    MintMana
        11
    MintMana  
       Oct 8, 2022
    捉个 typo ,:%s/getCgoupValueByPath/getCgroupValueByPath/g
    silenceshell
        12
    silenceshell  
    OP
       Oct 8, 2022
    @MintMana oh thanks ,我改下
    n0bug
        13
    n0bug  
       Oct 9, 2022
    @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  
       Oct 9, 2022
    1. docker run --rm -it ubuntu:latest bash 启动容器,
    2. stress --cpu 2
    3. 在物理机 stress --cpu 1
    4. 然后分别用 topic 和 top 查看结果
    n0bug
        15
    n0bug  
       Oct 9, 2022
    4. 然后在容器内,分别用 topic 和 top 查看结果
    WithLin
        16
    WithLin  
       Oct 9, 2022
    直接挂 lxc 不行么?
    silenceshell
        17
    silenceshell  
    OP
       Oct 9, 2022
    @n0bug top 看到的进程是容器里的,这个没问题;可以关注下 load/cpu/内存等指标,这些是取的宿主机,而非容器
    silenceshell
        18
    silenceshell  
    OP
       Oct 9, 2022
    @WithLin 是指 lxcfs ? lxcfs 功能上更好,java 应用不需要特意关注是在容器环境运行。lxcfs 需要管理员进行配置,对于一般的研发人员没有这个权限。
    julyclyde
        19
    julyclyde  
       Oct 9, 2022
    恶意凑词,会导致搜索你的作品的时候遇到困难的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   977 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 19:38 · PVG 03:38 · LAX 12:38 · JFK 15:38
    ♥ Do have faith in what you're doing.