【问题标题】:Cannot Remove all evicted pods in all Kubernetes namespaces Cronjob无法删除所有 Kubernetes 命名空间 Cronjob 中所有被驱逐的 pod
【发布时间】:2020-06-30 16:14:39
【问题描述】:

我的 Kubernetes 集群有内存压力限制,我需要修复(稍后)。

有时从几个被驱逐的豆荚到几十个不等。 我创建了一个 Cronjob 规范来清理被驱逐的 pod。我在里面测试了命令,它在 powershell 上运行良好。

但是,无论我是否在规范中指定命名空间,将其部署到存在的每个命名空间,脚本似乎都不会删除我被驱逐的 pod。

原文:

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: delete-evicted-pods
spec:
  schedule: "*/30 * * * *"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: kubectl-runner
            image: bitnami/kubectl:latest
            command: ["sh", "-c", "kubectl get pods --all-namespaces --field-selector 'status.phase==Failed' -o json | kubectl delete -f -"]
          restartPolicy: OnFailure

我尝试使用关联的 RBAC 创建脚本,但也没有成功。

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: development
  name: cronjob-runner
rules:
- apiGroups:
  - extensions
  - apps
  resources:
  - deployments
  verbs:
  - 'patch'

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cronjob-runner
  namespace: development
subjects:
- kind: ServiceAccount
  name: sa-cronjob-runner
  namespace: development
roleRef:
  kind: Role
  name: cronjob-runner
  apiGroup: ""

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-cronjob-runner
  namespace: development
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: delete-all-failed-pods
spec:
  schedule: "*/30 * * * *"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-cronjob-runner
          containers:
          - name: kubectl-runner
            image: bitnami/kubectl:latest
            command: 
              - /bin/sh
              - -c
              - kubectl get pods --all-namespaces --field-selector 'status.phase==Failed' -o json | kubectl delete -f -
          restartPolicy: OnFailure 

我意识到我应该定义更好的内存限制,但这个功能在我将 k8s 从 1.14 升级到 1.16 之前就可以使用。

我做错了什么或遗漏了什么?如果有帮助,我正在 Azure (AKS) 中运行。

【问题讨论】:

    标签: kubernetes kubernetes-helm kubectl kong kubernetes-cronjob


    【解决方案1】:

    升级后听起来是这样的:

    kubectl get pods --all-namespaces --field-selector 'status.phase==Failed'`
    

    不再拾取失败的 pod。可能是:

    • kubectl/apiserver 版本不匹配
    • 凭据/服务帐户权限
    • (?)

    您可以尝试运行调试 pod 来验证:

    $ kubectl run -i --tty --rm debug --image=bitnami/kubectl:latest --restart=Never -- get pods --all-namespaces --field-selector 'status.phase==Failed'
    

    Kubernetes 中的每个作业都会创建一个 Pod,因此您还可以查看 kubectl-runner pod 的日志:

    kubectl logs kubectl-runner-xxxxx
    

    更新:

    根据日志文件,default:default 服务帐户似乎没有足够的权限,这将解决它:

    kubectl create clusterrolebinding myadmin-binding --clusterrole=cluster-admin --serviceaccount=default:default
    
    

    但是,如果您想要更严格,您将不得不创建一个更有限的 ClusterRole 或 Role(如果您希望它限制在命名空间中)

    【讨论】:

    • 作为响应返回:来自服务器的错误(禁止):pod 被禁止:用户“system:serviceaccount:default:default”无法在集群范围 pod 的 API 组“”中列出资源“pod” “debug” 删除 pod 默认/调试终止(错误)
    • apiGroup 应该是空的吗?
    • 我刚刚更新了答案。看起来这可能会解决它:kubectl create clusterrolebinding myadmin-binding --clusterrole=cluster-admin --serviceaccount=default:default
    【解决方案2】:

    您的角色需要更改为 ClusterRole,因为您在 kubectl 命令中使用了 --all-namespaces

    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: cronjob-runner
    rules:
    - apiGroups: [""] # "" indicates the core API group
      resources: ["pods"]
      verbs: ["get", "watch", "list"]
    

    您拥有的RoleBindingdevelopment 命名空间中的服务帐户sa-cronjob-runner。但是您正在运行的 cron 实际上是在 default 命名空间中。因此它使用来自default 命名空间的default 服务帐户。

    所以要么在 cronjob 中指定命名空间 development,要么在 serviceAccountName: sa-cronjob-runner 中指定命名空间

    apiVersion: batch/v1beta1
    kind: CronJob
    metadata:
      name: delete-evicted-pods
      namespace: development
    spec:
      schedule: "*/30 * * * *"
      failedJobsHistoryLimit: 1
      successfulJobsHistoryLimit: 1
      jobTemplate:
        spec:
          template:
            spec:
              serviceAccountName: sa-cronjob-runner
              containers:
              - name: kubectl-runner
                image: bitnami/kubectl:latest
                command: ["sh", "-c", "kubectl get pods --all-namespaces --field-selector 'status.phase==Failed' -o json | kubectl delete -f -"]
              restartPolicy: OnFailure
    

    或者更改角色绑定以将 ClusterRole 绑定到 default 命名空间中的 default 服务帐户

    ---
    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: cronjob-runner
      namespace: development
    subjects:
    - kind: ServiceAccount
      name: default
      namespace: default
    roleRef:
      kind: Role
      name: cronjob-runner
      apiGroup: rbac.authorization.k8s.io
    

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 1970-01-01
      • 2019-11-14
      • 2012-03-08
      • 2021-05-16
      • 2020-08-30
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多