Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Kubernetes has revolutionized the way we deploy and manage containerized applications. Two powerful features that often confuse newcomers are multi-container pods and environment variables. In this post, we’ll demystify these concepts and walk through a practical example of creating a multi-container pod with custom environment variables.
Understanding Multi-Container Pods#
In Kubernetes, a pod is the smallest deployable unit. While it’s common to have a single container per pod, Kubernetes also supports multi-container pods. These are useful when you have tightly coupled containers that need to share resources.
The Power of Environment Variables#
Environment variables allow you to pass configuration data to your containers at runtime. This is crucial for creating flexible, portable applications that can adapt to different environments without code changes.
Hands-on: Creating a Multi-Container Pod#
Let’s dive into a practical example. We’ll create a pod with a main application container and two init containers, demonstrating both multi-container functionality and environment variable usage.
1apiVersion: v12kind: Pod3metadata:4 name: myapp-pod5 labels:6 app.kubernetes.io/name: MyApp7spec:8 containers:9 - name: myapp-container10 image: busybox:1.2811 env:12 - name: FIRSTNAME13 value: "Piyush"14 command: ["sh", "-c", "echo The app is running! && sleep 3600"]15 initContainers:16 - name: init-myservice17 image: busybox:1.2818 command: ["sh", "-c"]19 args:20 [21 "until nslookup myservice.default.svc.cluster.local; do echo waiting for myservice; sleep 2; done",22 ]23 - name: init-mydb24 image: busybox:1.2825 command: ["sh", "-c"]26 args:27 [28 "until nslookup mydb.default.svc.cluster.local; do echo waiting for mydb; sleep 2; done",29 ]Let’s break down this YAML:
-
Main Container:
- Named
myapp-container - Uses the
busybox:1.28image - Sets an environment variable
FIRSTNAMEwith the value “Piyush” - Runs a simple command to echo a message and sleep
- Named
-
Init Containers:
- Two init containers named
init-myserviceandinit-mydb - Both use the
busybox:1.28image - They check for the existence of specific services before allowing the main container to start
- Two init containers named
Deploying the Pod#
To deploy this pod, save the YAML to a file (e.g., multi-container-pod.yaml) and run:
1kubectl apply -f multi-container-pod.yamlVerifying the Deployment#
Check the status of your pod:
1kubectl get pods2kubectl describe pod myapp-podYou should see the init containers complete their tasks before the main container starts.
Accessing Environment Variables#
To verify that the environment variable is set correctly, you can exec into the running container:
1kubectl exec -it myapp-pod -- /bin/sh2echo $FIRSTNAMEThis should output “Piyush”, confirming that our environment variable is set correctly.
Conclusion#
Multi-container pods and environment variables are powerful features in Kubernetes that enable complex application architectures and flexible configurations. By mastering these concepts, you’ll be well on your way to creating robust, scalable Kubernetes deployments.
Remember, the key to success with Kubernetes is practice. Try modifying this example, add more environment variables, or experiment with different container configurations. Happy Kuberneting!