我们前面介绍了很多关于kubernetes 本身的 操作,但是对于如何写一个完整的kubernetes 应用还没有介绍过。在这篇文章中我们将介绍如何一步一步的写一个kubernetesgolang

golang

对于 golang 的安装和配置,我们这里就不详细说明了,因为这也不是我们的重点,我相信这一步你是能够自己独立完成的。

一个令人比较兴奋的事情是现在国内用户访问golang网站可以不用梯子了,我们可以自由的访问https://golang.google.cn/网站了。

新建项目文件夹goappk8s,然后在该目录下面新建一个src目录

$ mkdir goappk8s && cd goappk8s$ mkdir src
$ mkdir goappk8s && cd goappk8s$ mkdir src

在goappk8s目录下新建一个设置GOPATH的脚本:( setup-gopath.sh)

#!/bin/bashDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"export GOPATH="$DIR"
#!/bin/bashDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"export GOPATH="$DIR"

然后我们执行上面的脚本,设置GOPATH为当前工程目录:

$ source setup-gopath.sh$ echo $GOPATH/Users/ych/devs/workspace/yidianzhishi/goappk8s
$ source setup-gopath.sh$ echo $GOPATH/Users/ych/devs/workspace/yidianzhishi/goappk8s

我们可以看到GOPATH已经设置为当前项目根目录了。然后在 src目录下面添加 golang 代码:

$ mkdir -p github.com/cnych/goappk8s && cd github.com/cnych/goappk8s$ touch main.go
$ mkdir -p github.com/cnych/goappk8s && cd github.com/cnych/goappk8s$ touch main.go

我们添加一个最简单的golang服务,提供了一个 ping 接口:

package mainimport ( "github.com/gin-gonic/gin" "net/http")func main() { router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "PONG") }) router.Run(":8080")}
package mainimport ( "github.com/gin-gonic/gin" "net/http")func main() { router := gin.Default() router.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "PONG") }) router.Run(":8080")}

我们可以看到上面的服务中依赖了一个第三方包github.com/gin-gonic/gin,你可以手动将该依赖包下载下来放置到 GOPATH下面,我们这里为了使用govendor来进行管理,当然你可以使用其他的包管理工具,比如:dep、glide 等等。在github.com/cnych/goappk8s目录下面执行下面的操作:

$ govendor init$ govendor fetch github.com/gin-gonic/gin
$ govendor init$ govendor fetch github.com/gin-gonic/gin

注意上面的包需要拉取一些墙外的包。

然后我们切换到项目根目录,也就是GOPATH的路径,执行以下命令:

$ go install github.com/cnych/goappk8s && ./bin/goappk8s[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /ping --> main.main.func1 (3 handlers)[GIN-debug] Listening and serving HTTP on :8080
$ go install github.com/cnych/goappk8s && ./bin/goappk8s[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /ping --> main.main.func1 (3 handlers)[GIN-debug] Listening and serving HTTP on :8080

我们可以看到我们的golang服务已经运行起来了,然后我们浏览器中打开链接http://127.0.0.1:8080/ping,可以看到页面上打印出了 PONG,证明我们的服务已经正常启动了。

Docker

根据上一篇文章 Docker 的多阶段构建我们可以很容易的为上面的 golang服务写一个Dockerfile文件:(与 main.go同目录)

FROM golang AS build-envADD . /go/src/appWORKDIR /go/src/appRUN go get -u -v github.com/kardianos/govendorRUN govendor syncRUN GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-serverFROM alpineRUN apk add -U tzdataRUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeCOPY --from=build-env /go/src/app/app-server /usr/local/bin/app-serverEXPOSE 8080CMD [ "app-server" ]
FROM golang AS build-envADD . /go/src/appWORKDIR /go/src/appRUN go get -u -v github.com/kardianos/govendorRUN govendor syncRUN GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-serverFROM alpineRUN apk add -U tzdataRUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeCOPY --from=build-env /go/src/app/app-server /usr/local/bin/app-serverEXPOSE 8080CMD [ "app-server" ]

然后构建Docker镜像:

$ docker build -t cnych/goappk8s:v1.0.0 ........(省略了)Successfully built 00751f94d8a9Successfully tagged cnych/goappk8s:v1.0.0$ docker push cnych/goappk8s:v1.0.0
$ docker build -t cnych/goappk8s:v1.0.0 ........(省略了)Successfully built 00751f94d8a9Successfully tagged cnych/goappk8s:v1.0.0$ docker push cnych/goappk8s:v1.0.0

上面的操作可以将我们本地的镜像cnych/goappk8s:v1.0.0推送到公共的 dockerhub上面去(当然前提是你得先注册了)。到这里的话其实我们已经可以利用上面的镜像很容易的跑一个容器了,下面我们介绍下如何将服务部署到kubernetes上去。

Kubernetes

当然首先你得有一套可用的kubernetes环境,如果你对这部分还不熟悉的话,建议你可以先看下我们前面的相关文章。 在Dockerfile同目录下面新建文件用于描述 kubernetes的部署方式:(k8s.yaml)

---apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: goapp-deploy namespace: kube-apps labels: k8s-app: goappk8sspec: replicas: 2 revisionHistoryLimit: 10 minReadySeconds: 5 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 template: metadata: labels: k8s-app: goappk8s spec: containers: - image: cnych/goappk8s:v1.0.0 imagePullPolicy: Always name: goappk8s ports: - containerPort: 8080 protocol: TCP resources: limits: cpu: 100m memory: 100Mi requests: cpu: 50m memory: 50Mi livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 10 timeoutSeconds: 3 readinessProbe: httpGet: path: /ping port: 8080 initialDelaySeconds: 10 timeoutSeconds: 2---apiVersion: v1kind: Servicemetadata: name: goapp-svc namespace: kube-apps labels: k8s-app: goappk8sspec: ports: - name: api port: 8080 protocol: TCP targetPort: 8080 selector: k8s-app: goappk8s---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: goapp-ingress namespace: kube-appsspec: rules: - host: goappk8s.local http: paths: - path: / backend: serviceName: goapp-svc servicePort: api
---apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: goapp-deploy namespace: kube-apps labels: k8s-app: goappk8sspec: replicas: 2 revisionHistoryLimit: 10 minReadySeconds: 5 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 template: metadata: labels: k8s-app: goappk8s spec: containers: - image: cnych/goappk8s:v1.0.0 imagePullPolicy: Always name: goappk8s ports: - containerPort: 8080 protocol: TCP resources: limits: cpu: 100m memory: 100Mi requests: cpu: 50m memory: 50Mi livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 10 timeoutSeconds: 3 readinessProbe: httpGet: path: /ping port: 8080 initialDelaySeconds: 10 timeoutSeconds: 2---apiVersion: v1kind: Servicemetadata: name: goapp-svc namespace: kube-apps labels: k8s-app: goappk8sspec: ports: - name: api port: 8080 protocol: TCP targetPort: 8080 selector: k8s-app: goappk8s---apiVersion: extensions/v1beta1kind: Ingressmetadata: name: goapp-ingress namespace: kube-appsspec: rules: - host: goappk8s.local http: paths: - path: / backend: serviceName: goapp-svc servicePort: api

上面的k8s.yaml文件中,我们定义了3类资源: DeploymentServiceIngress,如果对YAML文件还不太了解的同学,可以查看前面的文章 使用YAML 文件创建 Kubernetes Deployment,其中我们设置了replicas: 2,表示我们会运行两个 POD,下面还定义了strategy的滚动策略为 RollingUpdateresources区域定义了我们一个 POD的资源限制,通过livenessProbereadinessProbe设置了健康检查。然后我们用kubectl执行下面的命令:

$ kubectl apply -f k8s.yamldeployment "goapp-deploy" createdservice "goapp-svc" createdingress "goapp-ingress" created
$ kubectl apply -f k8s.yamldeployment "goapp-deploy" createdservice "goapp-svc" createdingress "goapp-ingress" created

我们可以看到上面定义的三种资源都创建成功了。然后查看资源状态:

$ kubectl get deployments -n kube-apps |grep goappgoapp-deploy 2 2 2 2 57s$ kubectl get svc -n kube-apps |grep goappgoapp-svc ClusterIP 10.254.109.69 <none> 8080/TCP 1m$ kubectl get ingress -n kube-apps |grep goappgoapp-ingress goappk8s.local 80 1m$ kubectl get pods -n kube-apps |grep goappgoapp-deploy-84bb6979c-59qkl 1/1 Running 0 2mgoapp-deploy-84bb6979c-mgg2r 1/1 Running 0 2m
$ kubectl get deployments -n kube-apps |grep goappgoapp-deploy 2 2 2 2 57s$ kubectl get svc -n kube-apps |grep goappgoapp-svc ClusterIP 10.254.109.69 <none> 8080/TCP 1m$ kubectl get ingress -n kube-apps |grep goappgoapp-ingress goappk8s.local 80 1m$ kubectl get pods -n kube-apps |grep goappgoapp-deploy-84bb6979c-59qkl 1/1 Running 0 2mgoapp-deploy-84bb6979c-mgg2r 1/1 Running 0 2m

我们可以看到在kubernetes集群中已经有两个 POD在运行了,然后我们可以本地/etc/hosts中定义上面 Ingress中定义的域名:

你的k8s集群节点IP goappk8s.local
你的k8s集群节点IP goappk8s.local

然后我们在浏览器中访问链接http://goappk8s.local/ping,可以看到页面上已经打印出来PONG。



最后整理下代码,将我们的代码提交到github去吧。

最后我们还可以结合CI/CD,增加持续集成功能,做到我们更新代码后,自动更新我们的 kubernetes应用,这部分我们后面再单独进行说明。