Kubernetes: exploración del patrón Sidecar

Explicando el patrón de Sidecar con un ejemplo

Kubernetes es un motor de orquestación de contenedores de código abierto para implementar, escalar y administrar aplicaciones en contenedores automáticamente. Pod es un concepto básico al diseñar aplicaciones en Kubernetes. Kubernetes opera en pods, no en contenedores, y los pods incluyen contenedores. Un pod puede contener descripciones de uno o más contenedores, particiones montadas, direcciones IP y configuraciones de cómo deberían funcionar los contenedores dentro del 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, .





Prueba de contenedores sidecar
Sidecar Container

Deployment

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





Despliegue
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
      
      



despliegue en acción
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
      
      



El patrón Sidecar en acción
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