How to include script and run it into kubernetes yaml?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running shell scripts from Kubernetes manifests is common for setup tasks, migrations, health probes, and scheduled automation. The key is deciding where script logic should live and how it should be injected into pods safely. This article covers production-friendly patterns using ConfigMap, initContainers, and lifecycle hooks.
Decide Where the Script Should Run
Before writing YAML, choose the execution context. Different contexts solve different problems.
initContainer: one-time setup before the main container starts.- Main container command: primary process logic, often entrypoint driven.
- Lifecycle hook: short pre-stop or post-start operations.
JoborCronJob: standalone task execution.
If your script initializes data or waits for dependencies, initContainer is usually best because failure is explicit and startup blocks until success.
Store Script Content in a ConfigMap
A clean pattern is storing the script body in a ConfigMap and mounting it as a file. This avoids rebuilding images for small script changes.
Then mount and run the script from an initContainer.
This separation keeps script logic visible and testable.
Inline Script with Command and Args
For short logic, inline commands are acceptable. Keep them minimal so debugging stays easy.
Use this style for one-off operational tasks, not large multi-step deployment logic.
Run Scripts During Container Lifecycle
Lifecycle hooks help with startup or shutdown coordination. Keep hook commands short and resilient.
A preStop hook is useful when your service needs a short drain period before termination.
Security and Reliability Considerations
Scripts in clusters should follow the same standards as app code.
- Pin image versions like
alpine:3.20instead of floating tags. - Run as non-root where possible using pod security context.
- Use
set -euin shell scripts so failures are explicit. - Externalize secrets into Kubernetes
Secret, not plainConfigMap. - Log meaningful status messages because script output is often your first debug signal.
You can also test scripts locally in the same container image before shipping them.
Common Pitfalls
- Embedding very large scripts directly in
args. This becomes unreadable. Move script content to a mounted file. - Forgetting execute permissions on mounted script files. Set
defaultModeor call/bin/sh script.shexplicitly. - Using
latestimage tags. Reproducibility suffers and debugging rollbacks gets harder. - Mixing setup logic into the main application start command. Failures become ambiguous. Prefer
initContainerboundaries. - Hardcoding secrets in script text. Pull credentials from environment variables backed by Kubernetes
Secretresources.
Summary
- Pick the right execution context:
initContainer, main command, lifecycle hook, orJob. - Store non-trivial scripts in
ConfigMapand mount them as files. - Keep inline script snippets short and operationally focused.
- Apply secure defaults: pinned images, clear failure behavior, and secret hygiene.
- Favor small, composable scripts that are easy to test and debug.

