You can mount a Git Repo as a Kubernetes Volume in your pod.

But why do you need it?

Let’s say you want to host a few static web pages using Nginx running on a pod. You can commit the static web pages (the HTML files) on GitHub and mount them to the Nginx container using Kubernetes volume.

In an earlier post, I explained how you can create an emptyDir Kubernetes Volume.

A volume using Git Repo is basically an emptyDir volume that’s populated by cloning a Git repository. You can even check out a specific revision from the Git repository.

In this post, I’m going to show you how you can use the setup a Git Repo as Kubernetes Volume in a step-by-step manner.

1 – Process to setup a Git Repo as Kubernetes Volume?

In earlier versions of Kubernetes, there was a specific volume type known as gitRepo. You could directly specify the Git repository as part of the volume specification.

This is how the specifications looks like (as per the official documentation)

  - name: git-volume
      repository: "git@somewhere:me/my-git-repository.git"
      revision: "22f1d8406d464b0c0874075539c1f2e96c253775"

However, the Kubernetes volume type gitRepo has been deprecated due to security concerns.

The process now is as follows:

  • Setup a Kubernetes pod with initContainers and the actual container that uses the volume
  • The initContainer will perform the task of cloning the GitHub repository to a normal emptyDir volume.
  • The main container will access the cloned files from the emptyDir volume.

Here’s what it looks like in action:

git repo kubernetes volume

Note that in this process, the initContainer will run at the start of a pod. So, when you push changes to the GitHub repository, they won’t be automatically available. You’ve to restart the pod for the changes to reflect.

By the way, if you are interested in learning about Kubernetes and other Cloud Native tools and technologies, you’d love the Progressive Coder newsletter.

Subscribe now and join along.

Let us now see an example in action.

2 – Setting up a Git Repo as Kubernetes Volume

Here’s the example of a Pod YAML that mounts a Git repository as Kubernetes Volume:

apiVersion: v1
kind: Pod
  name: git-repo-demo
  - name: git-clone
    image: alpine/git
      - clone
      - --single-branch
      - --
      - /repo
      runAsUser: 1
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
    - name: html
      mountPath: /repo
  - name: web-server
    image: nginx:alpine
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    - containerPort: 80
      protocol: TCP
    - name: html
      emptyDir: {}

Let’s walk through it:

  • In the specification section, we first have the initContainers section. Here, we define a container with name git-clone. It’s job is to simply clone the Github repository mentioned in the args section.
  • If you notice, it will clone everything into the /repo folder. In the volumeMounts section, we specify the volume name as html and mountPath as /repo.
  • Now, within the containers section, we basically mount the html volume to the mountPath value /usr/share/nginx/html.
  • This is because Nginx serves the files present in the directory /usr/share/nginx/html.

You can create the pod using the below command:

$ kubectl apply -f gitrepo-pod.yaml

When the pod is created, Kubernetes first runs the initContainer. This container clones the contents of the GitHub repository to the volume.

Once that’s done, the web server container is started and it starts serving the files present in the Kubernetes volume.

You can test whether everything works fine by using port forwarding as below:

$ kubectl port-forward git-repo-demo 8080:80
Forwarding from -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
Handling connection for 8080

You can then visit http://localhost:8080 to see the contents of the HTML in the browser.


That’s all for now.

In this post, we learned how to clone a Git repository and mount its contents as Kubernetes Volume.

For testing purposes, you can also use my GitHub repository which contains a very simple HTML file.

If you have any queries or comments, do write them in the comments section below.

In case you are interested in checking about persistent storage in Kubernetes, the below posts might help you:

Anyways, before we end this post, a quick reminder about the Progressive Code Newsletter where I explain software concepts using a fun approach so that you never forget what you’ve learned. I’m 100% sure you’d love it.

Subscribe now and see you over there.

Categories: BlogKubernetes

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.


Leave a Reply

Your email address will not be published. Required fields are marked *