Kubernetes Deployment
Learn how to securely configure, orchestrate, and execute zero-downtime application updates to a live Kubernetes cluster using automated Jenkins pipelines.
What is a Kubernetes Deployment Stage in Jenkins?
Once an application is successfully containerized and pushed to a remote registry, the final step in a continuous delivery lifecycle is updating the target runtime infrastructure. A Kubernetes Deployment stage within a Jenkins pipeline automates this step by interacting directly with the cluster API to roll out fresh image versions securely.
Rather than forcing operators to manually modify runtime clusters using a terminal, Jenkins authenticates as a trusted deployment service account. It takes the newly built image version, injects it into standardized manifest specifications, applies configurations dynamically, and tracks cluster rollouts until the workload stabilizes across public load balancers.
Key Concepts
Kubeconfig Encapsulation
Using the Jenkins withKubeConfig or withKubeConfigFile credentials wrapper to cleanly inject API server endpoints and authentication tokens exclusively during the deployment block.
Dynamic Manifest Patching
Utilizing Linux token-replacement tools (like envsubst or sed filters) to modify standard image references inside raw deployment.yaml configurations dynamically per build.
Rollout Status Verification
Invoking explicit validation commands like kubectl rollout status to halt pipeline progression until container replicas successfully pass their internal cluster health checks.
Automated Rolling Updates
Leveraging Kubernetes' native upgrade loop to spin up fresh application pods before terminating old ones, ensuring zero-downtime service availability.
Practical Jenkins Example
The following declarative pipeline establishes a secure API endpoint context, substitutes the current tracking tag into a target Kubernetes resource file, and triggers a monitored deployment update:
pipeline {
agent any
environment {
// Defines image coordinate patterns mapped to the project container registry
REGISTRY_USER = 'yourdockerhubusername'
APP_NAME = 'microservice-core'
IMAGE_TAG = "${REGISTRY_USER}/${APP_NAME}:${BUILD_NUMBER}"
}
stages {
stage('Deploy to Kubernetes Cluster') {
steps {
echo 'Initializing secure cluster API configuration context...'
// Restricts API tokens to this scoped block using the Jenkins Kubernetes CLI credentials context
withKubeConfig([credentialsId: 'k8s-cluster-credentials-id', serverUrl: 'https://your-k8s-api-endpoint.com']) {
echo 'Injecting dynamic build tags into targeted manifest templates...'
// Updates the generic manifest placeholder with the explicit current image version string
sh "sed -i 's|IMAGE_PLACEHOLDER|${IMAGE_TAG}|g' k8s/deployment.yaml"
echo 'Applying state definitions to the active namespace...'
sh 'kubectl apply -f k8s/deployment.yaml'
sh 'kubectl apply -f k8s/service.yaml'
echo 'Monitoring update progression flags...'
// Blocks pipeline execution until the rollout completely succeeds or timeouts out
sh "kubectl rollout status deployment/${APP_NAME} --timeout=120s"
}
}
}
}
}
Practice Exercise
- Go to Manage Jenkins → Credentials and add your cluster configuration details as a Secret file or Kubernetes service account token named
k8s-cluster-credentials-id. - Ensure your repository code structures include a valid
k8s/deployment.yamlconfiguration tracking a dummy image string likeIMAGE_PLACEHOLDER. - Create a new Pipeline build job target inside your Jenkins environment folder titled
k8s-cluster-deployment. - Paste the declarative execution example shared in the configuration section above into your pipeline editor.
- Execute a manual execution run, verify that the manifest substitutes parameters accurately via the **Console Output**, and inspect the cluster layout directly using your local terminal.
Summary
You have completed the Kubernetes Deployment lesson. Shifting production states via imperative CLI commands highlights the need for declarative consistency, setting up a natural progression toward full **Jenkins and GitOps** patterns.