Access NFS shares inside your GitlabCI-Jobs with the kubernetes executor

A not-properly documented (not so) common use-case

Introduction

We have a central Gitlab instance hosted by an other department. Additionally I use a gitlab runner (kubernetes executor) in my internal kubernetes cluster to run my CI-Jobs. One of my co-workers asked me if it is possible to mount persistent storage like a nfs-volume into the GitlabCI pipeline to access a large set of sample files for testing purposes.

My first thought was that this kind of setup has to be well-supported by Gitlab, but damn was I wrong. I nearly wasted 2 hours of trial-and-error to get this working setup:

How to add NFS Storage to your GitlabCI Stages

Adding the NFS-volume (and volumeMount) to the values.yaml of your gitlab-runner deployment does not work, because a mount into the gitlab-runner pod does not propagate this volume to the different CI stages. Instead, you have to create a PV an PVC to the desired NFS path and link it to the runner as described in the following section:

Setup your Runner Helmchart

The NFS-share is accessible via 10.10.10.10:/volume

I use kustomize to deploy Helmcharts, because this is how we roll in ArgoCD:

kustomization.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - pvc.yaml

helmCharts:
  - name: gitlab-runner
    version: 0.57.1
    repo: https://example.com/chartrepo/internal/
    valuesFile: values.yaml
    namespace: gitlab-runner
    releaseName: my-runner

The runner needs a special config for mounting NFS

values.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
runners:
  config: |
    [[runners]]
      ...
      [runners.kubernetes]
        namespace = "{{.Release.Namespace}}"
        image = "ubuntu:22.04"
        privileged = true
        [[runners.kubernetes.volumes.pvc]]
          name = "nfs-storage-pvc"
          mount_path = "/examplestorage"
          read_only = true
    executor: kubernetes    
  privileged: true
  tags: "my-tag"
  name: "my-runner"
  namespace: gitlab-runner
  helpers:
    cpuLimit: 200m
    memoryLimit: 256Mi
    cpuRequests: 100m
    memoryRequests: 128Mi
    image: "gitlab/gitlab-runner-helper:ubuntu-x86_64-v16.0.3"
...

Create the PVC and PV in one step.

pvc.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 50Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    server: 10.10.10.10
    path: /volume

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-storage-pvc
spec:
  storageClassName: nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi

The caveat is that you have to specify the size of the NFS persistent volume, because .spec.resources.requests.storage is mandatory when specifying a persistent volume claim.

Conclusion

Not really trivial, but adding a nfs volume to your Gitlab CI stages should be easy following the guide above.

Built with Hugo
Theme Stack designed by Jimmy