DEV Community

Alex Aslam
Alex Aslam

Posted on

1

Jenkins on Kubernetes: Dynamic Agents & Effortless Scalability for Modern CI/CD 🚀

Hey there, DevOps adventurer! 👋 Let’s talk about a problem you know all too well: Your Jenkins setup is either starving for resources or drowning in idle agents, burning cash faster than a startup’s runway. Static agents? More like static cling—they’re always there, even when you don’t need them.

But what if Jenkins could spin up agents on-demand like Uber for CI/CD, scale to zero when idle, and handle 100x traffic without breaking a sweat? Enter Jenkins on Kubernetes: where pipelines become elastic, costs shrink, and your sanity returns. Let’s dive in!


Why Jenkins + Kubernetes? (Spoiler: It’s Like CI/CD Autopilot)

Imagine this:

  • Dynamic Agents: Agents are Kubernetes pods that vanish after jobs finish. No more paying for idle VMs!
  • Auto-Scaling: Need 10 agents at 2 PM and zero at 2 AM? Kubernetes handles it.
  • Consistent Environments: Every job runs in a fresh, Dockerized workspace. “Works on my machine” becomes “Works on every machine.”

Step 1: Install Jenkins on Kubernetes

A. Helm Makes It Easy

helm repo add jenkins https://charts.jenkins.io  
helm install jenkins jenkins/jenkins -f values.yaml  
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Customize values.yaml to set resource limits, plugins, and ingress.

B. Connect Jenkins to Kubernetes

  1. Install the Kubernetes Plugin in Jenkins.
  2. Go to Manage Jenkins > Manage Nodes and Clouds > Configure Clouds.
  3. Add your Kubernetes cluster (kubeconfig or service account).

![Jenkins Kubernetes cloud configuration]


Step 2: Define Dynamic Agents with Pod Templates

Agents are just Kubernetes pods! Define them in your Jenkinsfile:

pipeline {  
  agent {  
    kubernetes {  
      label 'my-dynamic-agent'  
      yaml """  
        spec:  
          containers:  
          - name: jnlp  
            image: jenkins/inbound-agent:latest  
          - name: node  
            image: node:18  
            command: ['sleep', 'infinity']  # Keep alive for multi-container pods  
      """  
    }  
  }  
  stages {  
    stage('Build') {  
      steps {  
        container('node') {  
          sh 'npm install && npm run build'  
        }  
      }  
    }  
  }  
}  
Enter fullscreen mode Exit fullscreen mode

What’s happening?

  • A Kubernetes pod spins up with Node.js and Jenkins’ JNLP agent.
  • Jobs run in the node container.
  • The pod self-destructs after the job. 💥

Step 3: Auto-Scale Your Cluster

A. Horizontal Pod Autoscaler (HPA)

Scale Jenkins agents based on CPU/memory:

apiVersion: autoscaling/v2  
kind: HorizontalPodAutoscaler  
metadata:  
  name: jenkins-agent-hpa  
spec:  
  scaleTargetRef:  
    apiVersion: apps/v1  
    kind: Deployment  
    name: jenkins-agent  
  minReplicas: 0  # Scale to zero!  
  maxReplicas: 10  
  metrics:  
    - type: Resource  
      resource:  
        name: cpu  
        target:  
          type: Utilization  
          averageUtilization: 80  
Enter fullscreen mode Exit fullscreen mode

B. Cluster Autoscaler

For cloud providers (AWS, GCP), let the cluster itself scale nodes up/down.


Real-World Wins

Case Study: Startup Saves 60% on Cloud Costs

A fintech startup switched from static AWS instances to Kubernetes-powered Jenkins:

  • Before: 10 always-on agents = $1,200/month.
  • After: Dynamic agents + scale-to-zero = $480/month.

Enterprise Handles Black Friday Traffic

An e-commerce giant scaled Jenkins agents to 200 pods during peak sales, then dropped to 5 post-holiday—without manual intervention.


Pro Tips to Avoid Facepalms 🤦

  1. Optimize Pod Templates:
    • Use lightweight base images (Alpine > Ubuntu).
    • Cache dependencies in PersistentVolumes.
  2. Set Resource Limits: Prevent greedy pods from hogging the cluster.
   resources:  
     limits:  
       cpu: "1"  
       memory: "2Gi"  
Enter fullscreen mode Exit fullscreen mode
  1. Secure Your Cluster:
    • Use RBAC to restrict Jenkins permissions.
    • Store secrets in Kubernetes Secrets, not Jenkinsfiles.

Common “Why Isn’t This Working?!” Moments

  • Pod Stuck in Pending: Check node resources or PersistentVolumeClaims.
  • Network Issues: Ensure Jenkins master can reach the Kubernetes API.
  • JNLP Connection Failures: Verify the Jenkins URL in Kubernetes plugin settings.

Level Up: Advanced Tactics

  • Spot Instances: Use AWS Spot or GCP Preemptible VMs for cheaper agents.
  • Multi-Container Pods: Run linters, tests, and builds in parallel.
  • GitOps Pipelines: Deploy with ArgoCD or Flux directly from Jenkins.

Your DevOps Superpower Awaits

Jenkins on Kubernetes isn’t just a setup—it’s a mindset shift. You’re no longer babysitting servers; you’re orchestrating a self-healing, cost-efficient CI/CD symphony.

Next Steps:

  1. Migrate one pipeline to Kubernetes agents.
  2. Watch your cloud bill drop. 😎
  3. Share this guide with that one teammate still SSH-ing into static VMs.

Hit a snag? Drop a comment below. Let’s debug together! 🛠️

Sentry image

Make it make sense

Make sense of fixing your code with straight-forward application monitoring.

Start debugging →

Top comments (2)

Collapse
 
michael_liang_0208 profile image
Michael Liang

Nice post for CI/CD pipelines!

Collapse
 
alex_aslam profile image
Alex Aslam

Thanks

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more