
# Pod资源监控指标
CCI支持Pod资源基础监控能力，提供CPU、内存、磁盘、网络等多种监控指标，满足对Pod资源的基本监控需求。
Pod内置系统agent，默认会以http服务的形式提供Pod和容器的监控指标。agent集成到Pod里面，会占用Pod内资源，建议您预留30MB的内存。
#### 资源监控指标
资源基础监控包含CPU/内存/磁盘等类别，具体请参见[资源监控指标]。
 表1资源监控指标 
| 监控指标类                                                                                                                                                            | 指标名称                                             | 释义                                       |
|:---|:---|:---|
| CPU                                                                                                   | container_cpu_system_seconds_total               | System CPU累积占用时间（单位：秒）                   |
| CPU                                                                                                   | container_cpu_usage_seconds_total                | 容器在所有CPU内核上的累积占用时间 (单位：秒)                |
| CPU                                                                                                   | container_cpu_user_seconds_total                 | User CPU累积占用时间（单位：秒）                     |
| CPU                                                                                                   | container_cpu_cfs_periods_total                  | 已经执行的CPU时间周期数                            |
| CPU                                                                                                   | container_cpu_cfs_throttled_periods_total        | 被限流的CPU时间周期                              |
| CPU                                                                                                   | container_cpu_cfs_throttled_seconds_total        | 被限流的CPU时间（单位：秒）                          |
| 文件系统/磁盘IO                  | container_fs_inodes_free                         | 文件系统的可用inode数量                           |
| 文件系统/磁盘IO                  | container_fs_usage_bytes                         | 文件系统的使用量(单位：字节)                          |
| 文件系统/磁盘IO                  | container_fs_inodes_total                        | 文件系统的总计inode数量                           |
| 文件系统/磁盘IO                  | container_fs_io_current                          | 磁盘/文件系统当前正在进行的 I/O 数量                    |
| 文件系统/磁盘IO                  | container_fs_io_time_seconds_total               | 磁盘/文件系统花费在 I/O 上的累计秒数                    |
| 文件系统/磁盘IO                  | container_fs_io_time_weighted_seconds_total      | 磁盘/文件系统累积加权 I/O 时间                       |
| 文件系统/磁盘IO                  | container_fs_limit_bytes                         | 容器可以使用的磁盘/文件系统总量(单位：字节)                  |
| 文件系统/磁盘IO                  | container_fs_reads_bytes_total                   | 容器累积读取磁盘/文件系统数据的总量(单位：字节)                |
| 文件系统/磁盘IO                  | container_fs_read_seconds_total                  | 容器累积读取磁盘/文件系统数据的秒数                       |
| 文件系统/磁盘IO                  | container_fs_reads_merged_total                  | 容器合并读取磁盘/文件系统的累积计数                       |
| 文件系统/磁盘IO                  | container_fs_reads_total                         | 容器已完成读取磁盘/文件系统的累积计数                      |
| 文件系统/磁盘IO                  | container_fs_sector_reads_total                  | 容器已完成扇区读取磁盘/文件系统的累积计数                    |
| 文件系统/磁盘IO                  | container_fs_sector_writes_total                 | 容器已完成扇区写入磁盘/文件系统的累积计数                    |
| 文件系统/磁盘IO                  | container_fs_writes_bytes_total                  | 容器累积写入磁盘/文件系统数据的总量(单位：字节)                |
| 文件系统/磁盘IO                  | container_fs_write_seconds_total                 | 容器累计写入磁盘/文件系统的秒数                         |
| 文件系统/磁盘IO                  | container_fs_writes_merged_total                 | 容器合并写入磁盘/文件系统的累积计数                       |
| 文件系统/磁盘IO                  | container_fs_writes_total                        | 容器已完成写入磁盘/文件系统的累积计数                      |
| 文件系统/磁盘IO                  | container_blkio_device_usage_total               | 容器区分IO操作对磁盘的使用总量(单位：字节)                  |
| 内存                                                                                  | container_memory_failures_total                  | 容器内存分配失败的累积计数                            |
| 内存                                                                                  | container_memory_failcnt                         | 容器内存使用达到限制的次数                            |
| 内存                                                                                  | container_memory_cache                           | 容器总页缓存内存（单位：字节）                          |
| 内存                                                                                  | container_memory_mapped_file                     | 容器内存映射文件的大小（单位：字节）                       |
| 内存                                                                                  | container_memory_max_usage_bytes                 | 容器历史最大内存使用量（单位：字节）                       |
| 内存                                                                                  | container_memory_rss                             | 容器常驻内存集的大小（单位：字节）                        |
| 内存                                                                                  | container_memory_swap                            | 容器虚拟内存使用量（单位：字节）                         |
| 内存                                                                                  | container_memory_usage_bytes                     | 容器当前的内存使用量（单位：字节）                        |
| 内存                                                                                  | container_memory_working_set_bytes               | 容器工作集内存使用量（单位：字节）                        |
| 网络                                                                                         | container_network_receive_bytes_total            | 容器网络累积接收数据总量（单位：字节）                      |
| 网络                                                                                         | container_network_receive_errors_total           | 接收时遇到的错误累积计数                             |
| 网络                                                                                         | container_network_receive_packets_dropped_total  | 接收时丢弃的数据包的累积计数                           |
| 网络                                                                                         | container_network_receive_packets_total          | 接收数据包的累积计数                               |
| 网络                                                                                         | container_network_transmit_bytes_total           | 容器网络累积传输数据总量（单位：字节）                      |
| 网络                                                                                         | container_network_transmit_errors_total          | 传输时遇到的错误累积计数                             |
| 网络                                                                                         | container_network_transmit_packets_dropped_total | 传输时丢弃的数据包的累积计数                           |
| 网络                                                                                         | container_network_transmit_packets_total         | 传输数据包的累积计数                               |
| 进程                                                  | container_processes                              | 容器当前运行的进程数                               |
| 进程                                                  | container_sockets                                | 容器当前打开套接字的个数                             |
| 进程                                                  | container_file_descriptors                       | 容器当前打开文件描述符的个数                           |
| 进程                                                  | container_threads                                | 容器内当前运行的线程数                              |
| 进程                                                  | container_threads_max                            | 容器内允许运行的最大线程数                            |
| 进程                                                  | container_ulimits_soft                           | 容器内1号进程的软 ulimit 值。如果为-1，则无限制，优先级和nice除外 |
| 进程                                                  | container_spec_cpu_period                        | 容器分配的CPU周期                               |
| 进程                                                  | container_spec_cpu_shares                        | 容器分配的CPU份额                               |
| 进程                                                  | container_spec_memory_limit_bytes                | 容器可以使用的总内存量限制                            |
| 进程                                                  | container_spec_memory_reservation_limit_bytes    | 容器可以使用的预留内存限制                            |
| 进程                                                  | container_spec_memory_swap_limit_bytes           | 容器可以使用的虚拟内存限制                            |
| 进程                                                  | container_start_time_seconds                     | 容器已经运行的时间（单位：秒）                          |
| 进程                                                  | container_last_seen                              | 最近一次监控采集器感知到容器的时间                        |
| gpu                                                                                                                    | container_accelerator_memory_used_bytes          | 容器正在使用的GPU加速卡内存量（单位：字节）                  |
| gpu                                                                                                                    | container_accelerator_memory_total_bytes         | 总GPU加速卡可用内存量（单位：字节）                      |
| gpu                                                                                                                    | container_accelerator_duty_cycle                 | GPU加速卡实际运行时间百分比                          |
| 挂载卷                                                                                                                                                              | volume_stats_capacity_bytes                      | 卷总计容量（单位：字节）                             |
| 挂载卷                                                                                                                                                              | volume_stats_available_bytes                     | 卷可用容量（单位：字节）                             |
| 挂载卷                                                                                                                                                              | volume_stats_used_bytes                          | 卷使用量（单位：字节）                              |
| 挂载卷                                                                                                                                                              | volume_stats_inodes                              | 卷总计inode数量                               |
| 挂载卷                                                                                                                                                              | volume_stats_inodes_free                         | 卷可用inode数量                               |
| 挂载卷                                                                                                                                                              | volume_stats_inodes_used                         | 卷inode使用量                                |
   
挂载卷为自定义指标，可获取挂载的inline类型evs卷或磁盘类型emptyDir卷指标。
除"挂载卷"指标外的其他指标与cadvisor提供的指标一致。指标详细含义，可参考cadvisor文档：<https://github.com/google/cadvisor/blob/v0.39.0/docs/storage/prometheus.md>。
#### 基础配置
以下示例介绍Pod资源监控指标的基础配置方式，提供了Pod级别特性开关和自定义端口的能力。
```
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-exporter
  template:
    metadata:
      labels:
        app: nginx-exporter
      annotations:
        monitoring.cci.io/enable-pod-metrics: "true"
        monitoring.cci.io/metrics-port: "19100"
    spec:
      containers:
        - name: container-0
          image: 'nginx:alpine'
          resources:
            limits:
              cpu: 1000m
              memory: 2048Mi
            requests:
              cpu: 1000m
              memory: 2048Mi
      imagePullSecrets:
        - name: imagepull-secret
```
表2参数说明 
| Annotation                           | 功能                   | 可选值                | 默认值   |
|:---|:---|:---|:---|
| monitoring.cci.io/enable-pod-metrics | 是否开启监控指标特性           | true，false（不区分大小写） | true  |
| monitoring.cci.io/metrics-port       | 指定pod exporter启动监听端口 | 合法端口（1\~65535）     | 19100 |
   
#### 高级配置
**创建Secret**
Secret是一种加密存储的资源对象，您可以将认证信息、证书、私钥等保存在密钥中，从而解决了密码、token、密钥等敏感数据的配置问题。
如下示例中定义的Secret中包含三条Key-Value。
```
apiVersion: v1
kind: Secret
metadata:
  name: cert
type: Opaque
data:
   ca.crt: ...
   server.crt: ...
   server.key: ...
```
**配置tls证书**
用户可以通过配置annotation指定exporter server的tls证书套件，进行加密通信，并使用文件挂载的方式，关联证书secret。示例如下：
```
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-tls
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-tls
  template:
    metadata:
      labels:
        app: nginx-tls
      annotations:
        monitoring.cci.io/enable-pod-metrics: "true"
        monitoring.cci.io/metrics-port: "19100"
        monitoring.cci.io/metrics-tls-cert-reference: cert/server.crt
        monitoring.cci.io/metrics-tls-key-reference: cert/server.key
        monitoring.cci.io/metrics-tls-ca-reference: cert/ca.crt
        sandbox-volume.openvessel.io/volume-names: cert
    spec:
      volumes:
        - name: cert
          secret:
            secretName: cert
            defaultMode: 384
      containers:
        - name: container-0
          image: 'nginx:alpine'  
          resources:
            limits:
              cpu: 1000m
              memory: 2048Mi
            requests:
              cpu: 1000m
              memory: 2048Mi
          volumeMounts:
            - name: cert
              mountPath: /tmp/secret0
      imagePullSecrets:
        - name: imagepull-secret
```
表3tls证书参数说明 
| Annotation                                   | 功能              | 可选值                                      | 默认值       |
|:---|:---|:---|:---|
| monitoring.cci.io/metrics-tls-cert-reference | tls证书volume引用   | ${volume-name}/${volume-keyOrPath}（卷/路径） | 无（使用http） |
| monitoring.cci.io/metrics-tls-key-reference  | tls私钥volume引用   | ${volume-name}/${volume-keyOrPath}       | 无（使用http） |
| monitoring.cci.io/metrics-tls-ca-reference   | tls CA volume引用 | ${volume-name}/${volume-keyOrPath}       | 无（使用http） |
   
以上参数的值为tls的证书、私钥、CA文件所在存储卷的"卷名"和"路径"。
#### 获取资源监控指标
配置完上述监控属性后，在能访问Pod IP的VPC内，通过执行如下命令获取Pod的监控数据。
```
curl $podIP:$port/metrics
```
其中\<podIP\>为Pod的IP地址，\<port\>为监听端口，例如curl 192.168.XXX.XXX:19100/metrics
