introduce

The previous articles provided a conceptual overview of what Kubernetes is and what its internal architecture looks like. Minikube has also been installed on the computer – having a single node Kubernetes cluster allows us to start playing Kubernetes on our own computer. In today’s article, I’m going to take a step by step attempt to make a Docker image of the Go application and deploy it to run on Minikuebe. Today’s article does not need any foundation, Kubernetes novice friends first get on the bus to learn.

Application code

Server listening on port 3000 contains two routes: “/” and “/health_check”. Today’s article will not focus on how to use Go to develop programs, so it is a Hello World level code, no more explanation, directly look at the code.

package main

import (
	"fmt"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<h1>Hello World</h1>")}func check(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<h1>Health check</h1>")}func main(a) {
	http.HandleFunc("/", index)
	http.HandleFunc("/health_check", check)
	fmt.Println("Server starting...")
	http.ListenAndServe(": 3000".nil)}Copy the code

Make program image

Next, start making the Docker image that contains the application code. About the use of docker and how to write Dockerfile, you can reply to the keyword docker in the public account to get a complete reference notes.

dockerfile

Add a file named Dockerfile to the root directory of the application and add the following command to the file:

FROM golang:alpine
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go build -o main . 
CMD ["/app/main"]
Copy the code

Build the mirror

Run the Docker build image in the directory where the Dockerfile resides

➜ docker build-t go-app-img.Copy the code

Docker will build the image according to the instructions in Dockerfile, and the whole construction process is similar to the following:

➜ docker build-t go-app-img.sending build context to docker daemon 9.216kB Step 1/6: FROM golang:alpine alpine: Pulling from library/golang df20fa9351a1: Pull complete ed8968b2872e: Pull complete a92cc7c5fd73: Pull complete 9e0cccf56431: Pull complete cbe0275821fc: Pull complete 
Digest: sha256:6042b9cfb4eb303f3bdcbfeaba79b45130d170939318de85ac5b9508cb6f0f7e
Status: Downloaded newer image for golang:alpine
 ---> 3289bf11c284
Step 2/6 : RUN mkdir /app
 ---> Running in b34dccb1f3de
Removing intermediate container b34dccb1f3de
 ---> 1fa1a1c21aa2
Step 3/6 : COPY . /app
 ---> 815660da9d1a
Step 4/6 : WORKDIR /app
 ---> Running in 49dc25fe6bb7
Removing intermediate container 49dc25fe6bb7
 ---> 14776702ccf7
Step 5/6 : RUN go build -o main .
 ---> Running in 3bd4dc1e2bf6
Removing intermediate container 3bd4dc1e2bf6
 ---> 59aa7f96ee42
Step 6/6 : CMD ["/app/main"]
 ---> Running in6309f604d662 Removing intermediate container 6309f604d662 ---> 023baffdcb28 Successfully built 023baffdcb28 Successfully  tagged go-app-img:latestCopy the code

Verify the image

This step can be omitted, but to make sure the image is correct, let’s run the container with the image using the docker run command.

➜ docker run -d -p 3333:3000 --rm --name go-app-container go-app-img
Copy the code

Here, we instruct Docker to run the container from the image Go-app-img, bind host port 3333 to the container’s internal port 3000, run the container in background mode (-d), name the container Go-app-Container, and automatically delete the container when the container finishes running (–rm).

Open your browser and type localhost:3333.

Push images to DockerHub

Kubernetes will pull the image from DockerHub according to the name of the image (the image source is configurable, not necessarily DockerHub, can be a private image repository).

➜ docker build-t Kevinyan001 /kube-go-app.... ➜ Docker push Kevinyan001 /kube-go-app...Copy the code

Rebuild the image with Dockerfile, specifying the image repository name. After the build is complete, the image is then pushed to the DockerHub.

Kevinyan001 in the warehouse name above is my own DockerHub account, you can directly use the following command to pull my image to use, but it is still suggested that everyone make their own image.

docker pull kevinyan001/kube-go-app:latest
Copy the code

Kubernetes deploys applications

To deploy an application, you need to define the expected state, which is to declare various expected states of specific Kubernetes objects in a YAML file. Then let Kubernetes create the object, which will always drive the current state of the cluster to the expected state (for example, if a node fails, a new node will be created to replace the failed node). After deploying the application, we also need to expose the application externally via Service so that we can access the application running in the Kubernetes cluster.

Let’s do these three steps step by step.

Before we start we need to start Minikube

minikube start
Copy the code

If you don’t already have it installed, see the installation steps in “Minikube- Kubernetes Cluster running on A Laptop”

Define the expected state

Define the expected state in the deployment manifest file (deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-go-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-app
  template:
    metadata:
      labels:
        app: go-app
    spec:
      containers:
        - name: go-app-container
          image: kevinyan001/kube-go-app
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
            - containerPort: 3000
Copy the code

The Kubernetes Deployment object (specified in KIND of the manifest file) represents the application running in the cluster. The file also specifies that the application needs a replicas to run, as well as the name of the running container, its image and resource size.

Deployment is one of the Kubernetes objects, and there are many other objects that correspond to different types of resources in Kubernetes.

The deployment of application

Create a Deployment object to run the container for the Go application using the deployment.yaml defined above:

➜ kubectl create -fDeployment.yaml deployment.apps/my-go-app created ➜ kubectl get deployments NAME READY to-date AVAILABLE AGE My-go-app 1/1 1 1 24s ➜ kubectl get Pods NAME READY STATUS RESTARTS AGE my-go-app-5bb8767F6D-2PDTK 1/1 Running 0 43sCopy the code

To expose the application

After the application is deployed, it is not directly accessible from the outside, and the application that the Deployment object just ran needs to be exposed as a Service of Kubernetes.

➜ kubectl expose Deployment my-go-app --type=NodePort --name= Go-app-svc --target-port=3000 service/ Go-app-svC exposed ➜ Kubectl get SVC NAME TYPE cluster-ip external-ip PORT(S) AGE go-app-svc NodePort 10.104.190.231 < None > 320:31425 /TCP 40h kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 6d13h ➜ minikube IP 192.168.64.4Copy the code

Kubectl get SVC Service: port 31425 of the host is mapped to port 3000 of the Kubernetes container where the application is running. Go applications deployed by Kubernetes can be accessed by using the Kubernetes cluster IP and NodePort in the browser.

The result of accessing the two routes defined by the application program through 192.168.64.4:31425 is as follows:

conclusion

This article briefly summarizes the steps of deploying an application to a Kubernetes cluster. There are many kinds of objects in Kubernetes to represent various resources within the cluster. Today’s Deployment is one of them. Kubectl requests Kubernetes’ apiServer to create various objects based on the configuration information in the. Yaml file. We will continue to explore these commonly used Kubernetes objects. As I have just started to learn, it is inevitable that there is an inaccurate expression, please forgive me.