Kubernetes Multicontainer Pod

1- Init Container Pattern


In a nutshell, the initcontainer must succesfully complete so the rest of containers can start. See example below:


Given the following manifest:

❯ cat initpod.yml
apiVersion: v1
kind: Pod
metadata:
  name: initpod
  labels:
    app: initializer
spec:
  initContainers:     
  - name: init-ctr
    image: busybox
    command: ['sh', '-c', 'until nslookup k8sbook; do echo waiting for k8sbook service; sleep 1; done; echo Service found!']
  containers:
    - name: web-ctr
      image: nigelpoulton/web-app:1.0
      ports:
        - containerPort: 8080

The pod is created but it will not move forward creating the "web-ctr" container defined in the manifest.
❯ kubectl apply -f initpod.yml
pod/initpod created
❯ kubectl get pods
NAME        READY   STATUS     RESTARTS   AGE
hello-pod   1/1     Running    0          50m
initpod     0/1     Init:0/1   0          31s  <<<===== It will NOT move forward until "k8sbook" is available

As soon as the Service appears, the init container completes and the "web-ctr" container starts.
❯ kubectl get pods --watch
NAME        READY   STATUS     RESTARTS   AGE
hello-pod   1/1     Running    0          52m
initpod     0/1     Init:0/1   0          2m37s     
initpod     0/1     PodInitializing   0          7m51s    <<<====================== Only when "k8sbook" service was up and running, the pod progressed and then got a "Running" state.
initpod     1/1     Running           0          8m8s

NOTE: The “k8sbook” service can be found in the following repo from “The Kubernetes Book”: https://github.com/nigelpoulton/TheK8sBook.git




2- Sidecar Container Pattern


There is a main container and a sidecar one. The second performs a secondary task of the first one. In the below example, the “ctr-web” serves a web page while the “ctr-sync” watches the specified github repo and syncs the changes to a shared volume in the Pod. The result, the webpage gets automatically updated when a change is detected .


Given the following manifest:

❯ cat sidecar-cloud.yml
apiVersion: v1
kind: Pod
metadata:
  name: git-sync
  labels:
    app: sidecar
spec:
  containers:
  - name: ctr-web     <<<==== container serving the webpage
    image: nginx
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/
  - name: ctr-sync                   <<<==== container watching the repo and syncing "html" shared volume.
    image: k8s.gcr.io/git-sync:v3.1.6
    volumeMounts:
    - name: html       
      mountPath: /tmp/git
    env:
    - name: GIT_SYNC_REPO
      value: https://github.com/rabocse/ps-sidecar   <<<==== ctr-sync watches the repo and syncronizes changes to "html"
    - name: GIT_SYNC_BRANCH
      value: master
    - name: GIT_SYNC_DEPTH
      value: "1"
    - name: GIT_SYNC_DEST
      value: "html"
  volumes:
  - name: html
    emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: svc-sidecar
spec:
  selector:
    app: sidecar
  ports:
  - port: 80
  type: LoadBalancer  

NOTE: The “Service” with “LoadBalancer” attribute is needed because I am using AWS and I want to publicly access the webpage. Otherwise, use the respective needed “Service”.


The Pod and the Service are created:

❯ kubectl apply -f sidecar-cloud.yml
pod/git-sync created
service/svc-sidecar created
❯ kubectl get svc
NAME          TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
k8sbook       ClusterIP      10.96.4.65     <none>        80/TCP         22h
kubernetes    ClusterIP      10.96.0.1      <none>        443/TCP        28d
svc-sidecar   LoadBalancer   10.96.201.66   <pending>     80:33564/TCP   140m   <<<====== The "EXTERNAL_IP" should be listed.

NOTE: The external IP should be listed if all AWS requirements are completed, in my case, it seems I am missing something. Either way, since this is just a lab, the external IP can be gotten from the AWS EC2 Dashboard.

Once the web server is up and running, proceed to modify the Github repo and the “ctr-sync” container should notice the changes, sync those to the shared volumen and the webpage should be updated automatically.

 Share!

 
comments powered by Disqus