Cloud Native 10 min read

How to Create Static and Dynamic Jenkins Agents on Kubernetes

This guide explains how to manually create Jenkins JNLP agents on a Kubernetes cluster, covering both static Deployment‑based agents and dynamic agents using the Kubernetes plugin, with detailed YAML definitions, pipeline scripts, and supporting helper functions for a complete CI/CD workflow.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
How to Create Static and Dynamic Jenkins Agents on Kubernetes

Manually create a Jenkins agent node on the Jenkins server by selecting the JNLP type and retrieve the required JENKINS_AGENT_NAME and JENKINS_AGENT_SECRET values.

For a static agent, write a Deployment manifest that includes the agent name, namespace, container image, resource limits, and environment variables for the Jenkins URL, secret, and name, then apply it with kubectl apply.

For a dynamic agent, install the Kubernetes plugin, configure a cloud with the cluster information, and define the pipeline agent section using a YAML pod template and the necessary stages.

Steps to create the node in the Jenkins UI (Node Management → New Node) are illustrated with screenshots, showing the configuration of node name and secret, which are later used in the deployment.

Static agent deployment example:

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: jenkinsagent
  name: jenkinsagent
  namespace: devops
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: jenkinsagent
  template:
    metadata:
      labels:
        k8s-app: jenkinsagent
    namespace: devops
    name: jenkinsagent
    spec:
      containers:
        - name: jenkinsagent
          image: jenkins/inbound-agent:4.10-3-jdk8
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              cpu: 1000m
              memory: 2Gi
            requests:
              cpu: 500m
              memory: 512Mi
          env:
            - name: JENKINS_URL
              value: http://192.168.1.200:8080
            - name: JENKINS_SECRET
              value: 2f3f27ee602171c5b51bc69c2bda501d703ac65f42de16600b3e3007dfdec422
            - name: JENKINS_AGENT_NAME
              value: buildserver1
            - name: JENKINS_AGENT_WORKDIR
              value: /home/jenkins/workspace

Dynamic agent pipeline (using the Kubernetes plugin) example:

@Library("mylib@feature-k8s") _
podYaml = """
kind: Pod
apiVersion: v1
metadata:
  labels:
    k8s-app: jenkinsagent
  name: jnlp
  namespace: devops
spec:
  securityContext:
    runAsUser: 0
  containers:
    - name: jnlp
      image: jenkins/inbound-agent:4.10-3-jdk8
      imagePullPolicy: IfNotPresent
    - name: maven
      image: maven:3.8.4-jdk-8
      imagePullPolicy: IfNotPresent
      command:
      - cat
      tty: true
    - name: dind
      image: 'docker:stable-dind'
      command:
      - dockerd
      - --host=unix:///var/run/docker.sock
      - --host=tcp://0.0.0.0:8000
      - --insecure-registry=192.168.1.200:8088
      securityContext:
        privileged: true
      volumeMounts:
      - mountPath: /var/run
        name: docker-dir
    - name: "docker"
      tty: true
      image: docker:19.03.15-git
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - mountPath: /var/run
        name: docker-dir
    - name: "curl"
      tty: true
      image: curlimages/curl:7.84.0
      imagePullPolicy: IfNotPresent
      command:
      - cat
    - name: "helm"
      tty: true
      image: dtzar/helm-kubectl:3.9.0
      imagePullPolicy: IfNotPresent
      command:
      - cat
  volumes:
    - name: docker-dir
      emptyDir: {}
"""

pipeline {
  agent {
    kubernetes {
      label "test01"
      cloud 'kubernetes'
      yaml podYaml
    }
  }
  options { skipDefaultCheckout() }
  stages {
    stage("Checkout") {
      steps {
        script {
          println "GetCode"
          // checkout code and set env variables
        }
      }
    }
    // Additional stages: Build, PushArtifact, Docker, ReleaseFile, HelmDeploy, CheckHealth, RollOut
  }
}

// Helper functions: GetCommitID, PushArtifact, HttpReqByHttpRequest, HttpReq, etc.

The pipeline includes stages for checking out source code, building with Maven, pushing artifacts to a Nexus repository, building and pushing Docker images, updating Helm values files in GitLab, deploying with Helm, health checking, and optional rollback via user input.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud Nativeci/cdKubernetesDevOpsAgentPipelineJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.