K8S Pod该如何监控

背景

由于业务容器化的推进,对K8S上运行的业务,也必须做到向之前物理机/虚拟机上一样,要有完备的监控保障。但是,毕竟K8S不是物理机/虚拟机,由于技术实现方式不同,监控方面也是有一定差别的。
Pod是K8S上调度的最小单元,本文就K8S Pod该如何监控进行说明。

CPU

在使用物理机/虚拟机时,对于CPU的监控通常关注的是CPU使用率、CPU负载等。在K8S场景下,关注的指标有所不同:CPU使用率、CPU受限(CPU Throttled)时间或占比等。

CPU使用率

K8S场景下,CPU使用率是相对CPU核的使用时间来衡量的。比如,一个应用申请的是1个CPU core,但实际运行时只消耗了0.5,这时的CPU使用率可以计算为50%。

CPU使用时间相关指标如下:

指标 类型 说明
container_cpu_usage_seconds_total counter Cumulative cpu time consumed in seconds(CPU消费累计时间)
container_cpu_system_seconds_total counter Cumulative system cpu time consumed in seconds(系统CPU消费的累计时间)
container_cpu_user_seconds_total counter Cumulative user cpu time consumed in seconds(用户CPU消费的累计时间)

需要注意的是:

  • 上面3个指标是对CPU不同的消费时间的统计,并不是CPU使用率。如果计算使用率,需要除以应用申请的CPU配额
  • 由于指标类型为counter,在计算使用时先做rate或irate

CPU配额可以通过container_spec_cpu_quota除以container_spec_cpu_period来得到,或用CPU Limit值kube_pod_container_resource_limits_cpu_cores也可以

CPU使用率监控图类似如下:
在这里插入图片描述

CPU受限

CPU受限这个说法在物理机/虚拟机场景是没有的,由于K8S使用绝对公平调度(Completely Fair Scheduler,简称CFS),通过配置cgroup带宽控制(bandwidth control)来限制Pod的CPU资源。带宽控制组定义了一个周期(cfs_period_us),通常100000微秒(即1/10秒)。还定义了一个配额(cfs_quota_us),表示允许进程在设置的周期长度内所能使用的CPU时间数,两个文件配合起来设置CPU的使用上限。两个文件的单位都是微秒(us),cfs_period_us的取值范围为 1 毫秒(ms)到 1 秒(s),cfs_quota_us 的取值大于 1ms 即可,如果 cfs_quota_us 的值为 -1(默认值),表示不受 CPU 时间的限制。我们在编写K8S资源的yaml文件时,如果将Pod的CPU Limits设置为100m,表示可使用100/1000个CPU核心,即100000微秒的CPU时间周期中的10000。当容器使用CPU资源达到申请的配额时,CPU使用时间会被限制。

CPU限制相关的指标如下:

指标 类型 说明
container_cpu_cfs_periods_total counter Number of elapsed enforcement period intervals(使用的CPU时间周期数)
container_cpu_cfs_throttled_periods_total counter Number of throttled period intervals(被限制CPU时间周期数)
container_cpu_cfs_throttled_seconds_total counter Total time duration the container has been throttled(记录CPU被限制的时间)

说明:CPU资源的限制与内存不同。当容器使用的内存超过限制配额后,会被系统加到OOM-Killing候选中。当容器使用CPU资源到达申请配额时,容器不会被系统驱逐或怎么样,只是限制CPU使用。

内存

内存监控相关指标如下:

指标 类型 说明
container_memory_cache gauge Number of bytes of page cache memory(页缓存的内存大小)
container_memory_max_usage_bytes gauge Maximum memory usage recorded in bytes(最大内存使用记录)
container_memory_rss gauge Size of RSS in bytes(常驻内存大小)
container_memory_swap gauge Container swap usage in bytes(swap使用量)
container_memory_usage_bytes gauge Current memory usage in bytes, including all memory regardless of when it was accessed(当前内存使用,包括所有的缓存)
container_memory_working_set_bytes gauge Current working set in bytes(当前使用的内存,不包括长期没有访问的缓存)
container_memory_failcnt counter Number of memory usage hits limits(达到使用上限的次数)
container_memory_failures_total counter Cumulative count of memory allocation failures(内存分配失败次数)
container_memory_mapped_file gauge Size of memory mapped files in bytes(内存映射文件的大小)

与物理机/虚拟机类似,通常关注的指标是如下几个:

  • 当前使用内存:container_memory_usage_bytes或container_memory_working_set_bytes
    • container_memory_usage_bytes包含了很久没用的缓存,该值比container_memory_working_set_bytes要大
  • 常驻内存:container_memory_rss
  • 缓存:container_memory_cache
  • swap:container_memory_swap

监控图类似如下:
在这里插入图片描述

磁盘

对于磁盘,容器层面的磁盘监控指标关注的要相对少一些。如果不使用持久卷,通常不需要关心磁盘可用空间。毕竟,宿主机的磁盘IO监控还是会做的。

指标 类型 说明
container_fs_usage_bytes gauge Number of bytes that are consumed by the container on this filesystem(容器磁盘空间使用)
container_fs_writes_bytes_total counter Cumulative count of bytes written(磁盘写入速度)
container_fs_reads_bytes_total counter Cumulative count of bytes read(磁盘读取速度)

监控图类似如下:
在这里插入图片描述

网络

与物理机/虚拟机场景类似,容器的网络监控主要关注入向/出向的网络流量、packet数、drop率等。

指标 类型 说明
container_network_receive_bytes_total counter Cumulative count of bytes received(入向流量大小,单位字节)
container_network_receive_packets_dropped_total counter Cumulative count of packets dropped while receiving(入向dropped包数)
container_network_receive_packets_total counter Cumulative count of packets received(入向packet数)
container_network_transmit_bytes_total counter Cumulative count of bytes transmitted(出向流量大小,单位字节)
container_network_transmit_packets_dropped_total counter Cumulative count of packets dropped while transmitting(出向dropped包数)
container_network_transmit_packets_total counter Cumulative count of packets transmitted(出向packet数)

这几个网络指标理解起来比较简单,直接使用即可。监控图类似如下:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章