Docker & Kubernetes
Multi-container Pod
Learn to run multiple containers in a Pod.
Multi-container pods
Several containers are put in one pod only if they are tightly coupled. The most common pattern is the sidecar: a container helping the main application (log collector, proxy, syncer). They share files through a common emptyDir volume:
spec:
volumes:
- name: loglar
emptyDir: {}
containers:
- name: uygulama
image: api:1.0
volumeMounts:
- name: loglar
mountPath: /var/log/app
- name: log-gonderici
image: busybox:1.36
command: ["sh", "-c", "tail -F /loglar/app.log"]
volumeMounts:
- name: loglar
mountPath: /loglar
Init containers
initContainers are containers that run in order and finish before the main containers. They are used to wait for a dependency to be ready or to prepare configuration files; the main containers don't start until all of them finish successfully:
spec:
initContainers:
- name: db-bekle
image: busybox:1.36
command: ["sh", "-c", "until nc -z db 5432; do sleep 2; done"]
containers:
- name: api
image: api:1.0
For a specific container's log, kubectl logs api -c db-bekle is used.
Exercise A pod with a sidecar
Write a pod YAML: name api; a loglar volume with emptyDir: {} under volumes; two containers: uygulama (api:1.0, mounts loglar at /var/log/app) and log-gonderici (busybox:1.36, mounts loglar at /loglar).
Show example solution
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
volumes:
- name: loglar
emptyDir: {}
containers:
- name: uygulama
image: api:1.0
volumeMounts:
- name: loglar
mountPath: /var/log/app
- name: log-gonderici
image: busybox:1.36
volumeMounts:
- name: loglar
mountPath: /loglar
Test yourself
-
Should a web application and a database be put in the same pod?
- Only in production
- Only in Minikube
- No; they scale separately and have separate life cycles
- Yes, always
Show answer
No; they scale separately and have separate life cycles — When a pod scales, all containers in it are copied together.
-
Show the log of a specific container in the pod.
kubectl logs api ____ db-bekle
Show answer
kubectl logs api -c db-bekle
-
Match.
- sidecar
- Helper container
- emptyDir
- Pod-lifetime shared directory
- initContainers
- Runs first and finishes
- -c
- Container selection
Next lesson: Summary - Pod