Env. Variables & Probes
Kubernetes allows you to configure runtime behavior of containers using environment variables, and to monitor their health using liveness and readiness probes. These features are essential for building reliable, configurable, and observable applications in the cluster.
Environment Variables
You can pass key-value pairs into containers using environment variables. These can be hardcoded, referenced from ConfigMaps, Secrets, or even dynamically derived from field references.
Static Environment Variables
From ConfigMap
Or individual keys:
From Secret
From Pod Metadata
Probes Overview
Kubernetes uses probes to check if a container is:
- Alive (liveness probe): Whether the app should be restarted
- Ready (readiness probe): Whether the app is ready to receive traffic
- Started (startup probe): Whether the app has finished starting up
Each probe runs a check (HTTP request, TCP socket, or command) and takes action based on success or failure.
Liveness Probe
Restarts the container if the probe fails repeatedly.
Readiness Probe
Used to signal when the container is ready to receive traffic. If the probe fails, the Pod is removed from Service endpoints.
Startup Probe
Useful for applications that take a long time to initialize. Prevents premature liveness failures during startup.
Best Practices
- Use readiness probes to avoid routing traffic to unready pods.
- Use liveness probes for self-healing on deadlocks or hung apps.
- Use startup probes for slow-starting applications.
- Avoid setting
initialDelaySeconds
too low — allow the app to start first. - Prefer HTTP or command probes for rich diagnostics.
- Avoid hardcoding values inside your images. Use environment variables, ConfigMaps, and Secrets for maximum flexibility and security.
Summary
- Environment variables make your containers configurable and portable.
- Use ConfigMaps and Secrets for dynamic or sensitive values.
- Probes help keep your apps healthy and ready for traffic.