Automating Deployment with CI/CD in Golang & Kubernetes
KodeNimbus Team • Golang

Automating Deployment with CI/CD in Golang & Kubernetes

November 17, 2025

In today’s fast-paced development landscape, automation is the key to success. Continuous Integration (CI) and Continuous Deployment (CD) are vital practices that enable teams to build, test, and deploy applications in an automated and efficient manner. This blog will focus on how to set up a CI/CD pipeline for deploying Golang applications to a Kubernetes cluster, ensuring faster, more reliable deployments and streamlined operations.

CI/CD helps developers and DevOps teams ensure that new code is always in a deployable state, and it allows them to push out bug fixes and features more quickly. By combining Golang, a fast and efficient language, with Kubernetes, a powerful container orchestration tool, you can scale your applications effortlessly and automate the entire deployment process.


What is CI/CD?

Before we dive into the implementation of CI/CD for Golang and Kubernetes, let's break down the two practices:


  1. Continuous Integration (CI):

    • CI involves regularly merging code changes into a shared repository, typically multiple times a day.

    • It ensures that every change triggers an automated build and test, catching integration issues early and preventing bugs from creeping into the main codebase.

    • The primary objective is to catch issues earlier in the development lifecycle and ensure a stable codebase.


  2. Continuous Deployment (CD):

    • Continuous Deployment extends CI by automating the deployment of applications to production.

    • In a CD pipeline, after the code passes through the CI process (build and test), it is automatically deployed to production.

    • This helps teams release features and fixes to production in real-time, ensuring that any change that passes tests is immediately available to users.

Together, CI/CD enables faster release cycles, more stable codebases, and greater overall efficiency in managing application deployments.



Why Use Golang and Kubernetes for CI/CD?

When paired together, Golang and Kubernetes make an excellent combination for cloud-native application development and deployment.
Golang:
Performance: Golang is a statically typed, compiled language, ideal for building high-performance, concurrent applications. Its goroutines and channels make it highly effective for building scalable applications that can handle high concurrency, a crucial factor in modern backend systems.
Efficiency: Golang is known for its simplicity and speed. It compiles directly to machine code, making it a powerful tool for creating efficient services and microservices.
Kubernetes:
Scalability: Kubernetes helps in managing and orchestrating containers, making it easier to scale applications dynamically based on traffic and demand.
Resilience: Kubernetes ensures high availability by automatically managing container health, restarting failed containers, and balancing workloads.
Automation: Kubernetes supports declarative configuration, enabling automatic rollouts, updates, and scaling of applications.

Together, Kubernetes manages the deployment and scaling of containers, while Golang serves as an efficient language to build the backend services.



CI/CD Pipeline Setup for Golang and Kubernetes

Now that we understand the basics, let’s move to the implementation of the CI/CD pipeline for a Golang application deployed to Kubernetes. We will use GitHub Actions for CI and Kubernetes for managing the deployment.


Prerequisites:

  1. Golang Application: A simple Golang application (we’ll build one for this blog).

  2. Docker: For containerizing the Golang application.

  3. Kubernetes Cluster: A running Kubernetes cluster (either locally using Minikube or on a cloud provider).

  4. GitHub Repository: Code stored in a GitHub repository, which is integrated with GitHub Actions.

Let’s begin with setting up a simple Golang application.



Step 1: Building a Simple Golang Application


We’ll start with a basic Golang HTTP server that serves a “Hello, World!” message. Create a new file called main.go in your Golang project directory.

// main.go package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World! This is a Golang app on Kubernetes!") } func main() { http.HandleFunc("/", handler) log.Println("Starting server on :8080...") log.Fatal(http.ListenAndServe(":8080", nil)) }

This is a simple HTTP server that listens on port 8080 and responds with "Hello, World!" when accessed.


Step 2: Dockerizing the Golang Application


For Kubernetes to run this application, we need to containerize it using Docker. Here’s how you can create a Dockerfile for the Golang application:


# Step 1: Build the Golang application FROM golang:1.18-alpine AS builder WORKDIR /app COPY . . RUN go mod tidy RUN go build -o app . # Step 2: Create the final image to run the application FROM alpine:latest WORKDIR /root/ COPY --from=builder /app/app . EXPOSE 8080 CMD ["./app"]

This Dockerfile has two stages:

  1. The builder stage uses the official Golang image to compile the application.

  2. The final image uses Alpine Linux (a lightweight image) and copies the compiled Go application from the builder image.


Now, build the Docker image by running:

docker build -t golang-k8s-app .


You can test the Docker container locally by running:

docker run -p 8080:8080 golang-k8s-app


Your app should now be accessible on localhost:8080.



Step 3: Setting Up GitHub Actions for CI/CD


With the application containerized, let’s set up GitHub Actions for CI/CD. Create a file named .github/workflows/ci-cd.yml in your repository with the following content:

name: CI/CD Pipeline for Golang & Kubernetes on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: # Checkout code - name: Checkout code uses: actions/checkout@v2 # Set up Docker - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 # Log in to Docker Hub - name: Log in to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} # Build and push the Docker image - name: Build and push Docker image run: | docker build -t ${{ secrets.DOCKER_USERNAME }}/golang-k8s-app:latest . docker push ${{ secrets.DOCKER_USERNAME }}/golang-k8s-app:latest # Set up Kubernetes Kubeconfig - name: Set up Kubeconfig uses: azure/setup-kubeconfig@v1 with: kubeconfig: ${{ secrets.KUBE_CONFIG }} # Deploy to Kubernetes - name: Deploy to Kubernetes run: | kubectl set image deployment/golang-k8s-app golang-k8s-app=${{ secrets.DOCKER_USERNAME }}/golang-k8s-app:latest


Explanation:

  • Checkout code: This step checks out the code from the GitHub repository.

  • Set up Docker: Sets up Docker Buildx, which is needed to build images on multiple platforms.

  • Log in to Docker Hub: Securely logs in to Docker Hub using GitHub Secrets for username and password.

  • Build and push Docker image: Builds the Docker image from the Dockerfile and pushes it to Docker Hub.

  • Set up Kubeconfig: Authenticates the GitHub Actions workflow to access your Kubernetes cluster using the Kubeconfig.

  • Deploy to Kubernetes: This command updates the running application in Kubernetes by setting the new image from Docker Hub.


Step 4: Kubernetes Deployment Configuration

To deploy the Golang application on Kubernetes, we need a Kubernetes deployment YAML file. Create a file named deployment.yaml in your repository:


apiVersion: apps/v1 kind: Deployment metadata: name: golang-k8s-app spec: replicas: 3 selector: matchLabels: app: golang-k8s-app template: metadata: labels: app: golang-k8s-app spec: containers: - name: golang-k8s-app image: ${DOCKER_USERNAME}/golang-k8s-app:latest ports: - containerPort: 8080 --- apiVersion: v1 kind: Service metadata: name: golang-k8s-app-service spec: selector: app: golang-k8s-app ports: - port: 80 targetPort: 8080 type: LoadBalancer


This configuration does two things:

  1. It defines a Deployment with three replicas for the Golang app.

  2. It exposes the application via a LoadBalancer service, making it accessible externally.


Step 5: Automating Deployment to Kubernetes

Now that everything is set up, pushing a change to the main branch of your repository will automatically trigger the CI/CD pipeline, build the Docker image, push it to Docker Hub, and deploy it to your Kubernetes cluster.



Lastly..

By integrating Golang and Kubernetes with a CI/CD pipeline, we’ve built a robust system for automating application deployments. This approach allows you to:


Automate the build, test, and deployment process.
Scale your application efficiently using Kubernetes.
Deploy code changes faster and more reliably.


This pipeline reduces human intervention, minimizes deployment errors, and increases the speed of delivering new features to production. The combination of Golang’s performance and Kubernetes’s orchestration ensures that your applications remain efficient, scalable, and resilient.

Interested in integrating golang with kubernetes?
check out my blog