Relevant Documentation:

A Kubernetes Pod can have one or more containers. A Pod with more than one container is a multi-container Pod. In a multi-container Pod, the containers share resources such as network and storage. They can interact with one another, working together to provide functionality.

Cross-Container Interaction

Network: Containers sharing the same Pod can interact with one another using shared resources. They share the same networking namespace and can communicate with one another on any port, even if that port is not exposed to the cluster.

Storage: Containers can use volumes to share data in a Pod.

  1. Create a simple multi-container Pod.
vi multi-container-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: nginx
    image: nginx
  - name: redis
    image: redis
  - name: couchbase
    image: couchbase

Run the following command to create the pod:

kubectl create -f multi-container-pod.yml
  1. View your pod's status.
kubectl get pod multi-container-pod
  1. Create a multi-container Pod that uses shared storage.
vi sidecar-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pod
spec:
  containers:
  - name: busybox1
    image: busybox
    command: ['sh', '-c', 'while true; do echo logs data > /output/output.log; sleep 5; done']
    volumeMounts:
    - name: sharedvol
      mountPath: /output
  - name: sidecar
    image: busybox
    command: ['sh', '-c', 'tail -f /input/output.log']
    volumeMounts:
    - name: sharedvol
      mountPath: /input
  volumes:
  - name: sharedvol
    emptyDir: {}

Run the following command to create the pod:

kubectl create -f sidecar-pod.yml
  1. View the logs for the sidecar container.