提问者:小点点

无法删除所有库伯内特斯命名空间中的所有被驱逐的podCronjob


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

有时会有从几个到几十个被驱逐的pod。我创建了一个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)中运行。


共2个答案

匿名用户

听起来升级后:

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

不再接收失败的豆荚。可能是:

  • 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,因此您还可以查看您的< code > kubectl-runner Pod的日志:

kubectl logs kubectl-runner-xxxxx

更新:

根据日志文件,它看起来像默认:默认服务号没有足够的权限,这将修复它:

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

但是,如果您想更具限制性,则必须创建一个更有限的ClusterRole或Role(如果您希望它仅限于命名空间)

匿名用户

您的角色需要更改为 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"]

您拥有的RoleBind用于Development命名空间中的服务号sa-cronjob-runer。但您正在运行的cron实际上位于默认命名空间中。因此它使用默认命名空间中的默认服务号。

因此,要么在cronjob和< code > service account name:sa-cron job-runner 中指定名称空间< code>development

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

或者更改角色绑定以将群集角色绑定到默认命名空间中的默认服务帐户

---
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