Kubernetes - exploring the Sidecar pattern

Explanation of the Sidecar pattern with an example

Kubernetes is an open source container orchestration engine for automatically deploying, scaling, and managing containerized applications. Pod is a basic concept when designing applications in Kubernetes. Kubernetes operates on pods, not containers, and pods include containers. A pod can contain descriptions of one or more containers, mounted partitions, IP addresses, and settings for how containers should work inside the pod.





, , - Kubernetes. , , - . - โ€” sidecar. .





  • Sidecar













  • Deployment





  • Resource Limits

















Sidecar-

Sidecar- โ€” , . . , , . .





, , , - , . - ? Sidecar .





, Sidecar . . sidecar- .






, Kubernetes:





  • Init Container Pattern





  • Adapter Container Pattern





  • Ambassador Container Pattern






, . Minikube.





https://github.com/bbachi/k8s-sidecar-container-pattern.git







, . , sidecar . Nginx, 80, index.html volume, location workdir. sidecar- busybox, timestamp . sidecar- , Nginx , .





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
      
      



curl localhost, .





Sidecar Container Testing
Sidecar Container

Deployment

deployment 5 . service NodePort, deployment . deployment controller, IP , service, . service 80 ( ).  .





Deployment
Deployment

deployment, sidecar-. . sidecar- /var/log. Nginx , 80. .





apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx-webapp
  name: nginx-webapp
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nginx-webapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-webapp
    spec:
      containers:
      - image: busybox
        command: ["/bin/sh"]
        args: ["-c", "while true; do echo echo $(date -u) 'Hi I am from Sidecar container 1' >> /var/log/index.html; sleep 5;done"]
        name: sidecar-container1
        resources: {}
        volumeMounts:
          - name: var-logs
            mountPath: /var/log
      - image: busybox
        command: ["/bin/sh"]
        args: ["-c", "while true; do echo echo $(date -u) 'Hi I am from Sidecar container 2' >> /var/log/index.html; sleep 5;done"]
        name: sidecar-container2
        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: {}
status: {}

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-webapp
  labels:
    run: nginx-webapp
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: nginx-webapp
  type: NodePort
      
      



, deployment.





// create a deployment
kubectl create -f manifest.yml
// list the deployment, pods, and service
kubectl get deploy -o wide
kubectl get po -o wide
kubectl get svc -o wide
      
      



deployment in action
deployment

5 , IP- service, 32123 80. deployment IP - Kubernetes 192.168.64.2 32123:





http://192.168.64.2:32123







:





// exec into main container of the pod
kubectl exec -it nginx-webapp-7c8b4d4f8d-9qmdm -c main-container -- /bin/sh
// install curl
# apt-get update && apt-get install -y curl
# curl localhost
      
      



The Sidecar pattern in action
Sidecar

, sidecar-. , , , , .





  • , ( ).






, :





  • ,





  • git- pull.(You can use this pattern to synchronize the main container code with the git server pull.)





  • , .





  • , (network-related tasks.).






  • , , - .





  • , , - .





  • Sidecar - , .





  • Sidecar- . sidecar- , / .





  •  Sidecar- , , . / , / .





  • health checks sidecar- , , , .





  • , deployment IP-, service, .





  • service .






kubernetes . , sidecar- , . , Sidecar- โ€œโ€, health checks. , .








All Articles