Write a Gin application running on the Kubernetes cluster from scratch

Posted by nileshkulkarni on Sun, 16 Jan 2022 20:02:52 +0100

Experimental premise

  • You need to have a macOS development environment. This article takes this as an example. Please build other types of development environments by yourself.
  • You need to know something about YAML, a language specially used to write configuration files.
  • You need to have some basic knowledge of Docker.
  • You need to understand some core basic concepts in Kubernetes, such as Node, Pod, ReplicaSet, Deployment, Service, progress, ConfigMap, etc.

YAML profile download address:

git clone https://github.com/jxlwqq/kubernetes-examples.git
cd deploying-simple-hello-gin-app

Installing Docker for Mac

Download address: hub.docker.com/editions/community/...

Start and open the Kubernetes function. During the function opening process, Docker will automatically pull the Kubernetes related image, so you need to use a special posture to surf the Internet throughout the process.

Why not use minicube? Minicube + VirtualBox + kubectl is too cumbersome to install, and even if you use a special posture to surf the Internet, you may not be able to do it. Of course, Alibaba cloud provided an article Installation tutorial Can refer to.

Local port preparation

Please ensure that port 80 of the local localhost is not occupied. If it is already in use, please temporarily shut down the service occupying port 80 during the experiment.

Switch cluster

If you have multiple Kubernetes cluster configurations locally, please switch to the cluster named docker desktop first:

kubectl config use-context docker-desktop

source code

A simple Gin application looks like this:

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()

    r.GET("/hello", func(c *gin.Context) {
        c.String(200, "Hello, Gin!")

    _ = r.Run(":8080")

go. The mod file contains the dependencies required by the application:

module hello-gin

go 1.16

require github.com/gin-gonic/gin v1.7.2 // indirect

Docker image

The Dockerfile applied is as follows:

# Multi phase construction: improve the construction speed and reduce the image size

# Obtain the Go basic image of 1.16 from the official warehouse
FROM golang:1.16-alpine AS builder
# Set working directory
WORKDIR /go/src/hello-gin
# Copy project files
ADD . /go/src/hello-gin
# Download dependency
RUN go get -d -v ./...
# Build a binary named "app"
RUN go build -o app .

# Get a lightweight Linux distribution with a size of only about 5M
FROM alpine:latest
# Copy the binaries built in the previous stage to this stage
COPY --from=builder /go/src/hello-gin/app .
# Set listening port
# Configure startup command
CMD ["./app"]

Build and commit images:

jxlwqq is my Docker Hub account, which needs to be changed to your own account. If you don't have an account, you need to register first: hub.docker.com/signup

If you want to skip this step, you can directly pull the image I made: docker pull jxlwqq / Hello gin: latest

docker build -f Dockerfile -t jxlwqq/hello-gin:latest . # Build mirror
docker login # Sign in
docker push jxlwqq/hello-gin:latest # Commit mirror

Prerequisite: deploy nginx ingress

In order for the Ingress resource to work, the cluster must have an Ingress controller running. Kubernetes officials currently support and maintain GCE and nginx controllers.

Here we select the ingress nginx controller:

kubectl apply -f ../ingress-nginx/deploy.yaml

Note: deploy Yaml file content comes from: github.com/kubernetes/ingress-ngin...

For detailed operation instructions, see: github.com/kubernetes/ingress-ngin...

Deploy hello gin application

Execute the following command:

kubectl apply -f hello-gin-deployment-and-service.yaml
kubectl apply -f ingress.yaml


service/hello-gin-svc created
deployment.apps/hello-gin created
ingress.networking.k8s.io/hello-gin-ingress created

hello-gin-deployment-and-service.yaml file interpretation:

apiVersion: apps/v1 # api version
kind: Deployment # Resource object type
metadata: # Deployment metadata
  name: hello-gin # Object name
spec: # Object specification
  selector: # Selector to select a Pod with the following labels
    matchLabels: # Label matching
      app: hello-gin # Tag KeyValue
  template: # Pod template
    metadata: # Pod metadata
      labels: # Pod tag
        app: hello-gin # Pod tag, which is the same as the above deployment The label in the selector corresponds to
    spec: # Pod object specification
      containers: # container
        - name: hello-gin # Container name
          image: jxlwqq/hello-gin:latest # Image name: image version
          resources: # Resource constraints
            limits: # It is simply understood as max resource value
              memory: "128Mi"
              cpu: "500m"
            requests: # It is simply understood as min resource value
              memory: "128Mi"
              cpu: "500m"
          ports: # port
            - containerPort: 8080 # Port number
apiVersion: v1 # api version
kind: Service # object type
metadata: # metadata
  name: hello-gin-svc # Object name
spec: # Statute
  selector: # selector
    app: hello-gin # Label selector, corresponding to the label of Pod
    - port: 8080 # Service port number
      targetPort: 8080 # Port number of Pod exposure

ingress.yaml file interpretation:

apiVersion: networking.k8s.io/v1 # api version
kind: Ingress # object type
metadata: # metadata
  name: hello-gin-ingress # Corresponding name
spec: # Statute
  rules: # rule
    - http:
        paths: # route
          - path: /
            pathType: Prefix
            backend: # Back end service
              service: # service
                name: hello-gin-svc # service name
                  number: 8080 # Port number

Access verification:

curl # Back to Hello, Gin!


kubectl delete -k .

Topics: Go Kubernetes gin