We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

27
1.459.919,70

Cart

Learn how to – How To Perform Git clone in Kubernetes Pod deployment

Learn how to – How To Perform Git clone in Kubernetes Pod deployment.

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=client -o yaml >nginx-helloworld-pod.yml

If you want to get Kubernetes Deployment YAML file run:

kubectl run  nginx-helloworld  --image nginx  --dry-run=client -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: "/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.

#First run
$ kubectl get pods -n helloworld
NAME               READY   STATUS     RESTARTS   AGE
nginx-helloworld   0/1     Init:0/1   0          7s

#Second run
$ kubectl get pods -n helloworld
NAME               READY   STATUS            RESTARTS   AGE
nginx-helloworld   0/1     PodInitializing   0          26s

#Third run
$ kubectl get pods -n helloworld
NAME               READY   STATUS    RESTARTS   AGE
nginx-helloworld   1/1     Running   0          34s

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

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

Git Learning Video Courses

27
1.459.919,70

Cart