Created
December 2, 2021 11:59
-
-
Save slayer/f91ea3bc47c9e8d09c57ffc3a0515a88 to your computer and use it in GitHub Desktop.
Auto delete failed pods in Kubernetes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Auto delete pods with status.phase=Failed via CronJob | |
--- | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: sa-auto-delete-failed-pods | |
namespace: kube-system | |
--- | |
kind: ClusterRole | |
apiVersion: rbac.authorization.k8s.io/v1 | |
metadata: | |
name: auto-delete-failed-pods | |
rules: | |
- apiGroups: [""] # "" indicates the core API group | |
resources: ["pods"] | |
verbs: ["get", "watch", "list", "delete"] | |
--- | |
apiVersion: rbac.authorization.k8s.io/v1 | |
kind: ClusterRoleBinding | |
metadata: | |
name: auto-delete-failed-pods | |
roleRef: | |
apiGroup: rbac.authorization.k8s.io | |
kind: ClusterRole | |
name: auto-delete-failed-pods | |
subjects: | |
- kind: ServiceAccount | |
name: sa-auto-delete-failed-pods | |
namespace: kube-system | |
--- | |
apiVersion: batch/v1 | |
kind: CronJob | |
metadata: | |
name: auto-delete-failed-pods-cronjob | |
namespace: kube-system | |
spec: | |
schedule: "*/30 * * * *" | |
failedJobsHistoryLimit: 1 | |
successfulJobsHistoryLimit: 1 | |
jobTemplate: | |
spec: | |
template: | |
spec: | |
serviceAccountName: sa-auto-delete-failed-pods | |
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: Never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment