<p>I have created a shared library and it has a groovy file named <code>myBuildPlugin.groovy</code>:</p>

<pre><code>def label = "worker-${UUID.randomUUID().toString()}"

podTemplate(label: label, yaml: """
          apiVersion: v1
          kind: Pod
          metadata:
            name: my-build
          spec:
            containers:
              - name: jnlp
                image: dtr.myhost.com/test/jenkins-build-agent:latest
                ports:
                  - containerPort: 8080
                  - containerPort: 50000
                resources:
                  limits:
                    cpu : 1
                    memory : 1Gi
                  requests:
                    cpu: 200m
                    memory: 256Mi
                env:
                  - name: JENKINS_URL
                    value: http://jenkins:8080
              - name: mongo
                image: mongo
                ports:
                  - containerPort: 8080
                  - containerPort: 50000
                  - containerPort: 27017
                resources:
                  requests:
                    cpu: 200m
                    memory: 256Mi
                  limits:
                    cpu: 1
                    memory: 512Mi
            imagePullSecrets:
            - name: dtrsecret""")
        {
            node(label) {
                pipelineParams.step1.call([label : label])
        }
    }
</code></pre>

<p>When in my project I use myBuildPlugin as below, the log shows it waits for an executor forever. When I look at Jenkins I can see the agent is being created but for some reason it can't talk to it via port <code>50000</code> (or perhaps the pod can't talk to the agent!)</p>

<p>Later I tried to remove <code>yaml</code> and instead used the following code:</p>

<pre><code>podTemplate(label: 'mypod', cloud: 'kubernetes', containers: [
        containerTemplate(
                name: 'jnlp',
                image: 'dtr.myhost.com/test/jenkins-build-agent:latest',
                ttyEnabled: true,
                privileged: false,
                alwaysPullImage: false,
                workingDir: '/home/jenkins',
                resourceRequestCpu: '1',
                resourceLimitCpu: '100m',
                resourceRequestMemory: '100Mi',
                resourceLimitMemory: '200Mi',
                envVars: [
                        envVar(key: 'JENKINS_URL', value: 'http://jenkins:8080'),
                ]
        ),
        containerTemplate(name: 'maven', image: 'maven:3.5.0', command: 'cat', ttyEnabled: true),
        containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true)
],
        volumes: [
                emptyDirVolume(mountPath: '/etc/mount1', memory: false),
                hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
        ],
        imagePullSecrets: [ 'dtrsecret' ],
)
        {
            node(label) {
                pipelineParams.step1.call([label : label])
            }
        }
</code></pre>

<p>Still no luck. Interestingly if I define all these containers in Jenkins configuration, things work smoothly. This is my configuration:</p>

<p><a href="https://i.stack.imgur.com/EyqQe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EyqQe.png" alt="enter image description here"></a></p>

<p>and this is the pod template configuration:</p>

<p><a href="https://i.stack.imgur.com/L2574.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L2574.png" alt="enter image description here"></a></p>

<p>It appears that if I change the label to something other that <code>jenkins-jenkins-slave</code> the issue happens. This is the case even if it's defined via Jenkins' configuration page. If that's the case, how am I suppose to create multiple Pod template for different type of projects?</p>

<p>Just today, I also tried to use pod inheritance as below without any success:</p>

<pre><code>def label = 'kubepod-test'
podTemplate(label : label, inheritFrom : 'default',
        containers : [
                containerTemplate(name : 'mongodb', image : 'mongo', command : '', ttyEnabled : true)
        ]
)
        {
            node(label) {


            }

        }
</code></pre>

<p>Please help me on this issue. Thanks</p>

<p>There's something iffy about your pod configuration, you can't have your Jenkins and Mongo containers using the same port <code>50000</code>. Generally, you want to specify a unique port since pods share the same <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#what-is-a-pod" rel="nofollow noreferrer">port space</a>.</p>

<p>In this case looks like you need port <code>50000</code> to set up a tunnel to the Jenkins agent. Keep in mind that the Jenkins plugin might be doing other things such as setting up a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Kubernetes Service</a> or using the internal <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">Kubernetes DNS</a>.</p>

<p>In the second example, I don't even see port <code>50000</code> exposed.</p>
