Saturday, September 10, 2022

Kubernetes - Side Car Container

 Side Car Container (In a multi-container Pod)

It is a helper container for the primary container (the container that runs the application) in a Pod.

It is not a part of the main traffic or API of main functionality.

It is usually running asynchronously.

Common use cases are logging, monitoring, synchronization of files, etc.

Example

Access logs from logfile in main container using sidecar

In this example we will set up the multi-container pod, wherein one of the pods will contain the primary application while the second pod would contain a 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.

Following YAML file will create a multi-container Pod:

[root@controller ~]# cat example-1.yaml

  - 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


0 comments:

Post a Comment