1.kubectl top 为什么会报错
一般情况下 top 报错有以下几种,可以 kubectl top pod -v=10看到具体的调用日志:

没有部署 heapster 或者 metric-server,或者 pod 运行异常,可以排查对应 pod 日志
要看的 pod 刚刚建出来,还没来得及采集指标,报 not found 错误,默认 1 分钟
以上两种都不是,可以检查下 kubelet 的 10255 端口是否开放,默认情况下会使用这个只读端口获取指标,也可以在 heapster 或 metric-server 的配置中增加证书,换成 10250 认证端口
2 kubectl top pod 内存怎么计算,包含 pause容器吗
每次启动 pod,都会有一个 pause 容器,既然是容器就一定有资源消耗(一般在 2-3M 的内存),cgroup 文件中,业务容器和 pause 容器都在同一个 pod的文件夹下。

但 cadvisor 在查询 pod 的内存使用量时,是先获取了 pod 下的container列表,再逐个获取container的内存占用,不过这里的 container 列表并没有包含 pause,因此最终 top pod 的结果也不包含 pause 容器

pod 的内存使用量计算

kubectl top pod 得到的内存使用量,并不是 cadvisor 中的 container_memory_usage_bytes,而是 container_memory_working_set_bytes,计算方式为:

container_memory_usage_bytes = container_memory_rss + container_memory_cache + kernel memory
container_memory_working_set_bytes = container_memory_usage_bytes – total_inactive_file(未激活的匿名缓存页)
container_memory_working_set_bytes 是容器真实使用的内存量,也是 limit限制时的 oom 判断依据。

cadvisor 中的 container_memory_usage_bytes 对应 cgroup 中的 memory.usage_in_bytes 文件,但 container_memory_working_set_bytes 并没有具体的文件,他的计算逻辑在 cadvisor 的代码中

同理,node 的内存使用量也是 container_memory_working_set_bytes

kubectl top 计算了缓存等容量,而进入pod内部top则计算不到这些缓存,
参考:http://www.xuyasong.com/?p=1781

https://cloud.tencent.com/developer/article/1645042

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐