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
- 1Install Jenkins on a VM or container.
- 2Install Docker and configure Jenkins to use Docker.
- 3Set up a Kubernetes cluster (EKS, GKE, AKS, or Minikube).
- 4Configure kubectl and Jenkins Kubernetes plugin.
Source Code Management
- 1Push application code to GitHub/GitLab.
- 2Include Dockerfile and Kubernetes manifests (deployment.yaml, service.yaml).
- 3
dockerfile
FROM node:16 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . CMD ["npm", "start"]
Jenkins Pipeline Configuration
- 1Create Jenkins pipeline with stages: Checkout, Build, Test, Push, Deploy.
- 2
groovy
pipeline { 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
- 1Create deployment.yaml and service.yaml.
- 2Apply manifests via Jenkins pipeline.
- 3
yaml
apiVersion: 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
- 1Configure Jenkins to trigger builds on Git commits.
- 2Add post-build actions: notifications, rollback on failure.
- 3Integrate with monitoring tools (Prometheus/Grafana).
Validation
- 1Commit code to SCM and verify Jenkins triggers pipeline.
- 2Confirm Docker image pushed to registry.
- 3Check Kubernetes pods updated with new image.
- 4Test application endpoint via Kubernetes service.
Tool Stack Used
- πJenkins
- π³Docker
- βKubernetes
- π§GitHub/GitLab
- πPrometheus
- πGrafana