Sunday, August 13, 2023

Kubernetes Sidecar Pattern

A pod is the basic building block of kubernetes application. Kubernetes manages pods instead of containers and pods encapsulate containers. A pod may contain one or more containers, storage, IP addresses, and, options that govern how containers should run inside the pod.


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. There are few patterns for multi-container pods one of them is the sidecar container pattern



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



All the Containers will be executed parallelly and the whole functionality works only if both types of containers are running successfully. Most of the time these sidecar containers are simple and small that consume fewer resources than the main container.


Below is a sample pod yaml file 


apiVersion: v1

kind: Pod

metadata:

  name: sidecar-container-demo

spec:

  containers:

  - image: busybox

    command: ["/bin/sh"]

    args: ["-c", "while true; do echo echo $(date -u) 'Hi I am from 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: {}




// create the pod

kubectl create -f pod.yml

// list the pods

kubectl get po

// exec into pod

kubectl exec -it sidecar-container-demo -c main-container -- /bin/sh

# apt-get update && apt-get install -y curl

# curl localhost




references:

https://medium.com/bb-tutorials-and-thoughts/kubernetes-learn-sidecar-container-pattern-6d8c21f873d

No comments:

Post a Comment