一、开发一个测试的docker镜像
我先写一个go的web server项目,这个项目模块名称为mystarter,下面是关键的代码部分
启动类src/main.go
package main import ( "mystarter/src/controller" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() controllers := getControllers() for _, controller := range controllers { controller.Route(router) } router.Run(":8000") } func getControllers() []controller.Controller { controllers := []controller.Controller{} controllers = append(controllers, &controller.SwaggerController{}) controllers = append(controllers, &controller.HomeController{}) return controllers }
在src/controller/home.go中写一个得到当前服务器名称的rest请求
package controller import ( "net/http" "os" "github.com/gin-gonic/gin" ) type HomeController struct { } func (this *HomeController) Route(router *gin.Engine) { router.GET("/hostname", this.hostname) } func (this *HomeController) hostname(ctx *gin.Context) { hostname, _ := os.Hostname() ctx.String(http.StatusOK, hostname+" ") }
src/Dockerfile
#!/bin/bash FROM alpine:3.13.6 #ubuntu:latest MAINTAINER kevin "kevin@hotmail.com" WORKDIR /home/go COPY ./gostarter /home/go COPY ./static /home/go/static COPY ./template /home/go/template ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone CMD ["./gostarter"]
生成镜像的脚本src/dockerbuild.sh
app=gostarter go build -o $app --ldflags "-extldflags -static" docker stop $app docker rm $app docker image rm $app docker build -t $app . docker run --name $app -p 8000:8000 -d $app # # 阿里与云个人镜像:公网 # docker tag gostarter registry.cn-beijing.aliyuncs.com/xufangbo/gostarter:v1.0.0 # docker push registry.cn-beijing.aliyuncs.com/xufangbo/gostarter:v1.0.0
二、开发kubernets的配置文件
编辑文件gostarter.yarml,参加下文
apiVersion: apps/v1 kind: Deployment metadata: name: gostarter-dep spec: selector: matchLabels: app: gostarter replicas: 5 template: metadata: labels: app: gostarter spec: containers: - name: gostarter image: registry.cn-beijing.aliyuncs.com/xufangbo/gostarter:v1.0.0 imagePullPolicy: IfNotPresent ports: - containerPort: 8000 --- apiVersion: v1 kind: Service metadata: name: gostarter-svc spec: selector: app: gostarter ports: - name: "8000" protocol: TCP # service端口,即消费者调用的端口,Service只能在k8s集群内部调用,互联网调用需要使用Ingress port: 8000 # 容器端口 targetPort: 8000
三、发布到kubernetes集群中
1.发布到kubernets集群中
$ kubectl apply -f gostarter.yaml deployment.apps/gostarter-dep created service/gostarter-svc created
2.查看pod
$ kubectl get pods NAME READY STATUS RESTARTS AGE busybox 1/1 Running 0 10h gostarter-dep-df898587f-6rvg5 1/1 Running 0 55s gostarter-dep-df898587f-hq7cz 1/1 Running 0 55s gostarter-dep-df898587f-qjxrj 1/1 Running 0 55s gostarter-dep-df898587f-x54lc 1/1 Running 0 55s gostarter-dep-df898587f-xgbz7 1/1 Running 0 55s
3.查看Servcie
$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE gostarter-svc ClusterIP 10.109.68.204 <none> 8000/TCP 91s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21d
4.使用脚本测试一下
$ for i in `seq 1 10`; do curl http://10.109.68.204:8000/hostname;done gostarter-dep-df898587f-x54lc gostarter-dep-df898587f-hq7cz gostarter-dep-df898587f-qjxrj gostarter-dep-df898587f-6rvg5 gostarter-dep-df898587f-x54lc gostarter-dep-df898587f-6rvg5 gostarter-dep-df898587f-hq7cz gostarter-dep-df898587f-6rvg5 gostarter-dep-df898587f-qjxrj gostarter-dep-df898587f-qjxrj
可以看到每次请求server端的hostname都不是完全相同的,说明集群起作用了
四、测试DNS
ClusterIP方式的IP可以在集群Node上进行访问,但是kubernets的dns不能在集群的node上访问
$ curl http://gostarter-svc:8000/hostname curl: (6) Could not resolve host: gostarter-svc
kubernets的dns可以在pod中进行访问,所以我们要创建一个pod进行访问,busybox就是比较好的方式
$ kubectl run -it --rm --image=busybox /bin/sh # busybox的pod一次创建下次还可以使用,下次进入方式:kubectl exec busybox -it /bin/sh / # rm hostname rm: can't remove 'hostname': No such file or directory / # wget http://10.109.68.204:8000/hostname Connecting to 10.109.68.204:8000 (10.109.68.204:8000) saving to 'hostname' hostname 100% |************************************| 30 0:00:00 ETA 'hostname' saved / # rm hostname / # wget http://gostarter-svc:8000/hostname Connecting to gostarter-svc:8000 (10.109.68.204:8000) saving to 'hostname' hostname 100% |************************************| 30 0:00:00 ETA 'hostname' saved
一切都很完美,只是busybox中不支持curl,wget可读性差一些
第一次使用busybox容器dns解析也不对,又换了infoblox/dnstools镜像就行,而且busybox后来的dns也支持了,没搞明白是什么原因,下面介绍infoblox/dnstools的例子(他也支持curl)
$ kubectl run -it --rm --image=infoblox/dnstools dns-clien If you don't see a command prompt, try pressing enter. dnstools# for i in `seq 1 10`; do curl http://gostarter-svc:8000/hostname;done gostarter-dep-df898587f-x54lc gostarter-dep-df898587f-qjxrj gostarter-dep-df898587f-qjxrj gostarter-dep-df898587f-xgbz7 gostarter-dep-df898587f-x54lc gostarter-dep-df898587f-x54lc gostarter-dep-df898587f-6rvg5 gostarter-dep-df898587f-qjxrj gostarter-dep-df898587f-6rvg5 gostarter-dep-df898587f-xgbz7
至此,本文介绍的Service使用结束