How to use Kubernetes Sidecar Container

Reading Time: 3 minutes


Kubernetes Sidecar Containers are those containers that run parallel with the main container in the pod. Also, this sidecar container increases the functionality and provides the dependencies of the container without changing it.

Why use a Kubernetes sidecar container :

Although you have a pod with a single container working properly. You want to attach some functionality to the current container without changing it. But, how can you add the functionality to the current container? the sidecar container has filled in this type of situation.

Sidecar Container YML File:

Create two containers in a pod, one sidecar container and the main container. Therefore, here is my YML file to create a pod-

apiVersion: v1
kind: pod
  name: sidecar
  - name: log
    emptyDir: {}

  - image: busybox
    name: main-container
     - /bin/sh
     - -c
     - >
      while true: do
        echo "$(date) INFO hello from main-container" >> /var/log/myapp.log ;
        sleep 1;
    - name: log
      mountPath: /var/log

  - name: sidecar-container
    image: busybox
     - /bin/sh
     - -c
     - tail -fn+1 /var/log/myapp.log
    - name: log
      mountPath: /var/log
Now Understand this YML file :
  • For easily understanding I have denoted my containers as main-container & sidecar-container.
  • main container in which our application (Nginx) is presented.
  • the shared path is mounted in both containers through the volume mounts.
Now create this pod:

Create the pod for the sidecar container.

kubectl apply -f sidecar.yml
Now check the status of the pod:

checks the pod status and shows that there are two containers and both are in a running state.

kubectl get pods
List the containers from the pod:

This command shows pod contain that have containers in it. Consequently, Now we have two containers in this sidecar pod – the first is the main container and the second is the sidecar container.

kubectl get pods sidecar -o jsonpath=' {.spec.containers[*].name}'
Check the sidecar container logs:

This command checks the communication between the main and sidecar container. Thus, the output of the sidecar container should show the logs coming from the main container.

kubectl logs -f sidecar -c sidecar-container
Check the IPs of the sidecar pod:

All the containers inside the pod share the same network namespace. Hence, this command checks the IP of the sidecar pod.

kubectl get pod sidecar -o wide
Check the IP for both containers:

our sidecar pod IP is let’s check for both containers have the same IP or not.

kubectl exec -it sidecar -c sidecar-container --ifconfig eth0