其他命令

  • goctl docker
  • goctl kube

goctl docker

goctl docker 可以极速生成一个 Dockerfile,帮助开发/运维人员加快部署节奏,降低部署复杂度。

准备工作

  • docker安装

Dockerfile 额外注意点

  • 选择最简单的镜像:比如alpine,整个镜像5M左右
  • 设置镜像时区
    1. RUN apk add --no-cache tzdata
    2. ENV TZ Asia/Shanghai

多阶段构建

  • 第一阶段构建否则构建出可执行文件,确保构建过程独立于宿主机
  • 第二阶段将第一阶段的输出作为输入,构建出最终的极简镜像

Dockerfile编写过程

  • 首先安装 goctl 工具

    1. $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl
  • 在 greet 项目下创建一个 hello 服务

    1. $ goctl api new hello

文件结构如下:

  1. greet
  2. ├── go.mod
  3. ├── go.sum
  4. └── service
  5. └── hello
  6. ├── Dockerfile
  7. ├── etc
  8. └── hello-api.yaml
  9. ├── hello.api
  10. ├── hello.go
  11. └── internal
  12. ├── config
  13. └── config.go
  14. ├── handler
  15. ├── hellohandler.go
  16. └── routes.go
  17. ├── logic
  18. └── hellologic.go
  19. ├── svc
  20. └── servicecontext.go
  21. └── types
  22. └── types.go
  • hello 目录下一键生成 Dockerfile
    1. $ goctl docker -go hello.go
    Dockerfile 内容如下:
    1. FROM golang:alpine AS builder
    2. LABEL stage=gobuilder
    3. ENV CGO_ENABLED 0
    4. ENV GOOS linux
    5. ENV GOPROXY https://goproxy.cn,direct
    6. WORKDIR /build/zero
    7. ADD go.mod .
    8. ADD go.sum .
    9. RUN go mod download
    10. COPY . .
    11. COPY service/hello/etc /app/etc
    12. RUN go build -ldflags="-s -w" -o /app/hello service/hello/hello.go
    13. FROM alpine
    14. RUN apk update --no-cache
    15. RUN apk add --no-cache ca-certificates
    16. RUN apk add --no-cache tzdata
    17. ENV TZ Asia/Shanghai
    18. WORKDIR /app
    19. COPY --from=builder /app/hello /app/hello
    20. COPY --from=builder /app/etc /app/etc
    21. CMD ["./hello", "-f", "etc/hello-api.yaml"]
  • hello 目录下 build 镜像

    1. $ docker build -t hello:v1 -f service/hello/Dockerfile .
  • 查看镜像

    1. hello v1 5455f2eaea6b 7 minutes ago 18.1MB

可以看出镜像大小约为18M。

  • 启动服务
    1. $ docker run --rm -it -p 8888:8888 hello:v1
  • 测试服务
    1. $ curl -i http://localhost:8888/from/you
    1. HTTP/1.1 200 OK
    2. Content-Type: application/json
    3. Date: Thu, 10 Dec 2020 06:03:02 GMT
    4. Content-Length: 14
    5. {"message":""}

goctl docker总结

goctl 工具极大简化了 Dockerfile 文件的编写,提供了开箱即用的最佳实践,并且支持了模板自定义。

goctl kube

goctl kube提供了快速生成一个 k8s 部署文件的功能,可以加快开发/运维人员的部署进度,减少部署复杂度。

头疼编写 K8S 部署文件?

  • K8S yaml 参数很多,需要边写边查?
  • 保留回滚版本数怎么设?
  • 如何探测启动成功,如何探活?
  • 如何分配和限制资源?
  • 如何设置时区?否则打印日志是 GMT 标准时间
  • 如何暴露服务供其它服务调用?
  • 如何根据 CPU 和内存使用率来配置水平伸缩?

首先,你需要知道有这些知识点,其次要把这些知识点都搞明白也不容易,再次,每次编写依然容易出错!

创建服务镜像

为了演示,这里我们以 redis:6-alpine 镜像为例。

完整 K8S 部署文件编写过程

  • 首先安装 goctl 工具
  1. $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl
  • 一键生成 K8S 部署文件
  1. $ goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379

生成的 yaml 文件如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: redis
  5. namespace: adhoc
  6. labels:
  7. app: redis
  8. spec:
  9. replicas: 3
  10. revisionHistoryLimit: 5
  11. selector:
  12. matchLabels:
  13. app: redis
  14. template:
  15. metadata:
  16. labels:
  17. app: redis
  18. spec:
  19. containers:
  20. - name: redis
  21. image: redis:6-alpine
  22. lifecycle:
  23. preStop:
  24. exec:
  25. command: ["sh","-c","sleep 5"]
  26. ports:
  27. - containerPort: 6379
  28. readinessProbe:
  29. tcpSocket:
  30. port: 6379
  31. initialDelaySeconds: 5
  32. periodSeconds: 10
  33. livenessProbe:
  34. tcpSocket:
  35. port: 6379
  36. initialDelaySeconds: 15
  37. periodSeconds: 20
  38. resources:
  39. requests:
  40. cpu: 500m
  41. memory: 512Mi
  42. limits:
  43. cpu: 1000m
  44. memory: 1024Mi
  45. volumeMounts:
  46. - name: timezone
  47. mountPath: /etc/localtime
  48. volumes:
  49. - name: timezone
  50. hostPath:
  51. path: /usr/share/zoneinfo/Asia/Shanghai
  52. ---
  53. apiVersion: v1
  54. kind: Service
  55. metadata:
  56. name: redis-svc
  57. namespace: adhoc
  58. spec:
  59. ports:
  60. - port: 6379
  61. selector:
  62. app: redis
  63. ---
  64. apiVersion: autoscaling/v2beta1
  65. kind: HorizontalPodAutoscaler
  66. metadata:
  67. name: redis-hpa-c
  68. namespace: adhoc
  69. labels:
  70. app: redis-hpa-c
  71. spec:
  72. scaleTargetRef:
  73. apiVersion: apps/v1
  74. kind: Deployment
  75. name: redis
  76. minReplicas: 3
  77. maxReplicas: 10
  78. metrics:
  79. - type: Resource
  80. resource:
  81. name: cpu
  82. targetAverageUtilization: 80
  83. ---
  84. apiVersion: autoscaling/v2beta1
  85. kind: HorizontalPodAutoscaler
  86. metadata:
  87. name: redis-hpa-m
  88. namespace: adhoc
  89. labels:
  90. app: redis-hpa-m
  91. spec:
  92. scaleTargetRef:
  93. apiVersion: apps/v1
  94. kind: Deployment
  95. name: redis
  96. minReplicas: 3
  97. maxReplicas: 10
  98. metrics:
  99. - type: Resource
  100. resource:
  101. name: memory
  102. targetAverageUtilization: 80
  • 部署服务,如果 adhoc namespace 不存在的话,请先通过 kubectl create namespace adhoc 创建

    1. $ kubectl apply -f redis.yaml
    2. deployment.apps/redis created
    3. service/redis-svc created
    4. horizontalpodautoscaler.autoscaling/redis-hpa-c created
    5. horizontalpodautoscaler.autoscaling/redis-hpa-m created
  • 查看服务允许状态

    1. $ kubectl get all -n adhoc
    2. NAME READY STATUS RESTARTS AGE
    3. pod/redis-585bc66876-5ph26 1/1 Running 0 6m5s
    4. pod/redis-585bc66876-bfqxz 1/1 Running 0 6m5s
    5. pod/redis-585bc66876-vvfc9 1/1 Running 0 6m5s
    6. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    7. service/redis-svc ClusterIP 172.24.15.8 <none> 6379/TCP 6m5s
    8. NAME READY UP-TO-DATE AVAILABLE AGE
    9. deployment.apps/redis 3/3 3 3 6m6s
    10. NAME DESIRED CURRENT READY AGE
    11. replicaset.apps/redis-585bc66876 3 3 3 6m6s
    12. NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
    13. horizontalpodautoscaler.autoscaling/redis-hpa-c Deployment/redis 0%/80% 3 10 3 6m6s
    14. horizontalpodautoscaler.autoscaling/redis-hpa-m Deployment/redis 0%/80% 3 10 3 6m6s
  • 测试服务

    1. $ kubectl run -i --tty --rm cli --image=redis:6-alpine -n adhoc -- sh
    2. /data # redis-cli -h redis-svc
    3. redis-svc:6379> set go-zero great
    4. OK
    5. redis-svc:6379> get go-zero
    6. "great"

    goctl kube 总结

    goctl 工具极大简化了 K8S yaml 文件的编写,提供了开箱即用的最佳实践,并且支持了模板自定义。

猜你想看