Table of Contents
Setting up a continuous integration and continuous deployment (CI/CD) pipeline is essential for modern software development. This tutorial guides you through creating a robust Go CI/CD pipeline using Docker and Kubernetes, enabling automated testing, building, and deployment of your applications.
Prerequisites
- Basic knowledge of Go programming language
- Docker installed on your machine
- Kubernetes cluster (local or cloud-based)
- kubectl configured to access your cluster
- GitHub account for repository hosting
- CI/CD tool (e.g., GitHub Actions, Jenkins, GitLab CI)
Step 1: Prepare Your Go Application
Start with a simple Go application. Create a directory for your project and initialize a new Go module.
Example command:
go mod init github.com/yourusername/yourapp
Write your main.go file:
package main
import "fmt"
func main() {
fmt.Println("Hello, CI/CD with Go, Docker, and Kubernetes!")
}
Step 2: Containerize Your Application with Docker
Create a Dockerfile in your project directory:
FROM golang:1.20-alpine
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o main .
EXPOSE 8080
CMD ["./main"]
Build your Docker image:
docker build -t yourusername/yourapp:latest .
Test your container locally:
docker run -p 8080:8080 yourusername/yourapp:latest
Step 3: Push Your Image to Docker Hub
Login to Docker Hub:
docker login
Push your image:
docker push yourusername/yourapp:latest
Step 4: Set Up Kubernetes Deployment
Create a deployment.yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: yourusername/yourapp:latest
ports:
- containerPort: 8080
Create a service.yaml file:
apiVersion: v1
kind: Service
metadata:
name: go-app-service
spec:
type: LoadBalancer
selector:
app: go-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Apply the configurations:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Step 5: Automate with CI/CD Pipeline
Configure your CI/CD tool to automate the process. For example, in GitHub Actions, create a workflow file:
.github/workflows/ci-cd.yml
Sample workflow:
name: Go CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.20
- name: Build
run: go build -v ./...
- name: Docker Build
run: docker build -t yourusername/yourapp:latest .
- name: Docker Login
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
- name: Push Docker Image
run: docker push yourusername/yourapp:latest
- name: Deploy to Kubernetes
uses: appleboy/scp-action@master
with:
host: ${{ secrets.K8S_HOST }}
username: ${{ secrets.K8S_USER }}
key: ${{ secrets.K8S_SSH_KEY }}
script: |
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Conclusion
By following this tutorial, you set up an automated CI/CD pipeline for your Go application using Docker and Kubernetes. This setup ensures rapid deployment cycles, consistent environments, and scalable infrastructure for your software projects.