Cloning git repos using Kubernetes initContainers and Secrets

Stefvnf
3 min readApr 9, 2021

Started as a beginner as everybody else, I remember that one time when i had to clone a private repo and i got my head around it for quite some time. It’s been something that had me for long, and i was thinking that this article might come useful to other people who are just getting started with Kubernetes and containers, and I’m going to show you first how to do a simple private repo clone, using Kubernetes and initContainers.

Also, as a side-note, in this tutorial we are going to clone our git using https, not ssh.

Later, we are also going to mount this private git repo inside a container using PersistentVolumeClaims (PVC).

So, let’s begin!

First of all, i am going to start by creating my Kubernetes Secret:

kubectl create secret generic test-secret --from-literal=username='foo' --from-literal=password='bar'

Basically, here we are creating our Kubernetes Secret where we are going to store our git username and password. For private repos, we will use our private access token from git as password.

We should have something like this:

kubectl create secret generic test-secret --from-literal=username='foo' --from-literal=password='asdas231242PXXLMNpx'

Now, it’s time to create our persistent volume claim and deployment and mount inside it our wanted repo.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pvc
spec:
accessModes:
— ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: git-cloner
labels:
app: git-cloner
spec:
replicas: 1
selector:
matchLabels:
app: git-cloner
template:
metadata:
labels:
app: git-cloner
spec:
containers:
- name: livegreen-backend
image: someimage/latest
imagePullPolicy: Always
volumeMounts:
- name: test-volume
mountPath: /testing
initContainers:
- args:
- clone
- '--single-branch'
- '--'
- 'https://$(GIT_USERNAME):$(GIT_PASSWORD)@gitlab.company.com>/path/to/repo.git'"
- '/testing/'
image: alpine/git
imagePullPolicy: Always
name: init-clone-repo
env:
- name: GIT_USERNAME
valueFrom:
secretKeyRef:
key: username
name: test-secret
- name: GIT_PASSWORD
valueFrom:
secretKeyRef:
key: password
name: test-secret
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /testing
name: test-volume
volumes:
- name: test-volume
persistentVolumeClaim:
claimName: test-pvc

Now, let’s create our deployment with persistent volume init.

kubectl apply -f git-cloner.yaml

persistentvolumeclaim/test-pvc created
deployment.apps/git-cloner created

That’s it! Our pod should be now up and running with init container!

$ kubectl get po --watch

NAME                          READY   STATUS     RESTARTS   AGE
git-cloner-7f99df6455-9hggg 0/1 Init:0/1 0 4s
git-cloner-7f99df6455-9hggg 0/1 Init:0/1 0 5s
git-cloner-7f99df6455-9hggg 0/1 PodInitializing 0 27s
git-cloner-7f99df6455-9hggg 1/1 Running 0 31s

We can now check inside our pod if ‘/testing’ directory has been made, and inside it we have our cloned git repo.

kubectl exec -it git-cloner-7f99df6455-9hggg -- /bin/bash
root@git-cloner-7f99df6455-9hggg:/usr/src/backend# cd
root@git-cloner-7f99df6455-9hggg:~# cd ..
root@git-cloner-7f99df6455-9hggg:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys testing tmp usr var

There you go! Our testing directory has been created and you can check inside it if our repo is there!

Thank you!

Also, i’d like to wish you Siqsess!

--

--