DEV Community

Yash Sonawane
Yash Sonawane

Posted on

2 1 1 1 1

DevOps Made Simple: A Beginner’s Guide to Setting Up CI/CD Pipelines with GitHub Actions & Kubernetes

Introduction

Continuous Integration and Continuous Deployment (CI/CD) is a fundamental practice in modern DevOps, ensuring rapid, reliable, and automated software delivery. GitHub Actions and Kubernetes provide a powerful combination for automating CI/CD workflows. This guide will walk you through setting up a CI/CD pipeline using GitHub Actions and deploying an application to Kubernetes step by step.

By the end of this guide, you’ll understand how to:

  • Automate builds and tests with GitHub Actions
  • Deploy applications to Kubernetes using GitHub Actions
  • Avoid common pitfalls in setting up CI/CD pipelines

Understanding CI/CD, GitHub Actions & Kubernetes

What is CI/CD?

CI/CD is a software development practice that automates integrating code changes (CI) and delivering software updates (CD). This approach minimizes errors, speeds up releases, and ensures consistency.

Why GitHub Actions?

GitHub Actions allows developers to automate workflows directly within GitHub repositories. With its simple YAML-based configuration, you can define CI/CD pipelines seamlessly.

Why Kubernetes?

Kubernetes is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications.

Step-by-Step Guide: Setting Up a CI/CD Pipeline

Step 1: Create a GitHub Repository

Ensure you have a GitHub repository where your application code is stored.

Step 2: Write a GitHub Actions Workflow

Create a .github/workflows/deploy.yml file in your repository with the following structure:

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
        uses: docker/setup-buildx-action@v1

      - name: Build Docker Image
        run: docker build -t myapp:latest .

      - name: Push Docker Image
        run: |
          echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
          docker tag myapp:latest mydockerhubusername/myapp:latest
          docker push mydockerhubusername/myapp:latest

  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to Kubernetes
        run: |
          echo "${{ secrets.KUBECONFIG }}" | base64 --decode > kubeconfig.yaml
          kubectl --kubeconfig=kubeconfig.yaml apply -f k8s/deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Kubernetes Deployment & Service Files

Create a k8s/deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: mydockerhubusername/myapp:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Step 4: Store Secrets Securely

Store your Docker credentials and Kubernetes configuration as GitHub Secrets:

  • DOCKER_USERNAME
  • DOCKER_PASSWORD
  • KUBECONFIG

Step 5: Push Code and Automate Deployment

Commit and push your code to trigger the GitHub Actions workflow. Your application will be built, pushed to Docker Hub, and deployed to Kubernetes automatically.

Real-World Applications of GitHub Actions & Kubernetes

Many organizations use GitHub Actions and Kubernetes for automating deployments. Examples include:

  • E-commerce platforms ensuring smooth feature releases
  • Financial applications maintaining high availability
  • Streaming services handling large-scale traffic efficiently

Common Mistakes & Best Practices

Mistakes to Avoid:

  • Hardcoding secrets instead of using GitHub Secrets
  • Not defining proper resource limits in Kubernetes deployments
  • Skipping automated testing in the CI pipeline

Best Practices:

  • Use branch protection rules to prevent untested code from being deployed
  • Implement monitoring tools like Prometheus and Grafana
  • Define rollback strategies in case of failed deployments

Conclusion & Call-to-Action

Setting up a CI/CD pipeline with GitHub Actions and Kubernetes streamlines deployments, improves code quality, and reduces manual intervention. By following this guide, you have built a basic pipeline, automated deployments, and understood best practices.

Want to learn more? Share your thoughts in the comments, ask questions, or try extending this setup with advanced Kubernetes techniques like Helm charts!

Further Reading:

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)