Sidecar (how containers in a pod communicate)
1. Introduction
Sidecar containers extend and enhance the “main” container
They take existing containers and make them better
2. Creating 2 containers in a pod
a debian container
a nginx web-server container
3. twocontainers.yaml
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: nginx-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: debian-container
image: debian
volumeMounts:
- name: shared-data
mountPath: /pod-data
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
- these 2 containers have a shared volume called shared-data
4. Creating the pod with 2 containers
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/two-container-pod.yaml
hadoop@k8s-00:~$ kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/two-container-pod.yaml
pod "two-containers" created
- view information about the pod and the exisiting 2 containers
image: debian:latest
imageID: docker-pullable://debian@sha256:de3eac83cd481c04c5d6c7344cd7327625a1d8b2540e82a8231b5675cef0ae5f
lastState: {}
name: debian-container
ready: false
restartCount: 0
state:
terminated:
containerID: docker://117897e0812ce815efce5530da34c65b0db3ebe1bc14c452c81f4f4a26d4b422
exitCode: 0
finishedAt: 2018-06-18T06:15:54Z
reason: Completed
startedAt: 2018-06-18T06:15:54Z
- containerID: docker://249f487e6f6ed95a8e411ef890c89e10bd1cb8b40abec40ef7225dd9d7c9e56d
image: nginx:latest
imageID: docker-pullable://nginx@sha256:3e2ffcf0edca2a4e9b24ca442d227baea7b7f0e33ad654ef1eb806fbd9bedcf0
lastState: {}
name: nginx-container
ready: true
restartCount: 0
state:
running:
startedAt: 2018-06-18T06:15:42Z
- the above log tells us that debian container has stopped
name: debian-container
ready: false
restartCount: 0
state:
terminated:
- while the nginx container is still running
image: nginx:latest
imageID: docker-pullable://nginx@sha256:3e2ffcf0edca2a4e9b24ca442d227baea7b7f0e33ad654ef1eb806fbd9bedcf0
lastState: {}
name: nginx-container
ready: true
- let's get to the shell of the nginx container
kubectl exec -it two-containers -c nginx-container -- /bin/bash
- install curl and ps inside the nginx container
apt-get update
apt-get install curl procsps
- let's check if the nginx is running (verification)
root@two-containers:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 32616 5256 ? Ss 06:15 0:00 nginx: master process nginx -g daemon off;
nginx 6 0.0 0.0 33068 2356 ? S 06:15 0:00 nginx: worker process
we can see that nginx is running
- the debian container is responsible for creating index.html and placing it in the root directory of nginx container
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
- let's verify the above index.html by curl command
root@two-containers:/# curl localhost
Hello from the debian container
- therefore, even if the debian container was killed, it was able to create a index.html file and place it in the root directory of nginx container for it to access it.