How to Deploy to Kubernetes Using Jenkins: Step-by-Step Guide
To deploy to Kubernetes using
Jenkins, create a Jenkins pipeline that builds your app, creates a Docker image, pushes it to a registry, and then applies Kubernetes manifests using kubectl. Use Jenkins plugins like Kubernetes CLI Plugin or run shell commands inside pipeline steps to interact with your cluster.Syntax
A typical Jenkins pipeline for Kubernetes deployment includes these steps:
- Build: Compile your application.
- Docker Build & Push: Create and push a Docker image to a registry.
- Deploy: Use
kubectl applyto update Kubernetes resources.
Each step can be scripted in a Jenkinsfile using declarative or scripted pipeline syntax.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo Building application'
}
}
stage('Docker Build & Push') {
steps {
sh 'docker build -t myapp:latest .'
sh 'docker tag myapp:latest myregistry/myapp:latest'
sh 'docker push myregistry/myapp:latest'
}
}
stage('Deploy to Kubernetes') {
steps {
sh 'kubectl apply -f k8s/deployment.yaml'
}
}
}
}Example
This example Jenkins pipeline builds a Docker image, pushes it to Docker Hub, and deploys to Kubernetes using kubectl. It assumes you have Docker and kubectl configured on the Jenkins agent.
groovy
pipeline {
agent any
environment {
DOCKER_IMAGE = 'yourdockerhubusername/myapp:latest'
}
stages {
stage('Build') {
steps {
sh 'echo Building the app...'
}
}
stage('Docker Build & Push') {
steps {
sh "docker build -t ${DOCKER_IMAGE} ."
sh "docker push ${DOCKER_IMAGE}"
}
}
stage('Deploy to Kubernetes') {
steps {
sh "kubectl set image deployment/myapp-deployment myapp=${DOCKER_IMAGE}"
sh 'kubectl rollout status deployment/myapp-deployment'
}
}
}
}Output
Building the app...
Sending build context to Docker daemon 10.24MB
Step 1/5 : FROM node:16
...
Successfully built abc123def456
Successfully tagged yourdockerhubusername/myapp:latest
The push refers to repository [docker.io/yourdockerhubusername/myapp]
...
deployment.apps/myapp-deployment image updated
Waiting for rollout to finish: 1 old replicas are pending termination...
deployment "myapp-deployment" successfully rolled out
Common Pitfalls
- Missing Kubernetes credentials: Jenkins must have access to the Kubernetes cluster via kubeconfig or service account.
- Docker login not done: Jenkins needs to authenticate to Docker registry before pushing images.
- Incorrect kubectl context: Ensure
kubectlcommands run in the right cluster context. - Not waiting for rollout: Skipping rollout status check can hide deployment failures.
groovy
pipeline {
agent any
stages {
stage('Deploy without login') {
steps {
// This will fail if Docker login is missing
sh 'docker push myregistry/myapp:latest'
}
}
stage('Correct Docker login') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-creds', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh 'echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin'
sh 'docker push myregistry/myapp:latest'
}
}
}
}
}Quick Reference
Tips for smooth Jenkins to Kubernetes deployment:
- Use Jenkins credentials store for Docker and Kubernetes secrets.
- Run
kubectlcommands inside Jenkins agents with proper kubeconfig. - Use
kubectl rollout statusto verify deployment success. - Automate image tagging with build numbers or Git commit hashes.
Key Takeaways
Create a Jenkins pipeline that builds, pushes Docker images, and deploys with kubectl.
Ensure Jenkins has access to Kubernetes cluster and Docker registry credentials.
Use kubectl rollout status to confirm successful deployment.
Automate image tagging to avoid caching and deployment issues.
Test kubectl commands manually before automating in Jenkins.