Kubernetes sidecar container examples


Kubernetes Tutorial, DevOPs

In this article we will explore Kubernetes sidecar container usage with some examples.

 

Why use Kubernetes sidecar container

  • Typically, there are two different categories of containers: the container that runs the application and another container that provides helper functionality to the primary application.
  • In the Kubernetes space, the container providing helper functionality is called a sidecar container.
  • Among the most commonly used capabilities of a sidecar container are file synchronization, logging, and watcher capabilities.
  • The sidecars are not part of the main traffic or API of the primary application. They usually operate asynchronously and are not involved in the public API.
  • A great example is a central logging agent. Your main container can just log to stdout, but the sidecar container will send all logs to a central logging service where they will be aggregated with the logs from the entire system.

 

Example-1: Access logs from logfile in main container using sidecar

In this example we will setup multi-container pod, wherein one of the pod will contain primary application while the second pod would contain sidecar container. The main application will write the logs into a log file, and the sidecar container will continuously read this log file and send the output to STDOUT. Now you may configure some logging agent to send these logs to a central log server.
Kubernetes sidecar container usage & examples

Following YAML file will create a multi-container Pod:

[root@controller ~]# cat example-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: sidecar-pod-1
spec:
  volumes:
  - name: log
    emptyDir: {}

  containers:
  - image: busybox
    name: application
    args:
     - /bin/sh
     - -c
     - >
      while true; do
        echo "$(date) INFO hello" >> /var/log/myapp.log ;
        sleep 1;
      done
    volumeMounts:
    - name: log
      mountPath: /var/log

  - name: sidecar
    image: busybox
    args:
     - /bin/sh
     - -c
     - tail -fn+1 /var/log/myapp.log
    volumeMounts:
    - name: log
      mountPath: /var/log

Let us create this Pod:

[root@controller ~]# kubectl create -f example-1.yaml
pod/sidecar-pod-1 created

Check if both the containers of our pod have started:

[root@controller ~]# kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
sidecar-pod-1   2/2     Running   0          54s

Now we can verify that the logs written to our primary container i.e. application is being read by our sidecar container:
Kubernetes sidecar container usage & examples

 

Example-2: Access logs from main container using HTTP in sidecar

In this example, our application would writing the logs on STDOUT and we will read the logs and access it via HTTP server on the sidecar container. Following image explains our execution, the file exchange between the main application container and the sidecar container happens through a Volume:

Kubernetes sidecar container usage & examples

 

Following is my YAML file required to create the multi-container pod:

[root@controller ~]# cat example-2.yaml
kind: Pod
apiVersion: v1
metadata:
  name: sidecar-pod-2
spec:
  volumes:
  - name: logs
    emptyDir: {}

  containers:
  - name: app
    image: busybox
    command: ["/bin/sh"]
    args: ["-c", "while true; do date >> /var/log/date.txt; sleep 10; done"]
    volumeMounts:
    - name: logs
      mountPath: /var/log

  - name: sidecar
    image: centos/httpd
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs
      mountPath: /var/www/html

Create this multi-container Pod:

[root@controller ~]# kubectl create -f example-2.yaml
pod/sidecar-pod-2 created

Make sure both the containers of our sidecar-pod-2 have started and are in READY state:

[root@controller ~]# kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
sidecar-pod-1   2/2     Running   0          14m
sidecar-pod-2   2/2     Running   0          27s

Now you can use curl to check the content of date.txt where we were appending the date command output every 10 seconds in a loop:
Kubernetes sidecar container usage & examples

So our sidecar container is successfully reading the logs from the application container.

 

Summary

In this article we learned about Kubernetes sidecar container usage. We created two sidecar containers used to forward the logs from main application container to outside world. A sidecar container lives alongside the main application container within the same Pod and fulfils this exact role. Sidecar containers are best understood by implementing a scenario for one of the established patterns. Based on what you’ve learned, come up with your own applicable use case and create a multi-container Pod to solve it. It’s helpful to be able to identify sidecar patterns and understand why they are important in practice and how to stand them up yourself.

 

Further Readings

Kubernetes Sidecar Pattern
Using sidecar containers to initialize applications

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment