【问题标题】:Kubernetes has a ton of pods in error state that can't seem to be clearedKubernetes 有大量处于错误状态的 pod,似乎无法清除
【发布时间】:2022-01-07 17:53:50
【问题描述】:

我最初试图运行一个似乎卡在 CrashBackoffLoop 中的作业。这是服务文件:

apiVersion: batch/v1
kind: Job
metadata:
  name: es-setup-indexes
  namespace: elk-test
spec:
  template:
    metadata:
      name: es-setup-indexes
    spec:
      containers:
      - name: es-setup-indexes
        image: appropriate/curl
        command: ['curl -H  "Content-Type: application/json" -XPUT http://elasticsearch.elk-test.svc.cluster.local:9200/_template/filebeat -d@/etc/filebeat/filebeat.template.json']
        volumeMounts:
        - name: configmap-volume
          mountPath: /etc/filebeat/filebeat.template.json
          subPath: filebeat.template.json
      restartPolicy: Never

      volumes:
        - name: configmap-volume
          configMap:
            name: elasticsearch-configmap-indexes

我尝试删除作业,但只有运行以下命令才能起作用:

kubectl delete job es-setup-indexes --cascade=false

之后我在跑步时注意到:

kubectl get pods -w

我会得到大量处于错误状态的 pod,但我看不出有什么办法可以清理它们。这只是我运行 get pods 时输出的一小部分示例:

es-setup-indexes-zvx9c   0/1       Error     0         20h
es-setup-indexes-zw23w   0/1       Error     0         15h
es-setup-indexes-zw57h   0/1       Error     0         21h
es-setup-indexes-zw6l9   0/1       Error     0         16h
es-setup-indexes-zw7fc   0/1       Error     0         22h
es-setup-indexes-zw9bw   0/1       Error     0         12h
es-setup-indexes-zw9ck   0/1       Error     0         1d
es-setup-indexes-zwf54   0/1       Error     0         18h
es-setup-indexes-zwlmg   0/1       Error     0         16h
es-setup-indexes-zwmsm   0/1       Error     0         21h
es-setup-indexes-zwp37   0/1       Error     0         22h
es-setup-indexes-zwzln   0/1       Error     0         22h
es-setup-indexes-zx4g3   0/1       Error     0         11h
es-setup-indexes-zx4hd   0/1       Error     0         21h
es-setup-indexes-zx512   0/1       Error     0         1d
es-setup-indexes-zx638   0/1       Error     0         17h
es-setup-indexes-zx64c   0/1       Error     0         21h
es-setup-indexes-zxczt   0/1       Error     0         15h
es-setup-indexes-zxdzf   0/1       Error     0         14h
es-setup-indexes-zxf56   0/1       Error     0         1d
es-setup-indexes-zxf9r   0/1       Error     0         16h
es-setup-indexes-zxg0m   0/1       Error     0         14h
es-setup-indexes-zxg71   0/1       Error     0         1d
es-setup-indexes-zxgwz   0/1       Error     0         19h
es-setup-indexes-zxkpm   0/1       Error     0         23h
es-setup-indexes-zxkvb   0/1       Error     0         15h
es-setup-indexes-zxpgg   0/1       Error     0         20h
es-setup-indexes-zxqh3   0/1       Error     0         1d
es-setup-indexes-zxr7f   0/1       Error     0         22h
es-setup-indexes-zxxbs   0/1       Error     0         13h
es-setup-indexes-zz7xr   0/1       Error     0         12h
es-setup-indexes-zzbjq   0/1       Error     0         13h
es-setup-indexes-zzc0z   0/1       Error     0         16h
es-setup-indexes-zzdb6   0/1       Error     0         1d
es-setup-indexes-zzjh2   0/1       Error     0         21h
es-setup-indexes-zzm77   0/1       Error     0         1d
es-setup-indexes-zzqt5   0/1       Error     0         12h
es-setup-indexes-zzr79   0/1       Error     0         16h
es-setup-indexes-zzsfx   0/1       Error     0         1d
es-setup-indexes-zzx1r   0/1       Error     0         21h
es-setup-indexes-zzx6j   0/1       Error     0         1d
kibana-kq51v   1/1       Running   0         10h

但是,如果我查看这些工作,我就再也找不到与此相关的东西了:

$ kubectl get jobs --all-namespaces                                                                              
NAMESPACE     NAME               DESIRED   SUCCESSFUL   AGE
kube-system   configure-calico   1         1            46d

我还注意到 kubectl 的响应似乎很慢。我不知道 pod 是否在不断尝试重新启动或处于某种损坏状态,但如果有人能告诉我如何排除故障,那就太好了,因为我在 kubernetes 中没有遇到过类似的问题。

Kube 信息:

$ kubectl version 
Client Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.1", GitCommit:"b0b7a323cc5a4a2019b2e9520c21c7830b7f708e", GitTreeState:"clean", BuildDate:"2017-04-03T20:44:38Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.1", GitCommit:"b0b7a323cc5a4a2019b2e9520c21c7830b7f708e", GitTreeState:"clean", BuildDate:"2017-04-03T20:33:27Z", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}

【问题讨论】:

  • 如何输出:$kubectl describe pods
  • 您是如何尝试删除 pod 的?你说的“它只适用于--cascade=false”是什么意思?有错误吗?
  • @turkenh 所以我最终确实运行了该命令。我看到了他们在哪些节点上运行,通过 ssh 连接到这些节点,并手动删除了所有与该映像与 docker ps -a 匹配的旧 docker 映像。删除旧容器后,即使我手动删除了它们,kubectl 似乎仍会报告它们。我不知道我是否应该尝试启动更多节点并迁移到新节点并拆除旧节点,或者是否有办法弄清楚如何让 kube 与状态同步备份码头工人。
  • @johnharris85 啊,感谢您手动删除它们。花了大约 2 个小时,因为有 9292 个错误的 pod。

标签: error-handling kubernetes kubectl


【解决方案1】:

kubectl delete pods --field-selector status.phase=Failed -n <your-namespace>

...清理您的命名空间中所有失败的 pod。

【讨论】:

  • 尝试使用--field-selector=status.phase=Failed
  • kubectl get pods -o name -n <your-namespace> --field-selector status.phase=Failed | xargs kubectl delete -n <your-namespace>
【解决方案2】:

这是一个快速修复它的方法:)

kubectl get pods | grep Error | cut -d' ' -f 1 | xargs kubectl delete pod

编辑:如果您使用的是旧版本的 k8s,请添加标志 -a

【讨论】:

  • 谢谢。是的,我之前应该使用过 xargs,所以它是并行与串行完成的。
  • 请记住,这也会删除所有标题中出现错误的 pod。下面的答案比较靠谱。
【解决方案3】:

我通常使用此命令删除所有 Error pod。 kubectl delete pod `kubectl get pods --namespace <yournamespace> | awk '$3 == "Error" {print $1}'` --namespace <yournamespace>

【讨论】:

  • 谢谢。自从升级到 1.7+ 后,我没有遇到过这个问题。
【解决方案4】:

我有很多 pod 卡在以下状态

  • ContainerCannotRun
  • 错误
  • ImagePullBackOff

出于正当理由,这些 pod 处于上述状态。但是,即使后来问题得到解决,它们也​​没有自动清理。

要清理,手动以下的东西不起作用:

# Doesn't work
kubectl get pods --field-selector status.phase=Error 

# Doesn't work
kubectl get pods \
    --field-selector=status.phase=Error

# Doesn't work
kubectl get pods \
    --field-selector="status.phase=Error"

# Doesn't work
kubectl get pods \
    --field-selector="status.phase==Error"


以下过滤具有我们希望保留的状态的 pod 的方法非常有效

# Validate list of pods.
# Please add more status that we don't want to delete
kubectl get pods \
    --field-selector="status.phase!=Succeeded,status.phase!=Running"

# Delete pods that matches the filter
kubectl delete pods \
    --field-selector="status.phase!=Succeeded,status.phase!=Running"

【讨论】:

    【解决方案5】:

    解决方案正如评论中提到的@johnharris85。我不得不手动删除所有的 pod。为此,我运行了以下命令:

    kubectl get pods -w | tee all-pods.txt
    

    这转储了我所有的 pod,然后只过滤和删除我想要的。

    kubectl delete pod $(more all-pods.txt | grep es-setup-index | awk '{print $1}')
    

    注意:我有大约 9292 个 pod,大约需要 1-2 小时才能将它们全部删除。

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 2021-09-15
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多