More Commands

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

  • goctl docker
  • goctl kube

goctl docker

goctl docker can quickly generate a Dockerfile to help developers/operations and maintenance personnel speed up the deployment pace and reduce deployment complexity.

Prepare

  • docker install

Dockerfile note

  • Choose the simplest mirror: For example, alpine, the entire mirror is about 5M
  • Set mirror time zone
    1. RUN apk add --no-cache tzdata
    2. ENV TZ Asia/Shanghai

Multi-stage build

  • Otherwise, an executable file will be built in the first stage of construction to ensure that the build process is independent of the host
  • The second stage uses the output of the first stage as input to construct the final minimalist image

Dockerfile writing process

  • First install the goctl tool

    1. $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl
  • Create a hello service under the greet project

    1. $ goctl api new hello

The file structure is as follows:

  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
  • Generate a Dockerfile in the hello directory
    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"]
  • To build mirror in the greet directory

    1. $ docker build -t hello:v1 -f service/hello/Dockerfile .
  • View mirror

    1. hello v1 5455f2eaea6b 7 minutes ago 18.1MB

It can be seen that the mirror size is about 18M.

  • Start service
    1. $ docker run --rm -it -p 8888:8888 hello:v1
  • Test service
    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 summary

The goctl tool greatly simplifies the writing of Dockerfile files, provides best practices out of the box, and supports template customization.

goctl kube

goctl kube provides the function of quickly generating a k8s deployment file, which can speed up the deployment progress of developers/operations and maintenance personnel and reduce deployment complexity.

Have a trouble to write K8S deployment files?

  • K8S yaml has a lot of parameters, need to write and check?
  • How to set the number of retained rollback versions?
  • How to detect startup success, how to detect live?
  • How to allocate and limit resources?
  • How to set the time zone? Otherwise, the print log is GMT standard time
  • How to expose services for other services to call?
  • How to configure horizontal scaling based on CPU and memory usage?

First, you need to know that you have these knowledge points, and secondly, it is not easy to understand all these knowledge points, and again, it is still easy to make mistakes every time you write!

Create service image

For demonstration, here we take the redis:6-alpine image as an example.

完整 K8S Deployment file writing process

  • First install the goctl tool
  1. $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl
  • One-click generation of K8S deployment files
  1. $ goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379

The generated yaml file is as follows:

  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
  • Deploy the service, if the adhoc namespace does not exist, please create it through 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
  • View service permission status

    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
  • Test service

    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 summary

    The goctl tool greatly simplifies the writing of K8S yaml files, provides best practices out of the box, and supports template customization.

Guess you wants