Jenkinsfile Template to mount a volume

7/29/2019

Here is my containerTemplate snippet from Jenkinsfile which creates a pod and a container called mvn-c1 onto Kubernetes.

containerTemplate(
  name: 'mvn-c1',
  image: 'mycompany.lab/build_images/mvn_c1_k8s:0.3',
  privileged: true,
  ttyEnabled: true,
  command: 'cat',
  imagePullSecrets: ['docker-repo'],

  volumeMounts: [ name: 'maven-repo1' , mountPath: '/root/.m2' ],
  volumes: [
    nfsVolume( mountPath: '/root/.m2', serverAddress: 'nfs-server-ip', 
    serverPath: '/jenkins_data', readOnly: false ),
  ]
)

The problem is that the volume is not able to mount to the container nor doesn't show any parse errors on the console.

I have referred this documentation to construct the containerTemplate

Has anybody had luck trying this method?

-- Vamshi Krishna Santhapuri
jenkins-pipeline
kubernetes
persistent-volumes

2 Answers

1/9/2020

Firstly thanks for the detailed explanation about the endpoint correlation of NFS IP with namepsaces.. and my apologies for the long gap between the posts.

I have Intentionally hidden podTemplate and posted Jenkinsfile snippet.

I was able to achieve this with the desired result by giving reference on my slave POD yaml file which has the NFS ip and mount point tags.

I referred it as below:

      kubernetes {
      label "slavepod-${UUID.randomUUID().toString()}"
      yamlFile './k8s-pod.yaml'
    }
  }

and my POD yaml has the following

  volumes:
    - name: maven-repo1
      nfs:
        server: NFS-IP
        path: /jenkins_data
-- Vamshi Krishna Santhapuri
Source: StackOverflow

8/26/2019

Welcome on StackOverflow @Vamshi

I think you have two issues with your current Pipeline definition:

  1. volumes are part of podTemplate not containerTemplate

WARNING: Unknown parameter(s) found for class type 'org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate': volumes

  1. Usually your Kubernetes plugin spawns Jenkins slave Pods in another namespace than your NFS server resides, therefore it's safer to specify for NFS volumes as 'serverAddress' an IP address of NFS server instead of its Kubernetes Service name.

Here is a fully working example:

podTemplate(containers: [
    containerTemplate(name: 'maven',
    image: 'maven:3.3.9-jdk-8-alpine',
    ttyEnabled: true,
    command: 'cat')
  ],
    volumes: [
    nfsVolume( mountPath: '/root/.m2', serverAddress: '10.0.174.57', 
    serverPath: '/', readOnly: false ),
    ]
  ) {

    node(POD_LABEL) {
        stage('Get a Maven project') {
            container('maven') {
                stage('Build a Maven project') {
                    sh 'while true; do date > /root/.m2/index.html; hostname >> /root/.m2/index.html; sleep $(($RANDOM % 5 + 5)); done'
                }
            }
        }

    }
}

Verifying the correctness of nfs-based Persistence Volume mounted inside POD:

kubectl exec container-template-with-nfs-pv-10-cl42l-042tq-z3n7b -c maven -- cat /root/.m2/index.html

Output: Mon Aug 26 14:47:28 UTC 2019 nfs-busybox-9t7wx

-- Nepomucen
Source: StackOverflow