Continuous Integration and Continuous Deployment
Lesson 15: Continuous Integration and Continuous Deployment (CI/CD) with Kubernetes
Continuous Integration (CI) and Continuous Deployment (CD) are essential practices in modern software development that help automate the process of testing and deploying applications. In this lesson, you will learn how to integrate CI/CD pipelines with Kubernetes for automated application updates.
What is CI/CD?
- Continuous Integration (CI): The practice of automatically testing and merging code changes into a shared repository. This ensures that the codebase remains stable and that new features can be integrated smoothly.
- Continuous Deployment (CD): The practice of automatically deploying every change that passes the testing phase to production. This enables quick delivery of features and fixes to users.
CI/CD Pipeline Overview
A CI/CD pipeline typically consists of several stages: 1. Source Code Management: Code is pushed to a version control system (e.g., Git). 2. Build: The application is built into a container image. 3. Test: Automated tests are run to ensure code quality. 4. Deploy: The application is deployed to a Kubernetes cluster.
Setting Up a CI/CD Pipeline with GitHub Actions
In this example, we will use GitHub Actions to create a CI/CD pipeline that builds a Docker image and deploys it to a Kubernetes cluster.
Step 1: Create a Dockerfile
Create a Dockerfile in your application directory:
# Dockerfile
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
Step 2: Create a GitHub Actions Workflow
Create a directory .github/workflows in your project and add a file named ci-cd.yml:
# .github/workflows/ci-cd.yml
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build Docker image
run: |
docker build -t myapp:${{ github.sha }} .
- name: Push Docker image
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker push myapp:${{ github.sha }}
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Set up Kubeconfig
run: |
echo "${{ secrets.KUBE_CONFIG }}" > kubeconfig
export KUBECONFIG=kubeconfig
- name: Deploy to Kubernetes
run: |
kubectl set image deployment/myapp myapp=myapp:${{ github.sha }}
kubectl rollout status deployment/myapp
Step 3: Configure Secrets in GitHub
You need to configure the following secrets in your GitHub repository:
- DOCKER_USERNAME: Your Docker Hub username.
- DOCKER_PASSWORD: Your Docker Hub password.
- KUBE_CONFIG: The base64 encoded kubeconfig file for accessing your Kubernetes cluster.
Best Practices
- Use Semantic Versioning: Tag your Docker images with semantic versioning to keep track of changes.
- Automate Testing: Include testing in your pipeline to catch issues early.
- Monitor Rollbacks: Implement strategies to roll back deployments in case of failures.
Common Mistakes: - Not configuring secrets properly, leading to failed deployments. - Forgetting to run tests before deploying, which can introduce bugs to production.
Summary
In this lesson, you have learned: - The basics of CI/CD and its importance in modern application development. - How to set up a CI/CD pipeline using GitHub Actions to automate the building and deployment of applications to Kubernetes. - Best practices for maintaining a robust CI/CD pipeline.
By following these practices, you can ensure a smoother development workflow and faster delivery of features to your users.
Exercises
Exercise 1: Modify the Dockerfile
- Add a new environment variable to your Dockerfile and test it in your application.
Exercise 2: Extend the CI/CD Pipeline
- Add a step to run unit tests in your GitHub Actions workflow before building the Docker image.
Exercise 3: Implement Rollback
- Modify the deployment step in your CI/CD pipeline to include a rollback strategy in case of a failed deployment.
Summary
- CI/CD automates testing and deployment processes, improving development efficiency.
- GitHub Actions can be used to create CI/CD pipelines for Kubernetes deployments.
- Proper configuration of secrets is crucial for the success of CI/CD workflows.
- Always include automated testing in your CI/CD pipeline to catch issues early.
- Implement rollback strategies to handle deployment failures effectively.