生产环境 Kubernetes 中出现了很多 Evicted Pod,我该怎么办呢?

系统 Linux
本文会分析为什么为产生 Evicted 实例、为什么 Evicted 实例没有被自动清理以及如何进行自动清理。

 [[439064]]

线上被驱逐实例数据

最近在线上发现很多实例处于 Evicted 状态,通过 pod yaml 可以看到实例是因为节点资源不足被驱逐,但是这些实例并没有被自动清理,平台的大部分用户在操作时看到服务下面出现 Evicted 实例时会以为服务有问题或者平台有问题的错觉,影响了用户的体验。而这部分 Evicted 状态的 Pod 在底层关联的容器其实已经被销毁了,对用户的服务也不会产生什么影响,也就是说只有一个 Pod 空壳在 k8s 中保存着,但需要人为手动清理。本文会分析为什么为产生 Evicted 实例、为什么 Evicted 实例没有被自动清理以及如何进行自动清理。

kubernetes 版本:v1.17 

  1. $ kubectl get pod | grep -i Evicted  
  2. cloud-1023955-84421-49604-5-deploy-c-7748f8fd8-hjqsh        0/1     Evicted   0          73d  
  3. cloud-1023955-84421-49604-5-deploy-c-7748f8fd8-mzd8x        0/1     Evicted   0          81d  
  4. cloud-1237162-276467-199844-2-deploy-7bdc7c98b6-26r2r       0/1     Evicted   0          18d 

Evicted 实例状态: 

  1. status:  
  2.   message: 'Pod The node had condition: [DiskPressure]. '  
  3.   phase: Failed  
  4.   reason: Evicted  
  5.   startTime: "2021-09-14T10:42:32Z" 

实例被驱逐的原因

kubelet 默认会配置节点资源不足时驱逐实例的策略,当节点资源不足时 k8s 会停止该节点上实例并在其他节点启动新实例,在某些情况下也可通过配置 --eviction-hard= 参数为空来禁用驱逐策略,在之前的生产环境中我们也确实这么做了。

节点资源不足导致实例被驱逐

k8s 中产生 Evicted 状态实例主要是因为节点资源不足实例主动被驱逐导致的,kubelet eviction_manager 模块会定期检查节点内存使用率、inode 使用率、磁盘使用率、pid 等资源,根据 kubelet 的配置当使用率达到一定阈值后会先回收可以回收的资源,若回收后资源使用率依然超过阈值则进行驱逐实例操作。

Eviction Signal Description
memory.available memory.available := node.status.capacity[memory] - node.stats.memory.workingSet
nodefs.available nodefs.available := node.stats.fs.available
nodefs.inodesFree nodefs.inodesFree := node.stats.fs.inodesFree
imagefs.available imagefs.available := node.stats.runtime.imagefs.available
imagefs.inodesFree imagefs.inodesFree := node.stats.runtime.imagefs.inodesFree
pid.available pid.available := node.stats.rlimit.maxpid - node.stats.rlimit.curproc

kubelet 中 pod 的 stats 数据一部分是通过 cAdvisor 接口获取到的,一部分是通过 CRI runtimes 的接口获取到的。

  •  memory.available:当前节点可用内存,计算方式为 cgroup memory 子系统中 memory.usage_in_bytes 中的值减去 memory.stat 中 total_inactive_file 的值;
  •  nodefs.available:nodefs 包含 kubelet 配置中 --root-dir 指定的文件分区和 /var/lib/kubelet/ 所在的分区磁盘使用率;
  •  nodefs.inodesFree:nodefs.available 分区的 inode 使用率;
  •  imagefs.available:镜像所在分区磁盘使用率;
  •  imagefs.inodesFree:镜像所在分区磁盘 inode 使用率;
  •  pid.available:/proc/sys/kernel/pid_max 中的值为系统最大可用 pid 数;

kubelet 可以通过参数 --eviction-hard 来配置以上几个参数的阈值,该参数默认值为 imagefs.available<15%,memory.available<100Mi,nodefs.available<10%,nodefs.inodesFree<5%,当达到阈值时会驱逐节点上的容器。

kubelet 驱逐实例时与资源处理相关的已知问题

1、kubelet 不会实时感知到节点内存数据的变化

kubelet 定期通过 cadvisor 接口采集节点内存使用数据,当节点短时间内内存使用率突增,此时 kubelet 无法感知到也不会有 MemoryPressure 相关事件,但依然会调用 OOMKiller 停止容器。可以通过为 kubelet 配置 --kernel-memcg-notification 参数启用 memcg api,当触发 memory 使用率阈值时 memcg 会主动进行通知;

memcg 主动通知的功能是 cgroup 中已有的,kubelet 会在 /sys/fs/cgroup/memory/cgroup.event_control 文件中写入 memory.available 的阈值,而阈值与 inactive_file 文件的大小有关系,kubelet 也会定期更新阈值,当 memcg 使用率达到配置的阈值后会主动通知 kubelet,kubelet 通过 epoll 机制来接收通知。

2、kubelet memory.available 不会计算 active page

kubelet 通过内存使用率驱逐实例时,内存使用率数据包含了 page cache 中 active_file 的数据,在某些场景下会因 page cache 过高导致内存使用率超过阈值会造成实例被驱逐,

由于在内存紧张时 inactive_file 会被内核首先回收,但在内存不足时,active_file 也会被内核进行回收,社区对此机制也有一些疑问,针对内核回收内存的情况比较复杂,社区暂时还未进行回应,详情可以参考 kubelet counts active page cache against memory.available (maybe it shouldn’t?)[1]。

kubelet 计算节点可用内存的方式如下: 

  1. #!/bin/bash  
  2. #!/usr/bin/env bash  
  3. # This script reproduces what the kubelet does  
  4. # to calculate memory.available relative to root cgroup.  
  5. # current memory usage  
  6. memory_capacity_in_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')  
  7. memory_capacity_in_bytes=$((memory_capacity_in_kb * 1024))  
  8. memory_usage_in_bytes=$(cat /sys/fs/cgroup/memory/memory.usage_in_bytes)  
  9. memory_total_inactive_file=$(cat /sys/fs/cgroup/memory/memory.stat | grep total_inactive_file | awk '{print $2}')  
  10. memory_working_set=${memory_usage_in_bytes}  
  11. if [ "$memory_working_set" -lt "$memory_total_inactive_file" ];  
  12. then  
  13.     memory_working_set=0  
  14. else  
  15.     memory_working_set=$((memory_usage_in_bytes - memory_total_inactive_file))  
  16. fi  
  17. memory_available_in_bytes=$((memory_capacity_in_bytes - memory_working_set))  
  18. memory_available_in_kb=$((memory_available_in_bytes / 1024))  
  19. memory_available_in_mb=$((memory_available_in_kb / 1024))  
  20. echo "memory.capacity_in_bytes $memory_capacity_in_bytes"  
  21. echo "memory.usage_in_bytes $memory_usage_in_bytes"  
  22. echo "memory.total_inactive_file $memory_total_inactive_file"  
  23. echo "memory.working_set $memory_working_set"  
  24. echo "memory.available_in_bytes $memory_available_in_bytes"  
  25. echo "memory.available_in_kb $memory_available_in_kb"  
  26. echo "memory.available_in_mb $memory_available_in_mb" 

驱逐实例未被删除原因分析

源码中对于 Statefulset 和 DaemonSet 会自动删除 Evicted 实例,但是对于 Deployment 不会自动删除。阅读了部分官方文档以及 issue,暂未找到官方对 Deployment Evicted 实例未删除原因给出解释。

statefulset:pkg/controller/statefulset/stateful_set_control.go 

  1. // Examine each replica with respect to its ordinal  
  2. for i :range replicas {  
  3.     // delete and recreate failed pods  
  4.     if isFailed(replicas[i]) {  
  5.         ssc.recorder.Eventf(set, v1.EventTypeWarning, "RecreatingFailedPod",  
  6.             "StatefulSet %s/%s is recreating failed Pod %s",  
  7.             set.Namespace,  
  8.             set.Name,  
  9.             replicas[i].Name)  
  10.         if err :ssc.podControl.DeleteStatefulPod(set, replicas[i]); err != nil {  
  11.             return &status, err  
  12.         }  
  13.         if getPodRevision(replicas[i]) == currentRevision.Name {  
  14.             status.CurrentReplicas--  
  15.         } 
  16.          if getPodRevision(replicas[i]) == updateRevision.Name {  
  17.             status.UpdatedReplicas--  
  18.         }  
  19.         ...... 

daemonset:pkg/controller/daemon/daemon_controller.go 

  1. func (dsc *DaemonSetsController) podsShouldBeOnNode(  
  2.   ......  
  3. ) (nodesNeedingDaemonPods, podsToDelete []string) {  
  4.   ......  
  5.     switch {  
  6.   ......  
  7.     case shouldContinueRunning:  
  8.     ......  
  9.         for _, pod :range daemonPods {  
  10.             if pod.DeletionTimestamp != nil {  
  11.                 continue  
  12.             }  
  13.             if pod.Status.Phase == v1.PodFailed {  
  14.                 // This is a critical place where DS is often fighting with kubelet that rejects pods.  
  15.                 // We need to avoid hot looping and backoff.  
  16.                 backoffKey :failedPodsBackoffKey(ds, node.Name)  
  17.                 ...... 

解决方案

1、团队里面有了一套 k8s 集群事件采集的链路,我们通过消费 k8s 中 pod 的相关事件来进行处理,消费事件时过滤 pod 中与 Evicted 实例相关的事件然后处理即可。

Evicted 实例判断逻辑: 

  1. const (  
  2.  podEvictedStatus = "Evicted"  
  3.  
  4. // 判断如果为 Evicted 状态的实例且 Pod 中容器数为 0 时直接删除 pod  
  5. if strings.ToLower(status) == strings.ToLower(podEvictedStatus) && len(pod.Status.ContainerStatuses) == 0 {  

2、社区有人提供通过在 kube-controller-manager 中配置 podgc controller –terminated-pod-gc-threshold 参数来自动清理: 

  1. Podgc controller flags:  
  2.       --terminated-pod-gc-threshold int32  
  3.                 Number of terminated pods that can exist before the terminated pod garbage collector starts deleting terminated pods. If  
  4.                 <= 0, the terminated pod garbage collector is disabled. (default 12500) 

该参数配置的是保留的异常实例数,默认值为 12500,但 podgc controller 回收 pod 时使用强杀模式不支持实例的优雅退出,因此暂不考虑使用。

3、其他处理方式可以参考社区中提供的 Kubelet does not delete evicted pods[2]。

总结

由于在之前的公司中对于稳定性的高度重视,线上节点并未开启驱逐实例的功能,因此也不会存在 Evicted 状态的实例,当节点资源严重不足时会有告警人工介入处理,以及还会有二次调度、故障自愈等一些辅助处理措施。本次针对 Evicted 相关实例的分析,发现 k8s 与操作系统之间存在了很多联系,如果要彻底搞清楚某些机制需要对操作系统的一些原理有一定的了解。 

 

责任编辑:庞桂玉 来源: 奇妙的Linux世界
相关推荐

2021-12-09 11:46:53

DockerIPLinux

2018-08-20 19:39:14

区块链职业岗位

2020-09-29 12:15:13

生死锁MySQL

2022-02-06 00:16:53

加密货币比特币以太坊

2020-07-21 10:05:48

技术研发指标

2019-06-03 10:53:49

MySQL硬盘数据库

2011-10-17 12:30:43

2022-05-19 08:01:49

PostgreSQL数据库

2018-01-28 20:39:39

戴尔

2022-09-05 09:02:01

服务器CPU服务

2009-11-03 08:56:02

linux死机操作系统

2022-12-19 11:31:57

缓存失效数据库

2022-07-05 11:48:47

MySQL死锁表锁

2022-04-22 10:30:07

框架JavaScript前端

2011-06-30 17:58:30

网站被K

2020-12-18 09:23:41

KubernetesDocker

2022-07-05 14:19:30

Spring接口CGLIB

2011-11-18 10:52:00

2011-11-16 10:02:48

DNSDNS记录DNS记录消失

2022-10-14 08:18:07

Guavaweb应用
点赞
收藏

51CTO技术栈公众号