Multi-Container help on creation

I’m currently exploring various deployment scenarios for multi-container pods in Kubernetes and would appreciate some guidance on optimizing the manifest creation process. Below, I’ve outlined several use cases along with corresponding YAML manifests:

Multi-container pod with messages stored in files:
This scenario involves deploying a pod with two containers: one running Nginx and the other running Busybox. The Busybox container stores a message (“Message 1”) in a file named message.txt within a shared volume. Here’s the YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod-messages
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: message-container
    image: busybox
    command: ["/bin/sh", "-c", "echo 'Message 1' > /shared-data/message.txt && sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data

Multi-container pod with sleep and calculation tasks:
In this case, the pod consists of an Nginx container and a Busybox container. The Busybox container executes a sleep command for 1 hour and calculates the result of the expression “5 + 5”, storing it in a file named result.txt within a shared volume. Here’s the YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod-sleep-calc
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: sleep-calc-container
    image: busybox
    command: ["/bin/sh", "-c", "sleep 3600 && echo 'Result: ' $((5 + 5)) > /shared-data/result.txt"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data

Multi-container pod with multiple messages stored in files:
This scenario extends the first use case by storing multiple messages (“Message 1” and “Message 2”) in separate files within the shared volume. The Busybox container handles the creation of both files. Here’s the YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod-sleep-calc
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: sleep-calc-container
    image: busybox
    command: ["/bin/sh", "-c", "sleep 3600 && echo 'Result: ' $((5 + 5)) > /shared-data/result.txt"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data

Multi-container pod with a specific order of containers:
Here, we deploy a pod with two containers: one running Busybox and the other running Nginx. The Busybox container stores a message (“Message 1”) in a file within a shared volume before the Nginx container starts. Here’s the YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod-sleep-calc
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: sleep-calc-container
    image: busybox
    command: ["/bin/sh", "-c", "sleep 3600 && echo 'Result: ' $((5 + 5)) > /shared-data/result.txt"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data

Multi-container pod with a combination of tasks and order:
This comprehensive scenario involves a pod with three containers: Busybox, Nginx, and another Busybox container. The first Busybox container stores a message (“Message 1”) in a file within a shared volume. Next, the Nginx container starts. Meanwhile, the second Busybox container executes a sleep command for 1 hour and calculates the result of “5 + 5”, storing it in a file within the shared volume. Here’s the YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod-combination
spec:
  volumes:
  - name: shared-data
    emptyDir: {}
  containers:
  - name: message-container
    image: busybox
    command: ["/bin/sh", "-c", "echo 'Message 1' > /shared-data/message.txt && sleep 3600"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data
  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html
  - name: sleep-calc-container
    image: busybox
    command: ["/bin/sh", "-c", "sleep 3600 && echo 'Result: ' $((5 + 5)) > /shared-data/result.txt"]
    volumeMounts:
    - name: shared-data
      mountPath: /shared-data
apiVersion: v1
kind: Pod
metadata:
  name: tres-containers-pod
  namespace: multi-containers
spec:
  containers:
  - name: primero
    image: busybox:1.28
    command: ["sleep", "3600"]
    env:
    - name: ORDER
      value: FIRST
  - name: segundo
    image: nginx:1.17
    ports:
    - containerPort: 8080
  - name: tercero
    image: busybox:1.31.1
    command: ["sleep", "3600"]
    env:
    - name: ORDER
      value: THIRD

To test multi pods you can use:

$ kubectl run --rm --restart=Never --image=alpine -i -t --labels=“app=foo” test – ash