I have a job that runs on deployment of our app. The job runs fine 99.9% of the time but every so often something goes wrong (in the application config) and we need to run commands by hand. Because the job has several initContainers it's not as simple as just running an instance of an application pod and execing into it.
We've considered creating a utility pod as part of the application (and this may be the way to go) but I was wondering if there was a good way to convert a completed job into a pod? I've experimented with getting the pod definition, editing by hand, and then applying; but since it's often urgent when we need to do this and it's quite possible to introduce errors when hand editing, this feels wrong.
I'm sure this can't be an unusual requirement, are there tools, commands, or approaches to this problem that I'm simply ignorant of?
"Converting a job into a pod" is basically what happens when you submit a Job resource to Kubernetes...so one option is just to delete and re-create the job:
kubectl get job myjob -o json | kubectl replace --force -f-
Poof, you have a new running pod!
You can use jq
to extract .spec.template
from the Job and attach the necessary bits to turn it into a Pod manifest:
kubectl get job myjob -o json |
jq '
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {"name": "example"}
} * .spec.template
' |
kubectl apply -f-
The above will create a pod named example
; change the name
attribute if you want to name it something else.