Solution to mount a persistent "dynamic" file in Kubernetes

6/22/2020

The idea is to store persistently the data generated by the user in Jenkins Dashboard (jobs, scriptlers, credentials, ...) so when I mount theses volumes after each restart, I am able to retrieve the stored data (mounted volumes) in jobs and scriptlers. (I am not using the solution with JcasC (yaml files) for jobs, scriptlers, ... as the user will define them dynamically from the UI).

Now, I am trying to mount the "credentials.xml" file (persistent volume), as I made with "scriptler" and "jobs" folder mount. But it does not work because it's a file, not a folder.

The solution to mount as a ConfigMap is not applicable here as the file cannot be predefined because modified by the user in the dashboard for any change in credentials.

I am looking for a solution to mount this file like a "persistent volume"

Here is my YAML deployment (values) file (it does not work with 'jenkins-credentials' mount):

persistence:
  volumes:
    - name: jenkins-jobs
      persistentVolumeClaim:
        claimName: jenkins-jobs

    - name: jenkins-scriptlers
      persistentVolumeClaim:
        claimName: jenkins-scriptlers

    - name: jenkins-credentials
      configMap:
        name: cm-credentials-xml

  mounts:
    - name: jenkins-jobs
      mountPath: /var/jenkins_home/jobs
      readOnly: false

    - name: jenkins-scriptlers
      mountPath: /var/jenkins_home/scriptler
      readOnly: false

    - name: jenkins-credentials
      mountPath: /var/jenkins_home/credentials.xml
      #subPath: credentials.xml
      readOnly: false

Thanks

-- Maurice Amar
jenkins
kubernetes
mounted-volumes

1 Answer

6/22/2020

Since all the VolumeMounts share the same 'volume' and the directory /var/jenkins_home you actually need one main volume. Something like this should work. (I tested myself mounting a file and works for me) This assumes that the root of your persistent volume (/) has the main content.

persistence:
  volumes:
    - name: jenkins-home
      persistentVolumeClaim:
        claimName: jenkins-home

  mounts:
    - name: jenkins-home
      mountPath: /var/jenkins_home/jobs
      subpath: jobs
      readOnly: false

    - name: jenkins-home
      mountPath: /var/jenkins_home/scriptler
      subPath: scriptler
      readOnly: false

    - name: jenkins-home
      mountPath: /var/jenkins_home/credentials.xml
      subPath: credentials.xml
      readOnly: false

Note: This is not K8s configuration, so I assume it's Jenkins config YAML.

-- Rico
Source: StackOverflow