Continuous Integration and Continuous Deployment (CI/CD) pipelines have become essential for modern application development, and Kubernetes is a popular choice for orchestrating containerized applications. Combine this with GitHub Actions, and you have a powerful toolchain for automating your container workflows from code to deployment.
This guide will walk you through setting up a CI/CD pipeline using GitHub Actions to build, test, and deploy a containerized application to a Kubernetes cluster.
Why Use Kubernetes with GitHub Actions?
Kubernetes and GitHub Actions complement each other perfectly:
- Scalability: Kubernetes ensures your applications run reliably at scale.
- Automation: GitHub Actions automates every step of your CI/CD pipeline.
- Flexibility: GitHub Actions workflows can be customized to fit your specific Kubernetes deployment needs.
Together, they streamline the deployment process, enabling teams to focus on building and improving applications rather than managing infrastructure.
Prerequisites
Before you begin, make sure you have:
- A Kubernetes cluster (e.g., GKE, EKS, AKS, or Minikube).
kubectl
installed and configured to interact with your cluster.- A Dockerfile in your project for containerizing your application.
- A GitHub repository for your application code.
Setting Up the CI/CD Pipeline
Step 1: Create a GitHub Workflow File
In your repository, create a .github/workflows/deploy.yml
file. This file defines the pipeline steps for GitHub Actions.
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/my-app:latest
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Configure Kubernetes Context
run: |
echo "${{ secrets.KUBE_CONFIG }}" > kubeconfig
export KUBECONFIG=kubeconfig
- name: Deploy to Kubernetes
run: |
kubectl apply -f k8s/deployment.yaml
kubectl rollout status deployment/my-app
Explanation of the Workflow
- Trigger: The workflow runs on every push to the
main
branch. - Checkout Code: Fetches the code from your repository.
- Set up Docker Buildx: Prepares the environment for building multi-platform images.
- Log in to Docker Hub: Authenticates with Docker Hub using secrets stored in your repository.
- Build and Push Docker Image: Builds the Docker image and pushes it to Docker Hub.
- Set up kubectl: Installs the Kubernetes CLI for managing your cluster.
- Configure Kubernetes Context: Sets up access to your Kubernetes cluster using a
KUBE_CONFIG
secret. - Deploy to Kubernetes: Applies your Kubernetes manifests and ensures the deployment is successful.
Kubernetes Manifests
Ensure you have the necessary Kubernetes manifests in your repository. For example:
File: deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-dockerhub-username/my-app:latest
ports:
- containerPort: 80
File: service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
File: Secrets Management
Store sensitive data securely in GitHub Secrets:
DOCKER_USERNAME
: Your Docker Hub username.DOCKER_PASSWORD
: Your Docker Hub password.KUBE_CONFIG
: Base64-encoded Kubernetes configuration file.
To add secrets, go to your repository settings and navigate to Secrets and variables > Actions.
Testing and Monitoring
Testing the Workflow
- Push changes to the
main
branch to trigger the workflow. - Monitor the progress in the Actions tab of your GitHub repository.
Monitoring Kubernetes Deployment
Use kubectl
to verify the deployment:
kubectl get deployments
kubectl get pods
kubectl get services
Conclusion
By combining Kubernetes with GitHub Actions, you can automate the entire CI/CD process for your containerized applications. This approach not only saves time but also ensures consistency and reliability in your deployments. Start building your CI/CD pipeline today and unlock the full potential of modern application development!