What are Sidecar Containers

Sidecar containers are the containers that should run along with the main container in the pod. This sidecar pattern extends and enhances the functionality of current containers without changing it. Nowadays, We know that we use container technology to wrap all the dependencies for the application to run anywhere. A container does only one thing and does that thing very well.

Imagine that you have the pod with a single container working very well and you want to add some functionality to the current container without touching or changing, how can you add the additional functionality or extending the current functionality? This sidecar container pattern really helps exactly in that situation.

Let's implement a simple project to understand this pattern. Here is a simple pod that has main and sidecar containers. The main container is nginx serving on the port 80 that takes the index.html from the volume mount workdir location. The Sidecar container with the image busybox creates logs in the same location with a timestamp. Since the Sidecar container and main container runs parallel Nginx will display the new log information every time you hit in the browser.

apiVersion: v1
kind: Pod
metadata:
  name: sidecar-container-test
spec:
  containers:
    - image: busybox
      command: ["/bin/sh"]
      args:
        [
          "-c",
          "while true; do echo echo $(date -u) 'Sidecar container' >> /var/log/index.html; sleep 5;done",
        ]
      name: sidecar-container
      resources: {}
      volumeMounts:
        - name: var-logs
          mountPath: /var/log
    - image: nginx
      name: main-container
      resources: {}
      ports:
        - containerPort: 80
      volumeMounts:
        - name: var-logs
          mountPath: /usr/share/nginx/html
  dnsPolicy: Default
  volumes:
    - name: var-logs
      emptyDir: {}

When should we use this pattern

These are some of the scenarios where you can use this pattern

  • Whenever you want to extend the functionality of the existing single container pod without touching the existing one.
  • Whenever you want to enhance the functionality of the existing single container pod without touching the existing one.
  • You can use this pattern to synchronize the main container code with the git server pull.
  • You can use this pattern for sending log events to the external server.
  • You can use this pattern for network-related tasks.

Summary

  • A pod that contains one container refers to a single container pod and it is the most common kubernetes use case.
  • A pod that contains Multiple co-related containers refers to a multi-container pod.
  • The Sidecar container pattern is one of the patterns that we use regularly for extending or enhancing pre-existing containers.
  • Sidecar containers run in parallel with the main container. So that you need to consider resource limits of sidecar containers while defining request/resource limits for the pod.
  • The application containers and Sidecar containers run-in parallel which means all the containers run at the same time. So that you need to sum up all the request/resource limits of the containers while defining request/resource limits for the pod.
  • You should configure health checks for sidecar containers as main containers to make sure they are healthy.
  • All the pods in the deployment object don't have static IP addresses so that you need a service object to expose to the outside world.
  • The service object internally maps to the port container port based on the selectors.
  • You can use this pattern where your application or main containers need extending or enhancing the current functionality.

Conclusion

It's always to good to know already proven kubernetes patterns. Make sure all your sidecar containers are simple and small enough because you have to sum up all the resources/request limits while defining resource limits for the pod. You need to make sure Sidecar containers are also healthy by configuring health checks. So it's important to know when to combine your functionality into the main container or have a separate container.