How to perform Git clone in Kubernetes Pod deployment

To
You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off
To

To
To

In today’s article, we will study how to clone application code into a Container running on the Kubernetes Container platform. This is the ideal solution if you store your application code in Git version control and want to extract the latest code during deployment without rebuilding the container image. The kubernetes feature that allows us to do this is Initialize the container.

The initialization container is a dedicated type container that runs before the application container in the Pod. These containers can contain utilities or setup scripts that are not present in the application image. Init containers are nothing special because they can be specified in the Pod specification along with the container array.

In our example, we will deploy a sample nginx container whose web application data is extracted from the Git repository using the Init container. caution pod There can be multiple containers running the application, but there can also be one or more init containers.

My setup requirements

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

  • Alpine/git : Run as an Init container for git pull operations
  • Nginx: Run Nginx web server

I will create a demo namespace for this test:

$ kubectl create ns helloworld
namespace/helloworld created

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

Create a Kubernetes Pod deployment list

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

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

If you want to get the YAML file for Kubernetes deployment, run:

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

This 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 updated the contents of the manifest file as shown 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: {}

Please note that we are doing the following:

  • Use init container call git clone Clone the git repository to /data
  • /Data Is the mount of the volume named www data. This can be shared between containers
  • of www data Volume is mounted to /usr/share/nginx/html In the Nginx container. For the web data we cloned, they reside in the default root directory.

Let’s apply this file to create Kubernetes resources.

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

The confirmation pane has been created.

$ kubectl get pods -n helloworld

This is a complete screenshot where it is confirmed that the cloning has occurred and that the data has been placed in the path when it is loaded.

When using permanent bulk declarations, you update the “bulk” section to the following.

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

See Persistent Volume Task Page 2 on the Kubernetes documentation to learn more about persistent storage.

Clean up:

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

Kubernetes learning video:


Docker and Kubernetes: The complete guide

Docker and Kubernetes: The complete guide

★★★★★
(31360)

$ 15.38

$ 118.32

In stock

Buy now

How to perform Git clone in Kubernetes Pod deploymentUdemy.com


Kubernetes for absolute beginners-hands-on

Kubernetes for absolute beginners-hands-on

★★★★★
(17464)

$ 15.38

$ 153.82

In stock

Buy now

How to perform Git clone in Kubernetes Pod deploymentUdemy.com


Certified Kubernetes Administrator (CKA) and practice test

Certified Kubernetes Administrator (CKA) and practice test

★★★★★
(15193)

$ 15.38

$ 153.82

In stock

Buy now

How to perform Git clone in Kubernetes Pod deploymentUdemy.com


Kubernetes Certified Application Developer (CKAD) and testing

Kubernetes Certified Application Developer (CKAD) with testing capabilities

★★★★★
(7141)

$ 15.38

$ 153.82

In stock

Buy now

How to perform Git clone in Kubernetes Pod deploymentUdemy.com


Learn DevOps: The complete Kubernetes course

Learn DevOps: The complete Kubernetes course

★★★★☆
(11317)

$ 17.75

$47.32

In stock

Buy now

How to perform Git clone in Kubernetes Pod deploymentUdemy.com

You can download this article in PDF format via the link below to support us.

Download the guide in PDF format

turn off
To

To
To

Sidebar