In today’s article we will be looking at how you can clone your application code into the Container running in the Kubernetes Container platform. This serves as an ideal solution if you store application code in Git version control and would like to pull the latest code during deployment without rebuilding container image. A kubernetes feature which allows us to perform this operation is Init Containers.

Init Containers are specialized type of containers that run before application containers in a Pod. These containers can contain utilities or setup scripts not present in an application image. There is nothing so unique about Init containers as they can be specified in the Pod specification alongside the containers array.

In our example we will deploy an example nginx container whose web application data is pulled from a Git repository using Init container. Note that a Pod can have multiple containers running applications within it, but it can also have one or more init containers.

My Setup requirements

These are the container images which will be used in this example:

  • alpine/git : Run as Init container for git pull operation
  • nginx: Runs Nginx web server

I’ll create a demo namespace for this test:

$ kubectl create ns helloworld
namespace/helloworld created

I have a Git repository with Hello World HTML file: https://github.com/jmutai/hello-world-nginx

Create Kubernetes Pod deployment manifest

We will generate a template and modify it to add Init container.

kubectl run nginx-helloworld  --image nginx --restart=Never --dry-run -o yaml >nginx-helloworld-pod.yml

If you want to get Kubernetes Deployment YAML file run:

kubectl run  nginx-helloworld  --image nginx  --dry-run -o yaml >nginx-helloworld-deploy.yml

Here is the Pod deployment file I got.

$ cat nginx-helloworld-pod.yml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx-helloworld
  name: nginx-helloworld
spec:
  containers:
  - image: nginx
    name: nginx-helloworld
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

I’ll update the manifest file contents to look like below.

---
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx-helloworld
  name: nginx-helloworld
spec:
  containers:
  - image: nginx
    name: nginx-helloworld
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: "https://computingforgeeks.com/usr/share/nginx/html"
      name: www-data
  initContainers:
  - name: git-cloner
    image: alpine/git
    args:
        - clone
        - --single-branch
        - --
        - https://github.com/jmutai/hello-world-nginx.git
        - /data
    volumeMounts:
    - mountPath: /data
      name: www-data
  volumes:
  - name: www-data
    emptyDir: {}

Notice we’re performing the following:

  • Using init container called git-cloner to clone git repository to /data
  • /data is a mount of the volume named www-data. This enables sharing between containers
  • The www-data volume is mounted to /usr/share/nginx/html in the nginx container. For the web data we cloned to reside in default root directory.

Let’s apply the file to create Kubernetes resources.

$ kubectl apply -f nginx-helloworld-pod.yml -n helloworld
pod/nginx-helloworld created

Confirm the pods are created.

$ kubectl get pods -n helloworld

Here is a complete screenshot with confirmation that clone happened and data was placed in the path as mounted.

<img alt="" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/nginx-kubernetes-git-clone-1024×585.png" data-ez ezimgfmt="rs rscb8 src ng ngcb8 srcset" height="585" loading="lazy" src="data:image/svg xml,” width=”1024″>

When using persistent volume claim, you’ll update the volumes section to something like below.

volumes:
    - name: my-pv-storage
      persistentVolumeClaim:
        claimName: mypv-claim

Refer to the Persistent Volumes task page on Kubernetes Documentation to learn more about persistent storage.

Clean up:

kubectl delete all --all -n helloworld
kubectl delete ns helloworld

Kubernetes learning videos:


<img alt="Docker and Kubernetes: The Complete Guide" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/1793828_7999.jpg5f634deb4275d.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/icon_udemy-com.png5f634deb4c7f3.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>Udemy.com


<img alt="Kubernetes for the Absolute Beginners – Hands-on" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/1602900_f550_8.jpg5f634debc2b5b.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/icon_udemy-com.png5f634deb4c7f3.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>Udemy.com


<img alt="Certified Kubernetes Administrator (CKA) with Practice Tests" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/2301254_26c8_2.jpg5f634dec4b414.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/icon_udemy-com.png5f634deb4c7f3.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>Udemy.com


<img alt="Kubernetes Certified Application Developer (CKAD) with Tests" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/1988542_3bcb_11.jpg5f634dec8678e.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/icon_udemy-com.png5f634deb4c7f3.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>Udemy.com


<img alt="Learn DevOps: The Complete Kubernetes Course" data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/1007250_9e3c_5.jpg5f634dece99d8.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2020/09/echo/icon_udemy-com.png5f634deb4c7f3.jpg" ezimgfmt="rs rscb8 src ng ngcb8" src="data:image/svg xml,”>Udemy.com