kubectl YAML config file equivalent of kubectl run ... -i --tty ...
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The YAML equivalent of kubectl run ... -i --tty ... is a Pod manifest with stdin: true and tty: true set on the container. In practice, you also usually want restartPolicy: Never, because interactive shells and debug pods are rarely meant to restart forever.
Basic translation
Imperative command:
YAML equivalent:
Here:
- '
stdin: truecorresponds to-i' - '
tty: truecorresponds to--tty' - '
commandcorresponds to the command after--'
Then create and attach:
Why restartPolicy: Never matters
Interactive debug pods are usually short-lived. If you omit restartPolicy: Never, the container may restart after you exit the shell, which is often not what you intended.
That makes the pod feel "haunted" because it comes back instead of finishing cleanly.
For one-off interactive work, Never is the usual declarative equivalent of the imperative debug intent.
Reattach versus exec
After the pod exists, you have two different actions:
- '
kubectl attachreconnects to the main container process' - '
kubectl exec -itstarts a new command inside the running container'
For example:
This is useful if the original shell exited or if you prefer launching a fresh interactive shell session.
Generate YAML from the command first if needed
If you want a quick starting point, Kubernetes can generate the YAML skeleton for you:
That is often the fastest way to avoid forgetting fields. You can save the output, edit it, and then version-control the result.
Cleaning up the interactive pod
Because this pattern is often used for debugging, cleanup matters too. When you are done, remove the pod explicitly so it does not linger in the namespace:
That is one of the main differences between a quick terminal session and a declarative manifest stored in version control. YAML makes the pod reproducible, but it also means you should be deliberate about lifecycle and cleanup.
Add other fields normally
Once you have the interactive pod skeleton, you can extend it with the normal Pod spec fields:
- environment variables
- volume mounts
- service account
- namespace
- resource limits
Example with environment variables:
The interactive settings are just two booleans in a normal Pod definition.
Common Pitfalls
The biggest mistake is setting only tty: true without stdin: true. A TTY alone does not make the container interactive in the same way as the imperative command.
Another mistake is forgetting restartPolicy: Never for a shell-style pod. Kubernetes may restart it after you exit, which is usually confusing for debugging.
Developers also sometimes attach to an image that does not actually contain /bin/bash or /bin/sh. Minimal images may not have an interactive shell at all.
Finally, do not forget that kubectl run is imperative convenience. YAML is the durable representation if you want the pod definition reviewed, reused, or stored in version control.
Summary
- The YAML equivalent of
kubectl run -i --ttyisstdin: trueplustty: true. - Use
restartPolicy: Neverfor interactive debug pods. - Put the intended shell or command in the container
commandfield. - Use
kubectl attachto reconnect orkubectl exec -itto start a new shell. - Generate a starting manifest with
--dry-run=client -o yamlif you want a quick template.

