Kubernetes - Erkundung des Beiwagenmusters

Erklären Sie das Sidecar-Muster anhand eines Beispiels

Kubernetes ist eine Open-Source-Container-Orchestrierungs-Engine zum automatischen Bereitstellen, Skalieren und Verwalten von Containeranwendungen. Pod ist ein Grundkonzept beim Entwerfen von Anwendungen in Kubernetes. Kubernetes arbeitet mit Pods, nicht mit Containern, und Pods enthalten Container. Ein Pod kann Beschreibungen eines oder mehrerer Container, gemountete Partitionen, IP-Adressen und Einstellungen für die Funktionsweise von Containern im Pod enthalten.





, , - 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, .





Seitenwagencontainertest
Sidecar Container

Deployment

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





Einsatz
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
      
      



Bereitstellung in Aktion
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
      
      



Das Sidecar-Muster in Aktion
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