How to include script and run it into kubernetes yaml?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- How to increase shm size of a kubernetes container --shm-size equivalent of docker
- How to initialize systemd services in kubernetes pod?
- How to Inject Environment Variables into Kubernetes Pods Before Deployment
- How to inject kubernetes secret into configuration file
- how to include xml in log-back.xml
- How to increase the maximum size of the AWS lambda deployment package RequestEntityTooLargeException?
- How to install a specific Chart version
- How to install Hive Metastore in Kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.