CI/CD Pipeline with Jenkins

Description

Build a complete CI/CD pipeline using Jenkins, Docker, and Kubernetes for automated testing and deployment.

Objective

Automate the build, test, and deployment process for containerized applications on Kubernetes.

Solution

Environment Setup

  1. 1Install Jenkins on a VM or container.
  2. 2Install Docker and configure Jenkins to use Docker.
  3. 3Set up a Kubernetes cluster (EKS, GKE, AKS, or Minikube).
  4. 4Configure kubectl and Jenkins Kubernetes plugin.

Source Code Management

  1. 1Push application code to GitHub/GitLab.
  2. 2Include Dockerfile and Kubernetes manifests (deployment.yaml, service.yaml).
  3. 3
    dockerfileFROM node:16
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    CMD ["npm", "start"]

Jenkins Pipeline Configuration

  1. 1Create Jenkins pipeline with stages: Checkout, Build, Test, Push, Deploy.
  2. 2
    groovypipeline {
      agent any
      stages {
        stage('Checkout') {
          steps { checkout scm }
        }
        stage('Build') {
          steps { sh 'docker build -t myapp:latest .' }
        }
        stage('Test') {
          steps { sh 'docker run myapp:latest npm test' }
        }
        stage('Push') {
          steps { sh 'docker push myrepo/myapp:latest' }
        }
        stage('Deploy') {
          steps { sh 'kubectl apply -f k8s/deployment.yaml' }
        }
      }
    }

Kubernetes Deployment

  1. 1Create deployment.yaml and service.yaml.
  2. 2Apply manifests via Jenkins pipeline.
  3. 3
    yamlapiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: myapp
      template:
        metadata:
          labels:
            app: myapp
        spec:
          containers:
          - name: myapp
            image: myrepo/myapp:latest
            ports:
            - containerPort: 3000
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: myapp-service
    spec:
      type: LoadBalancer
      selector:
        app: myapp
      ports:
      - port: 80
        targetPort: 3000

CI/CD Pipeline Automation

  1. 1Configure Jenkins to trigger builds on Git commits.
  2. 2Add post-build actions: notifications, rollback on failure.
  3. 3Integrate with monitoring tools (Prometheus/Grafana).

Validation

  1. 1Commit code to SCM and verify Jenkins triggers pipeline.
  2. 2Confirm Docker image pushed to registry.
  3. 3Check Kubernetes pods updated with new image.
  4. 4Test application endpoint via Kubernetes service.

Tool Stack Used

  • πŸ”Jenkins
  • 🐳Docker
  • ⎈Kubernetes
  • πŸ”§GitHub/GitLab
  • πŸ“ŠPrometheus
  • πŸ“ŠGrafana