爱意满满的作品展示区。
silenceshell

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

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