介绍

KubernetesMinikubeKubernetesKubernetesGoDockerMinikuebeKubernetes

应用程序代码

GoHTTP ServerServer"/""/health_check"GoHello World
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() {
	http.HandleFunc("/", index)
	http.HandleFunc("/health_check", check)
	fmt.Println("Server starting...")
	http.ListenAndServe(":3000", nil)
}
复制代码

制作程序镜像

DockerdockerDockerfile

dockerfile

Dockerfile
FROM golang:alpine
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go build -o main . 
CMD ["/app/main"]
复制代码

build 镜像

Dockerfiledocker build
➜  docker build -t go-app-img .
复制代码
dockerDockerfile
➜  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 in 6309f604d662
Removing intermediate container 6309f604d662
 ---> 023baffdcb28
Successfully built 023baffdcb28
Successfully tagged go-app-img:latest
复制代码

验证镜像

docker run
➜ docker run -d -p 3333:3000 --rm --name go-app-container go-app-img
复制代码
dockergo-app-img33333000go-app-container
localhost:3333
图片

推送镜像到DockerHub

DockerHubKubernetesDockerHubDockerHub
➜  docker build -t kevinyan001/kube-go-app .   
...

➜  docker push  kevinyan001/kube-go-app
...
复制代码
DockerfileDockerHub
kevinyan001DockerHub
docker pull kevinyan001/kube-go-app:latest
复制代码

Kubernetes部署应用

yamlKubernetesKubernetesServiceKubernetes

下面我们来一步步递进地执行这三个步骤。

Minikube
minikube start
复制代码

如果你还没有安装可以参照《Minikube-运行在笔记本上的Kubernetes集群》里的安装步骤

定义预期状态

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
复制代码
Kubernetes Deploymentkindreplicas
DeploymentKubernetesKubernetes

部署应用

deployment.yamlDeploymentGo
➜ kubectl create -f deployment.yaml
deployment.apps/my-go-app created

➜ kubectl get deployments
NAME        READY     UP-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          43s
复制代码

暴露应用

DeploymentKubernetesService
➜ 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>        3000:31425/TCP   40h
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          6d13h

➜ minikube ip
192.168.64.4
复制代码
kubectl get svcService31425Kubernetes3000KubernetesNodePortKubernetesGo
192.168.64.4:31425
IndexPage
HealthCheckPage

总结

KubernetesKubernetesDeploymentkubectl.yamlKubernetesapiServerKubernetes