☸ Gophernetes: Deploy or Cry -- K8s + Go and Ingress
Deploy Golang servers with K8s and Ingress.
Intro
Deploying Golang applications with Kubernetes marries the simplicity and efficiency of Go with the powerful orchestration capabilities of Kubernetes, and managing external access with the help of Ingress. This integration not only streamlines the deployment process but also enhances the resilience and scalability of applications.
In this writing, we w'll walk through a simple example of deploying a Go app on K8s. We'll cover how containerization and K8s' features like scaling and self-healing, and Ingress routing work together to keep our apps running smoothly, while ensuring seamless external access.
Desired State
We'll set up three web apps on our K8s cluster. The configuration should ensure that when a client accesses the domain app1.com, the server displays the first one; when he uses app2.com, the second application is server and the same for the third one.
Implementation
1- Building the Application
We setup a basic HTTP server that listens for requests and responds with a simple message.
Creating a lightweight final image which will be pushed to Docker registry and pulled later when deploying the app using the deployment YAML.
# Build stage
FROM golang:1.23.6-alpine AS builder
WORKDIR /app
RUN if [ ! -f go.mod ]; then \
go mod init auto/module; \
fi
RUN go mod tidy
COPY . .
RUN go build -o server .
# Final image stage
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/server .
EXPOSE 8080
CMD ["./server"]
3- Kubernetes Manifest For Deployment
Defining in Deployment.yaml file the way our app runs in Kubernetes, specifying the number of replicas, container image(the one we created in the previous step), and resource settings.
Creating s stable network endpoint for the app using a Service.yaml file, allowing communication between pods and external clients, ensuring traffic is routed correctly to the running application instances.
Managing external access to the application using host-based routing with the use of Ingress.yaml file, which directs requests to the appropriate service based on the domain name.
By following the steps outlined, we'll be able to deploy our apps seamlessly and achieve the desired states, with external access routed correctly and the apps maintaining high availability.