Skip to main content

Kubernetes Example

Github StackBlitz

This project demonstrates how to deploy a Go application using the Fiber framework on a Kubernetes cluster.

Prerequisites

Ensure you have the following installed:

  • Golang
  • Fiber package
  • Docker
  • Kubernetes
  • kubectl
  • Minikube (for local development)

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/k8s
  2. Install dependencies:

    go get
  3. Build the Docker image:

    docker build -t fiber-k8s-example .
  4. Start Minikube (if using Minikube):

    minikube start
  5. Deploy the application to Kubernetes:

    kubectl apply -f deployment.yaml

Running the Application

  1. Check the status of the pods:

    kubectl get pods
  2. Forward the port to access the application:

    kubectl port-forward svc/fiber-k8s-example 3000:3000
  3. 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

References