Complete Guide to CI/CD with Jenkins and Kubernetes

Why CI/CD Matters

Continuous Integration and Continuous Deployment (CI/CD) automates the process of testing, building, and deploying your applications. A well-designed pipeline catches bugs early, ensures consistent deployments, and dramatically reduces time-to-production.

Pipeline Architecture

Our pipeline follows these stages:

  1. Checkout: Pull the latest code from the Git repository.
  2. Unit Tests: Run the full test suite in an isolated Docker container.
  3. UI Tests: Execute Playwright-based browser tests against a running instance.
  4. Build Image: Create a Docker image with the application.
  5. Push Image: Push to a container registry.
  6. Deploy: Rolling update on Kubernetes cluster.
  7. Health Check: Verify the deployment is healthy.

Jenkinsfile Example

pipeline {
    agent any

    stages {
        stage('Test') {
            agent {
                docker { image 'python:3.11' }
            }
            steps {
                sh 'pip install -r requirements.txt'
                sh 'pytest --tb=short -v'
            }
        }

        stage('Build & Push') {
            steps {
                script {
                    def img = docker.build("myapp:${env.BUILD_NUMBER}")
                    docker.withRegistry('https://registry.example.com', 'registry-creds') {
                        img.push()
                        img.push('latest')
                    }
                }
            }
        }

        stage('Deploy') {
            steps {
                sh "kubectl set image deployment/myapp myapp=myapp:${env.BUILD_NUMBER}"
                sh 'kubectl rollout status deployment/myapp --timeout=120s'
            }
        }
    }
}

Kubernetes Deployment

Our Kubernetes setup uses Deployments with rolling updates, ConfigMaps for environment variables, and Services with NodePort for internal access behind an Nginx reverse proxy.

Monitoring & Alerts

Integrate Sentry for error tracking and Prometheus for metrics. Set up alerts for deployment failures, high error rates, and resource exhaustion to catch problems before users do.

Next Docker Best Practices for Production Applications

💬 Comments (0)

No comments yet. Be the first to share your thoughts!

Leave a Comment

Want to receive reply notifications? Login or Sign up to get notified when someone replies to your comment!

Your comment will be reviewed before it appears publicly.