Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete oldest pod k8s cronJob
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: your-cron
spec:
schedule: "0 0 */2 * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
app: memoryLeak
spec:
containers:
- name: kubectl
image: bitnami/kubectl:1.11
imagePullPolicy: IfNotPresent
command: ["/bin/sh"]
args: ["-c", "kubectl delete pod $(kubectl get pod -l app=redis --sort-by=.metadata.creationTimestamp -o custom-columns=:metadata.name| tail -1)"]
securityContext:
privileged: false
allowPrivilegeEscalation: false
restartPolicy: OnFailure
serviceAccountName: pod-deleter-sa
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: pod-delete
namespace: default
labels:
app: memoryLeak
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["delete", "get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-delete-binding
namespace: default
labels:
app: memoryLeak
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pod-delete
subjects:
- kind: ServiceAccount
name: pod-deleter-sa
namespace: default
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-deleter-sa
namespace: default
labels:
app: memoryLeak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment