{"id":70415,"date":"2025-02-07T18:34:12","date_gmt":"2025-02-07T15:34:12","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=70415"},"modified":"2025-02-07T18:34:15","modified_gmt":"2025-02-07T15:34:15","slug":"perform-git-clone-in-kubernetes-pod-deployment","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/perform-git-clone-in-kubernetes-pod-deployment\/","title":{"rendered":"How To Perform Git clone in Kubernetes Pod deployment"},"content":{"rendered":"\n<p>In today&#8217;s article we will be looking at how you can clone your application code into the Container running in the <a href=\"https:\/\/computingforgeeks.com\/install-kubernetes-production-cluster-using-rancher-rke\/\">Kubernetes<\/a> 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 <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/init-containers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Init Containers<\/a>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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 <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/pods\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pod<\/a>&nbsp;can have multiple containers running <a href=\"https:\/\/computingforgeeks.com\/install-and-use-winget-windows-package-manager-client\/\">applications<\/a> within it, but it can also have one or more init containers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Our setup requirements<\/h3>\n\n\n\n<p>These are the container images which will be used in this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/hub.docker.com\/r\/alpine\/git\" target=\"_blank\" rel=\"noreferrer noopener\">alpine\/git<\/a> : Run as Init container for git pull operation<\/li>\n\n\n\n<li><a href=\"https:\/\/hub.docker.com\/_\/nginx\" target=\"_blank\" rel=\"noreferrer noopener\">nginx<\/a>: Runs Nginx web server<\/li>\n<\/ul>\n\n\n\n<p>I&#8217;ll create a demo namespace for this test:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">kubectl create ns helloworld<\/mark>\nnamespace\/helloworld created<\/code><\/pre>\n\n\n\n<p>I have a Git repository with Hello World HTML file: <a href=\"https:\/\/github.com\/jmutai\/hello-world-nginx\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/jmutai\/hello-world-nginx<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Kubernetes Pod deployment manifest<\/h3>\n\n\n\n<p>We will generate a template and modify it to add Init container.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl run nginx-helloworld  --image nginx --restart=Never --dry-run=client -o yaml &gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">nginx-helloworld-pod.yml<\/mark><\/code><\/pre>\n\n\n\n<p>If you want to get Kubernetes Deployment YAML file run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl run  nginx-helloworld  --image nginx  --dry-run=client -o yaml &gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">nginx-helloworld-deploy.yml<\/mark><\/code><\/pre>\n\n\n\n<p>Here is the Pod deployment file I got.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> cat nginx-helloworld-pod.yml \n<\/mark>apiVersion: v1\nkind: Pod\nmetadata:\n  creationTimestamp: null\n  labels:\n    run: nginx-helloworld\n  name: nginx-helloworld\nspec:\n  containers:\n  - image: nginx\n    name: nginx-helloworld\n    resources: {}\n  dnsPolicy: ClusterFirst\n  restartPolicy: Never\nstatus: {}\n<\/code><\/pre>\n\n\n\n<p>I&#8217;ll update the manifest file contents to look like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\napiVersion: v1\nkind: Pod\nmetadata:\n  labels:\n    run: nginx-helloworld\n  name: nginx-helloworld\nspec:\n  containers:\n  - image: nginx\n    name: nginx-helloworld\n    ports:\n    - containerPort: 80\n    volumeMounts:\n    - mountPath: \"<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">\/usr\/share\/nginx\/html<\/mark>\"\n      name: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">www-data<\/mark>\n  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">initContainers:<\/mark>\n  - name: git-cloner\n    image: alpine\/git\n    args:\n        - clone\n        - --single-branch\n        - --\n        - <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">https:\/\/github.com\/jmutai\/hello-world-nginx.git<\/mark>\n        - \/data\n    volumeMounts:\n    - mountPath: \/data\n      name: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">www-data<\/mark>\n  volumes:\n  - name: www-data\n    emptyDir: {}<\/code><\/pre>\n\n\n\n<p>Notice we&#8217;re performing the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using init container called <strong>git-cloner<\/strong> to clone git repository to <strong>\/data<\/strong><\/li>\n\n\n\n<li><strong>\/data <\/strong>is a mount of the volume named <strong>www-data<\/strong>. This enables sharing between containers<\/li>\n\n\n\n<li>The <strong>www-data<\/strong> volume is mounted to <strong>\/usr\/share\/nginx\/html <\/strong>in the nginx container. For the web data we cloned to reside in default root directory.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s apply the file to create Kubernetes resources.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>$ <\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">kubectl apply -f nginx-helloworld-pod.yml -n helloworld<\/mark>\npod\/nginx-helloworld created<\/code><\/pre>\n\n\n\n<p>Confirm the pods are created.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">#First run\n<\/mark><\/em>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">kubectl get pods -n helloworld<\/mark>\nNAME               READY   STATUS     RESTARTS   AGE\nnginx-helloworld   0\/1     <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Init:0\/1<\/mark>   0          7s\n\n<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">#Second run<\/mark><\/em>\n$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\"> kubectl get pods -n helloworld<\/mark>\nNAME               READY   STATUS            RESTARTS   AGE\nnginx-helloworld   0\/1     <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">PodInitializing<\/mark>   0          26s\n\n<em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">#Third run<\/mark><\/em>\n$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">kubectl get pods -n helloworld<\/mark>\nNAME               READY   STATUS    RESTARTS   AGE\nnginx-helloworld   1\/1     <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">Running<\/mark>   0          34s<\/code><\/pre>\n\n\n\n<p>Here is a complete screenshot with confirmation that clone happened and data was placed in the path as mounted.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-1024x585.png\" alt=\"\" class=\"wp-image-70479\" title=\"\" srcset=\"https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-1024x585.png 1024w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-300x171.png 300w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-768x439.png 768w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-1536x877.png 1536w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-2048x1170.png 2048w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-696x397.png 696w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-1068x610.png 1068w, https:\/\/computingforgeeks.com\/wp-content\/uploads\/2020\/09\/nginx-kubernetes-git-clone-735x420.png 735w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>When using persistent volume claim, you&#8217;ll update the volumes section to something like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>volumes:\n    - name: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">my-pv-storage<\/mark>\n      persistentVolumeClaim:\n        claimName: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">mypv-claim<\/mark><\/code><\/pre>\n\n\n\n<p>Refer to the <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/configure-pod-container\/configure-persistent-volume-storage\/\" target=\"_blank\" rel=\"noreferrer noopener\">Persistent Volumes task<\/a> page on Kubernetes Documentation to learn more about persistent storage.<\/p>\n\n\n\n<p>Clean up:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete all --all -n helloworld\nkubectl delete ns helloworld<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Git Learning Books<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/computingforgeeks.com\/7-top-books-to-master-git-version-control\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/computingforgeeks.com\/7-top-books-to-master-git-version-control\/\" rel=\"noreferrer noopener\">7 Top Git Books To Master Git Version Control<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;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 &#8230; <a title=\"How To Perform Git clone in Kubernetes Pod deployment\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/perform-git-clone-in-kubernetes-pod-deployment\/\" aria-label=\"Read more about How To Perform Git clone in Kubernetes Pod deployment\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":42052,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[317,316,299,50],"tags":[25867,318],"class_list":["post-70415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","category-containers","category-how-to","category-linux-tutorials","tag-cka","tag-kubernetes"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/70415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=70415"}],"version-history":[{"count":1,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/70415\/revisions"}],"predecessor-version":[{"id":159825,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/70415\/revisions\/159825"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/42052"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=70415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=70415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=70415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}