GKE 容器被“内存 cgroup 内存不足”杀死,但监控、本地测试和 pprof 显示使用率远低于限制

Joh*_*röm 7 out-of-memory go kubernetes google-kubernetes-engine

我最近将一个新的容器映像推送到我的 GKE 部署之一,并注意到 API 延迟上升并且请求开始返回 502。

查看日志我发现容器由于 OOM 开始崩溃:

Memory cgroup out of memory: Killed process 2774370 (main) total-vm:1801348kB, anon-rss:1043688kB, file-rss:12884kB, shmem-rss:0kB, UID:0 pgtables:2236kB oom_score_adj:980
Run Code Online (Sandbox Code Playgroud)

查看内存使用情况图,看起来 pod 使用的内存并没有超过 50MB。我的原始资源请求是:

...
spec:
...
  template:
...
    spec:
...
      containers:
      - name: api-server
...
        resources:
          # You must specify requests for CPU to autoscale
          # based on CPU utilization
          requests:
            cpu: "150m"
            memory: "80Mi"
          limits:
            cpu: "1"
            memory: "1024Mi"
      - name: cloud-sql-proxy
        # It is recommended to use the latest version of the Cloud SQL proxy
        # Make sure to update on a regular schedule!
        image: gcr.io/cloudsql-docker/gce-proxy:1.17
        resources:
          # You must specify requests for CPU to autoscale
          # based on CPU utilization
          requests:
            cpu: "100m"
...
Run Code Online (Sandbox Code Playgroud)

然后我尝试将 API 服务器的请求提高到 1GB,但没有帮助。最后,帮助的是将容器映像恢复到以前的版本:

在此处输入图片说明

查看 golang 二进制文件中的更改,没有明显的内存泄漏。当我在本地运行它时,它最多使用 80MB 的内存,即使在来自与生产中相同请求的负载下也是如此。

我从 GKE 控制台获得的上图还显示了 pod 使用的内存远低于 1GB 的限制。

所以我的问题是:当 GKE 监控和在本地运行它只使用 1GB 限制中的 80MB 时,什么可能导致 GKE 为 OOM 终止我的进程?

=== 编辑 ===

在此处输入图片说明

添加同一中断的另一个图表。这次拆分 pod 中的两个容器。如果我理解正确,这里的指标是不可驱逐的 container/memory/used_bytes

container/memory/used_bytes GA
Memory usage
GAUGE, INT64, By
k8s_container   Memory usage in bytes. Sampled every 60 seconds.
memory_type: Either `evictable` or `non-evictable`. Evictable memory is memory that can be easily reclaimed by the kernel, while non-evictable memory cannot.
Run Code Online (Sandbox Code Playgroud)

编辑 2021 年 4 月 26 日

我尝试将部署 yaml 中的资源字段更新为请求的 1GB RAM 和 1GB RAM 限制,如 Paul 和 Ryan 建议的那样:

        resources:
          # You must specify requests for CPU to autoscale
          # based on CPU utilization
          requests:
            cpu: "150m"
            memory: "1024Mi"
          limits:
            cpu: "1"
            memory: "1024Mi"
Run Code Online (Sandbox Code Playgroud)

不幸的是,更新后结果相同kubectl apply -f api_server_deployment.yaml

{
 insertId: "yyq7u3g2sy7f00"  
 jsonPayload: {
  apiVersion: "v1"   
  eventTime: null   
  involvedObject: {
   kind: "Node"    
   name: "gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy"    
   uid: "gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy"    
  }
  kind: "Event"   
  message: "Memory cgroup out of memory: Killed process 1707107 (main) total-vm:1801412kB, anon-rss:1043284kB, file-rss:9732kB, shmem-rss:0kB, UID:0 pgtables:2224kB oom_score_adj:741"   
  metadata: {
   creationTimestamp: "2021-04-26T23:13:13Z"    
   managedFields: [
    0: {
     apiVersion: "v1"      
     fieldsType: "FieldsV1"      
     fieldsV1: {
      f:count: {
      }
      f:firstTimestamp: {
      }
      f:involvedObject: {
       f:kind: {
       }
       f:name: {
       }
       f:uid: {
       }
      }
      f:lastTimestamp: {
      }
      f:message: {
      }
      f:reason: {
      }
      f:source: {
       f:component: {
       }
       f:host: {
       }
      }
      f:type: {
      }
     }
     manager: "node-problem-detector"      
     operation: "Update"      
     time: "2021-04-26T23:13:13Z"      
    }
   ]
   name: "gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy.16798b61e3b76ec7"    
   namespace: "default"    
   resourceVersion: "156359"    
   selfLink: "/api/v1/namespaces/default/events/gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy.16798b61e3b76ec7"    
   uid: "da2ad319-3f86-4ec7-8467-e7523c9eff1c"    
  }
  reason: "OOMKilling"   
  reportingComponent: ""   
  reportingInstance: ""   
  source: {
   component: "kernel-monitor"    
   host: "gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy"    
  }
  type: "Warning"   
 }
 logName: "projects/questions-279902/logs/events"  
 receiveTimestamp: "2021-04-26T23:13:16.918764734Z"  
 resource: {
  labels: {
   cluster_name: "api-us-central-1"    
   location: "us-central1-a"    
   node_name: "gke-api-us-central-1-e2-highcpu-4-nod-dfe5c3a6-c0jy"    
   project_id: "questions-279902"    
  }
  type: "k8s_node"   
 }
 severity: "WARNING"  
 timestamp: "2021-04-26T23:13:13Z"  
}
Run Code Online (Sandbox Code Playgroud)

Kubernetes 似乎几乎立即杀死了使用 1GB 内存的容器。但同样,指标显示容器仅使用 2MB 内存:

在此处输入图片说明

我再次被难住了,因为即使在负载下这个二进制文件在我本地运行时也不会使用超过 80MB。

我也试过跑步go tool pprof <url>/debug/pprof/heap。它显示了几个不同的值,因为 Kubernetes 不断地颠簸容器。但没有超过~20MB,也没有异常的内存使用

编辑 04/27

我尝试为 pod 中的两个容器设置 request=limit:

 requests:
   cpu: "1"
   memory: "1024Mi"
 limits:
   cpu: "1"
   memory: "1024Mi"
...
requests:
  cpu: "100m"
  memory: "200Mi"
limits:
  cpu: "100m"
  memory: "200Mi"
Run Code Online (Sandbox Code Playgroud)

但它也不起作用:

Memory cgroup out of memory: Killed process 2662217 (main) total-vm:1800900kB, anon-rss:1042888kB, file-rss:10384kB, shmem-rss:0kB, UID:0 pgtables:2224kB oom_score_adj:-998
Run Code Online (Sandbox Code Playgroud)

内存指标仍以个位数 MB 显示使用情况。

更新 04/30

我通过煞费苦心地逐一检查我的最新提交,确定了似乎导致此问题的更改。

在违规提交中,我有几行像

type Pic struct {
        image.Image
        Proto *pb.Image
}
...

pic.Image = picture.Resize(pic, sz.Height, sz.Width)
...
Run Code Online (Sandbox Code Playgroud)

哪里picture.Resize最终叫resize.Resize。我把它改成:

type Pic struct {
        Img   image.Image
        Proto *pb.Image
 }
...
pic.Img = picture.Resize(pic.Img, sz.Height, sz.Width)
Run Code Online (Sandbox Code Playgroud)

这解决了我的直接问题,容器现在运行良好。但它没有回答我原来的问题:

  1. 为什么这些行会导致 GKE OOM 我的容器?
  2. 为什么 GKE 内存指标显示一切正常?

pau*_*aul 5

我猜这是由Pod QoS 等级引起的

当系统过度使用时,QoS 类别会确定哪个 Pod 首先被终止,以便将释放的资源分配给更高优先级的 Pod。

在你的情况下,你的 Pod 的 QoS 是Burstable

每个正在运行的进程都有一个 OutOfMemory(OOM) 分数。系统通过比较所有正在运行的进程的OOM分数来选择要杀死的进程。当需要释放内存时,得分最高的进程将被杀死。有关如何score计算的详细信息,请参阅内核 oom 分数如何计算?

如果两个 pod 都在班级中,哪个 pod 将首先被杀死Burstable

简而言之,系统将按百分比杀死使用比另一个更多的请求内存的一个。

Pod A

used: 90Mi
requests: 100Mi
limits: 200Mi
Run Code Online (Sandbox Code Playgroud)
Pod B

used: 150Mi
requests: 200Mi
limits: 400Mi
Run Code Online (Sandbox Code Playgroud)

Pod A之前会被杀死Pod B,因为它使用了所请求内存的 90%,而Pod B只使用了所请求内存的 75%。


Mih*_*ert 3

确保“保证”的 QoS 等级对您的场景没有帮助。您的进程之一会导致父 cgroup 超出其内存限制(反过来由您针对相应容器指定的内存限制值设置),并且 OOM 杀手会终止它。这不是 pod 驱逐,因为您可以在日志中清楚地看到 OOM 杀手的商标消息。在这种情况下,如果另一个 pod 分配了如此多的内存,导致节点面临内存压力,那么“保证”的 QoS 类别将会有所帮助 - 在这种情况下,您的“保证”的 pod 将不会受到影响。但就您而言,Kubelet 在这一切中永远不会得到任何消息 - 就像决定完全驱逐 pod 一样 - 因为OOM 杀手的行动速度更快

Burak Serdar 在其评论中有一个很好的观点——大内存块的临时分配。考虑到在您的情况下,从您粘贴的消息中收集数据的分辨率是 60 秒,情况很可能就是这样。那是很多时间。不到 1 秒即可轻松充满 GB RAM。我的假设是,内存“峰值”永远不会被渲染,因为指标永远不会及时收集(即使您直接查询 cAdvisor,这也会很棘手,因为它收集指标的分辨率为 10-15 秒)。

如何更多地了解正在发生的事情?几个想法:

  • 有一些工具可以显示应用程序实际分配的数量,直至框架级别。在 .NET 中,dotMemory 是一种常用的工具,可以在容器内运行并捕获发生的情况。Go 可能有一个等价的东西。这种方法的问题在于,当容器被 OOMKilled 时,该工具也会随之被删除
  • 在您自己的应用程序中编写有关内存使用情况的详细信息。在这里,您会发现一个影片,该影片捕获了一个分配内存的进程,直到其父容器被 OOM 杀死。相应的 .NET 应用程序会不时地将其使用的内存量写入控制台,即使容器不再存在,Kubernetes 日志也会显示这些内存量,从而可以查看发生了什么
  • 限制应用程序,使其处理少量数据(例如,如果每分钟仅处理一张图片,则暂时从内存的角度来看会发生什么)
  • 查看详细的 OOM Killer 内核日志以查看 cgroup 中的所有进程。在一个容器内拥有多个进程是完全有效的(就像在该容器中除了 PID 1 之外的其他进程一样),并且 OOM 杀手很可能杀死其中任何一个进程。在这种情况下,你可能会遇到意想不到的转折。然而,在您的场景中,主进程似乎已终止,否则容器不会被 OOMkilled,因此这种情况不太可能发生。

只是为了完整性:底层框架可以强制执行比容器内存限制更低的限制。例如,在 .NET 中,当在具有内存限制的容器中运行时,该值为 75%。换句话说,在具有 2,000 MiB 限制的容器内分配内存的 .NET 应用程序将在 1,500 MiB 时出错。但在这种情况下,您会得到退出代码 139 (SIGSEGV)。这似乎并不适用于此,因为 OOM 杀手会终止进程,并且从内核日志中可以清楚地看到所有 1 GiB 均已实际使用 ( anon-rss:1043688kB)。据我所知,尽管社区一再要求,Go 还没有类似的设置。