3 min read | by Jordi Prats
If we take a look all the possible properties for a Pod definition we might notice that there's one to limit the time a Pod can be running:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
activeDeadlineSeconds: 10
containers:
- args:
- sleep
- 24h
image: alpine
name: test
If we give it a try it will work as expected for a single Pod:
$ kubectl get pods --watch
NAME READY STATUS RESTARTS AGE
test 0/1 DeadlineExceeded 0 39s
For objects such a Job (or a CronJob) that we might want to be able to cancel if a Pod runs for too long it makes perfect sense:
apiVersion: batch/v1
kind: CronJob
metadata:
name: democron-sitemapgen
spec:
schedule: '* * * * *'
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
name: democron-sitemapgen
spec:
template:
spec:
restartPolicy: OnFailure
activeDeadlineSeconds: 40
containers:
- name: democron-sitemapgen
image: alpine:latest
command:
- sleep
- 24h
Trying this out we will be able to see how it kills the Pod once the activeDeadlineSeconds is reached:
$ kubectl get pods --watch
NAME READY STATUS RESTARTS AGE
democron-sitemapgen-27440182--1-mcldv 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-mcldv 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-mcldv 0/1 ContainerCreating 0 1s
democron-sitemapgen-27440182--1-mcldv 1/1 Running 0 12s
democron-sitemapgen-27440182--1-mcldv 1/1 DeadlineExceeded 0 40s
democron-sitemapgen-27440182--1-t874q 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-t874q 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-t874q 0/1 ContainerCreating 0 1s
democron-sitemapgen-27440182--1-t874q 1/1 Running 0 12s
democron-sitemapgen-27440182--1-mcldv 0/1 Error 0 77s
democron-sitemapgen-27440182--1-t874q 1/1 DeadlineExceeded 0 40s
democron-sitemapgen-27440182--1-cjbzw 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-cjbzw 0/1 Pending 0 0s
democron-sitemapgen-27440182--1-cjbzw 0/1 ContainerCreating 0 0s
democron-sitemapgen-27440182--1-cjbzw 1/1 Running 0 12s
(...)
But if we try to the same on a Deployment like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deploy
labels:
demo: test
spec:
replicas: 1
selector:
matchLabels:
demo: test
template:
metadata:
labels:
demo: test
spec:
activeDeadlineSeconds: 10
containers:
- args:
- sleep
- 24h
image: alpine
name: test
It won't allow us to have it on the Pod template:
$ kubectl apply -f test.yaml
The Deployment "test-deploy" is invalid: spec.template.spec.activeDeadlineSeconds: Forbidden: activeDeadlineSeconds in ReplicaSet is not Supported
A deployment is intended for workloads that are supposed to be running perpetually, so the activeDeadlineSeconds doesn't make sense for that use case. If we want to be able to refresh the Pods on a Deployment (or a StatefulSet) we can use the bestby controller. With it we can use a label to defined how often Pods need to be refreshed
Posted on 07/03/2022