Multi Container Pod

(this post is part of the material I cover in my devops course)

multi container pod

A pod is a group (sometimes of whales) of containers.
Does it mean that kubernetes is asking us to put multiple containers in a pod?

pod of whales

What Multiple Containers

  • The k8s Pods guide tells us that:
    "A Pod models an application-specific "logical host": it contains one or more application containers which are relatively tightly coupled."
  • Somehow, the example of a web-server and a database server, that together form a web application (like wordpress site) is one of the first examples that come to mind.
    BUT THIS IS AN ANTIPATTERS, THESE CONTAINERS SHOULD NOT RUN INSIDE A SINGLE POD.
  • The big thing, then, is understanding what coupled means. So ask yourself these questions:
    • Is it true that those pods shouls ALWAYS run together?
    • Is it true that these containers should run IN THE SAME NODE?
    • Is there a one-to-one ratio of the numbers of these pods? We nay want to run several web servers, and use a load balancer to increase performance and taulerance for our web application. In the same time, we may choose running just a single DB server (or maybe 2 instances that backup each other).
  • So what could be a good example of a multi-container-pod?

Sidecar Containers

sidecar containers

  • A sidecar container help the main container do its job (it does not do a completelly different job).
  • Here's an example:
    • Kubernetes collects logs of all pods.
    • Logs are text that is written to the standard output in containers
    • It turn out that the best practice would be to write log messages to standard output,
      BUT NOT TO A LOCAL FILE!!
    • You can view them by using the kubectl logs command.
  • Let's say you got an server code that writes everything to a local file, and you want to run it as a container in kubernetes.
    You could run another container that will monitor the local file, and write everything to the standard output.
    THIS IS A GOOD EXAMPLE FOR ANOTHER CONTAINER IN THE SAME POD!

Sidecar Container - Example

  • Here's out pod configuration, with both containers:
 1$> cat multi-container.yaml 
 2apiVersion: v1
 3kind: Pod
 4metadata:
 5  name: multi
 6spec:
 7  containers:
 8  - name: busybox1
 9    image: busybox
10    command: ['sh', '-c', 'COUNTER=0; while true; do printf "line: $COUNTER\n" >> /output/output.log;sleep 1;$(()); COUNTER=$((COUNTER + 1));done']
11    volumeMounts:
12    - name: sharedvol
13      mountPath: /output
14  - name: busybox2
15    image: busybox
16    args: ['sh', '-c', 'tail -f /output/output.log']
17    volumeMounts:
18    - name: sharedvol
19      mountPath: /output
20  volumes:
21  - name: sharedvol
22    emptyDir: {}
  • Note that both container mount a shared volume (called sharedvol)
  • One container write multiple lines into a file called /output/output.log
  • The other container writes each line of this file to the standard output using the tail -f linux command.
  • Try this, and use kubectl logs multi -c busybox2 to see those logs.