Kubernetes Example
This project demonstrates how to deploy a Go application using the Fiber framework on a Kubernetes cluster.
Prerequisites
Ensure you have the following installed:
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.git
cd recipes/k8s -
Install dependencies:
go get
-
Build the Docker image:
docker build -t fiber-k8s-example .
-
Start Minikube (if using Minikube):
minikube start
-
Deploy the application to Kubernetes:
kubectl apply -f deployment.yaml
Running the Application
-
Check the status of the pods:
kubectl get pods
-
Forward the port to access the application:
kubectl port-forward svc/fiber-k8s-example 3000:3000
-
Access the application at
http://localhost:3000
.
Example
Here is an example main.go
file for the Fiber application:
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, Kubernetes!")
})
log.Fatal(app.Listen(":3000"))
}
Here is an example Dockerfile
for the application:
FROM golang:1.20-alpine
WORKDIR /app
COPY go.mod ./
COPY go.sum ./
RUN go mod download
COPY *.go ./
RUN go build -o /fiber-k8s-example
EXPOSE 3000
CMD ["/fiber-k8s-example"]
Here is an example deployment.yaml
file for deploying the application to Kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: fiber-k8s-example
spec:
replicas: 2
selector:
matchLabels:
app: fiber-k8s-example
template:
metadata:
labels:
app: fiber-k8s-example
spec:
containers:
- name: fiber-k8s-example
image: fiber-k8s-example:latest
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: fiber-k8s-example
spec:
type: NodePort
selector:
app: fiber-k8s-example
ports:
- protocol: TCP
port: 3000
targetPort: 3000
nodePort: 30001