Introduction
In this post, I will guide you through the process of DevOpsifying a Go-based web application. We will cover everything from containerizing the application with Docker to deploying it on a Kubernetes cluster (AWS EKS) using Helm, setting up continuous integration with GitHub Actions, and automating deployments with ArgoCD. By the end of this tutorial, you'll have a fully operational, CI/CD-enabled Go web application.
Prerequisites
Before starting this project, ensure you meet the following prerequisites:
AWS Account: You need an active AWS account to create and manage your EKS cluster for deploying the Go-based application.
DockerHub Account: You should have a DockerHub account to push your Docker images.
Basic DevOps Knowledge: Familiarity with DevOps concepts and practices is essential, including understanding CI/CD pipelines, containerization, orchestration, and cloud deployment.
Helm: Basic knowledge of Helm, the Kubernetes package manager, will be required to package and deploy your application.
By meeting these prerequisites, you'll be well-prepared to follow the steps in this guide and successfully DevOpsify your Go-based application!
Step 1: Getting the Source Code
To get started with the project, you'll need to clone the source code from the GitHub repository. Use the following command to clone the project:
git clone https://github.com/iam-veeramalla/go-web-app-devops.git
This repository contains all the necessary files and configurations to set up and deploy the Go-based application using the DevOps practices described in this guide. Once cloned, you can navigate through the steps below and follow them to containerize, deploy, and manage the application.
Step 2: Containerizing the Go Web Application
The first step is to containerize our Go application. We will use a multistage Dockerfile to build the Go application and create a lightweight production-ready image.
FROM golang:1.22.5 as build
WORKDIR /app
COPY go.mod .
RUN go mod download
COPY . .
RUN go build -o main .
FROM gcr.io/distroless/base
WORKDIR /app
COPY --from=build /app/main .
COPY --from=build /app/static ./static
EXPOSE 8080
CMD ["./main"]
Commands to Build and Push Docker Image:
docker login
docker build . -t go-web-app
docker push go-web-app:latest
In this Dockerfile, the first stage uses the Golang image to build the application. The second stage uses a distroless base image, which is much smaller and more secure, containing only the necessary files to run our Go application.
Step 3: Deploying on Kubernetes with AWS EKS
Next, we will deploy our containerized application to a Kubernetes cluster. Here’s how you can set up your cluster and deploy your app.
Create an EKS Cluster:
eksctl create cluster --name demo-cluster --region us-east-1
Deployment Configuration (deployment.yaml):
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-web-app
labels:
app: go-web-app
spec:
replicas: 1
selector:
matchLabels:
app: go-web-app
template:
metadata:
labels:
app: go-web-app
spec:
containers:
- name: go-web-app
image: iamamash/go-web-app:latest
ports:
- containerPort: 8080
Service Configuration (service.yaml):
apiVersion: v1
kind: Service
metadata:
name: go-web-app
labels:
app: go-web-app
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
selector:
app: go-web-app
type: ClusterIP
Ingress Configuration (ingress.yaml):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: go-web-app
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: go-web-app.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: go-web-app
port:
number: 80
Apply the configurations using kubectl:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
Setting Up Nginx Ingress Controller:
An Ingress controller in Kubernetes manages external access to services within the cluster, typically handling HTTP and HTTPS traffic. It provides centralized routing, allowing you to define rules for how traffic should reach your services. In this project, we use the Nginx Ingress controller to efficiently manage and route traffic to our Go-based application deployed in the Kubernetes cluster.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.1/deploy/static/provider/aws/deploy.yaml
Step 4: Packaging with Helm
To manage our Kubernetes resources more effectively, we package our application using Helm, a package manager for Kubernetes.
Create a Helm Chart:
helm create go-web-app-chart
After creating the chart, replace everything inside the templates directory with your deployment.yaml
, service.yaml
, and ingress.yaml
files.
Update values.yaml
: The values.yaml
file will contain dynamic values, like the Docker image tag. This tag will be updated automatically based on the GitHub Actions run ID, ensuring each deployment is unique.
# Default values for go-web-app-chart.
replicaCount: 1
image:
repository: iamamash/Go-Web-App
pullPolicy: IfNotPresent
tag: "10620920515" # Will be updated by CI/CD pipeline
ingress:
enabled: false
className: ""
annotations: {}
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
Helm Deployment:
kubectl delete -f k8s/.
helm install go-web-app helm/go-web-app-chart
kubectl get all
Step 5: Continuous Integration with GitHub Actions
To automate the build and deployment of our application, we set up a CI/CD pipeline using GitHub Actions.
GitHub Actions Workflow (.github/workflows/cicd.yaml):
name: CI/CD
on:
push:
branches:
- main
paths-ignore:
- 'helm/**'
- 'README.md'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go 1.22
uses: actions/setup-go@v2
with:
go-version: 1.22
- name: Build
run: go build -o go-web-app
- name: Test
run: go test ./...
push:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and Push action
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/go-web-app:${{github.run_id}}
update-newtag-in-helm-chart:
runs-on: ubuntu-latest
needs: push
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.TOKEN }}
- name: Update tag in Helm chart
run: |
sed -i 's/tag: .*/tag: "${{github.run_id}}"/' helm/go-web-app-chart/values.yaml
- name: Commit and push changes
run: |
git config --global user.email "ansari2002ksp@gmail.com"
git config --global user.name "Amash Ansari"
git add helm/go-web-app-chart/values.yaml
git commit -m "Updated tag in Helm chart"
git push
To securely store sensitive information like DockerHub credentials and Personal Access Tokens (PAT) in GitHub, you can use GitHub Secrets. To create a secret, navigate to your repository on GitHub, go to Settings > Secrets and variables > Actions > New repository secret. Here, you can add secrets like DOCKERHUB_USERNAME
, DOCKERHUB_TOKEN
, and TOKEN
. Once added, these secrets can be accessed in your GitHub Actions workflows using ${{ secrets.SECRET_NAME }}
syntax, ensuring that your sensitive data is securely managed during the CI/CD process.
Step 6: Continuous Deployment with ArgoCD
Finally, we implement continuous deployment using ArgoCD to automatically deploy the application whenever changes are pushed.
Install ArgoCD:
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
kubectl get svc argocd-server -n argocd
Setup ArgoCD Project: To access the ArgoCD UI after setting it up, you first need to determine the external IP of the node where ArgoCD is running. You can obtain this by running the command:
kubectl get nodes -o wide
Next, get the port number at which the ArgoCD server is running using:
kubectl get svc argocd-server -n argocd
Once you have the external IP and port number, you can access the ArgoCD UI by navigating to http://<node-external-IP>:<port>
. For example, if the external IP is 54.161.25.151
and the port number is 30498
, the URL to access ArgoCD UI would be http://54.161.25.151:30498
.
To log in to the ArgoCD UI for the first time, use the default username admin. The password can be retrieved from the ArgoCD secrets using:
kubectl edit secret argocd-initial-admin-secret -n argocd
Copy the encoded password from the data.password
field and decode it using base64
:
echo <encoded-password> | base64 --decode
For example, if the encoded password is kjasdfbSNLnlkaW==
, decoding it with:
echo kjasdfbSNLnlkaW== | base64 --decode
will provide the actual password. Be sure to exclude any trailing % symbol from the decoded output when using the password to log in.
Now, after accessing the ArgoCD UI, since both ArgoCD and the application are in the same cluster, you can create a project. To do this, click on the "New App" button and fill in the required fields, such as:
- App Name: Provide a name for your application.
- Sync Policy: Choose between manual or automatic synchronization.
- Self-Heal: Enable this option if you want ArgoCD to automatically fix any drift.
- Source Path: Enter the GitHub repository URL where your application code resides.
- Helm Chart Path: Specify the path to the Helm chart within your repository.
- Destination: Set the Cluster URL and namespace where you want the application deployed.
- Helm Values: Select the appropriate values.yaml file for your Helm chart.
After filling in these details, click on "Create" and wait for ArgoCD to create the project. ArgoCD will pick up the Helm chart and deploy the application to the Kubernetes cluster for you. You can verify the deployment using:
kubectl get all
That's all you need to do!
Conclusion
Congratulations! You have successfully DevOpsified your Go web application. This end-to-end guide covered containerizing your application with Docker, deploying it with Kubernetes and Helm, automating builds with GitHub Actions, and setting up continuous deployments with ArgoCD. You are now ready to manage your Go application with full CI/CD capabilities.
Feel free to leave your comments and feedback below! Happy DevOpsifying!
Reference
For a detailed video guide on deploying Go applications on AWS EKS, check out this video.
Top comments (0)