https://go-zero.dev/cn
    一个旨在为开发人员提高工程效率、降低出错率的辅助工具
    goctl是go-zero微服务框架下的代码生成工具。使用 goctl 可显著提升开发效率,让开发人员将时间重点放在业务开发上,其功能有:

    • api服务生成
    • rpc服务生成
    • model代码生成
    • 模板管理

    安装

    1. GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

    1642657780(1).png

    1. [root@UR-20210425NAMA ~]# goctl -v
    2. 2022/01/20 13:48:42 {"@timestamp":"2022-01-20T13:48:42.965+08","level":"error","content":"cpu_linux.go:29 open cpuacct.usage_percpu: no such file or directory"}
    3. goctl version 1.2.5 linux/amd64
    4. [root@UR-20210425NAMA ~]#
    1. [root@UR-20210425NAMA goctl]# goctl kube deploy -name nginx -namespace test -image nginx:alpine -o nginx-test.yaml -p
    2. ort 80
    3. 2022/01/20 13:23:43 {"@timestamp":"2022-01-20T13:23:43.726+08","level":"error","content":"cpu_linux.go:29 open cpuacct.usage_percpu: no such file or directory"}
    4. Done.
    5. [root@UR-20210425NAMA goctl]# ls -l
    6. total 4
    7. -rwxrwxrwx 1 root root 1944 Jan 20 13:23 nginx-test.yaml
    8. [root@UR-20210425NAMA goctl]# cat nginx-test.yaml
    9. apiVersion: apps/v1
    10. kind: Deployment
    11. metadata:
    12. name: nginx
    13. namespace: test
    14. labels:
    15. app: nginx
    16. spec:
    17. replicas: 3
    18. revisionHistoryLimit: 5
    19. selector:
    20. matchLabels:
    21. app: nginx
    22. template:
    23. metadata:
    24. labels:
    25. app: nginx
    26. spec:
    27. containers:
    28. - name: nginx
    29. image: nginx:alpine
    30. lifecycle:
    31. preStop:
    32. exec:
    33. command: ["sh","-c","sleep 5"]
    34. ports:
    35. - containerPort: 80
    36. readinessProbe:
    37. tcpSocket:
    38. port: 80
    39. initialDelaySeconds: 5
    40. periodSeconds: 10
    41. livenessProbe:
    42. tcpSocket:
    43. port: 80
    44. initialDelaySeconds: 15
    45. periodSeconds: 20
    46. resources:
    47. requests:
    48. cpu: 500m
    49. memory: 512Mi
    50. limits:
    51. cpu: 1000m
    52. memory: 1024Mi
    53. volumeMounts:
    54. - name: timezone
    55. mountPath: /etc/localtime
    56. volumes:
    57. - name: timezone
    58. hostPath:
    59. path: /usr/share/zoneinfo/Asia/Shanghai
    60. ---
    61. apiVersion: v1
    62. kind: Service
    63. metadata:
    64. name: nginx-svc
    65. namespace: test
    66. spec:
    67. ports:
    68. - port: 80
    69. selector:
    70. app: nginx
    71. ---
    72. apiVersion: autoscaling/v2beta1
    73. kind: HorizontalPodAutoscaler
    74. metadata:
    75. name: nginx-hpa-c
    76. namespace: test
    77. labels:
    78. app: nginx-hpa-c
    79. spec:
    80. scaleTargetRef:
    81. apiVersion: apps/v1
    82. kind: Deployment
    83. name: nginx
    84. minReplicas: 3
    85. maxReplicas: 10
    86. metrics:
    87. - type: Resource
    88. resource:
    89. name: cpu
    90. targetAverageUtilization: 80
    91. ---
    92. apiVersion: autoscaling/v2beta1
    93. kind: HorizontalPodAutoscaler
    94. metadata:
    95. name: nginx-hpa-m
    96. namespace: test
    97. labels:
    98. app: nginx-hpa-m
    99. spec:
    100. scaleTargetRef:
    101. apiVersion: apps/v1
    102. kind: Deployment
    103. name: nginx
    104. minReplicas: 3
    105. maxReplicas: 10
    106. metrics:
    107. - type: Resource
    108. resource:
    109. name: memory
    110. targetAverageUtilization: 80
    111. [root@UR-20210425NAMA goctl]#

    自定义参数

    1. [root@UR-20210425NAMA goctl]# goctl kube deploy -h
    2. 2022/01/20 13:39:28 {"@timestamp":"2022-01-20T13:39:28.620+08","level":"error","content":"cpu_linux.go:29 open cpuacct.usage_percpu: no such file or directory"}
    3. NAME:
    4. goctl kube deploy - generate deployment yaml file
    5. USAGE:
    6. goctl kube deploy [command options] [arguments...]
    7. OPTIONS:
    8. --name value the name of deployment
    9. --namespace value the namespace of deployment
    10. --image value the docker image of deployment
    11. --secret value the secret to image pull from registry
    12. --requestCpu value the request cpu to deploy (default: 500)
    13. --requestMem value the request memory to deploy (default: 512)
    14. --limitCpu value the limit cpu to deploy (default: 1000)
    15. --limitMem value the limit memory to deploy (default: 1024)
    16. -o value the output yaml file
    17. --replicas value the number of replicas to deploy (default: 3)
    18. --revisions value the number of revision history to limit (default: 5)
    19. --port value the port of the deployment to listen on pod (default: 0)
    20. --nodePort value the nodePort of the deployment to expose (default: 0)
    21. --minReplicas value the min replicas to deploy (default: 3)
    22. --maxReplicas value the max replicas of deploy (default: 10)
    23. --home value the goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
    24. --remote value the remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
    25. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
    26. [root@UR-20210425NAMA goctl]#
    1. goctl kube deploy -name nginx -namespace test -image nginx:alpine -replicas 1 -minReplicas 1 -maxReplicas 3 -o nginx-j.yaml -port 80 -nodePort 31330
    1. [root@UR-20210425NAMA goctl]# goctl kube deploy -name nginx -namespace test -image nginx:alpine -replicas 1 -minReplicas 1 -maxReplicas 3 -o nginx-j.yaml -port 80 -nodePort 31330
    2. 2022/01/20 13:26:22 {"@timestamp":"2022-01-20T13:26:22.868+08","level":"error","content":"cpu_linux.go:29 open cpuacct.usage_percpu: no such file or directory"}
    3. Done.
    4. [root@UR-20210425NAMA goctl]# ls -l
    5. total 8
    6. -rwxrwxrwx 1 root root 2022 Jan 20 13:26 nginx-j.yaml
    7. -rwxrwxrwx 1 root root 1944 Jan 20 13:23 nginx-test.yaml
    8. [root@UR-20210425NAMA goctl]# cat nginx-j.yaml
    9. apiVersion: apps/v1
    10. kind: Deployment
    11. metadata:
    12. name: nginx
    13. namespace: test
    14. labels:
    15. app: nginx
    16. spec:
    17. replicas: 1
    18. revisionHistoryLimit: 5
    19. selector:
    20. matchLabels:
    21. app: nginx
    22. template:
    23. metadata:
    24. labels:
    25. app: nginx
    26. spec:
    27. containers:
    28. - name: nginx
    29. image: nginx:alpine
    30. lifecycle:
    31. preStop:
    32. exec:
    33. command: ["sh","-c","sleep 5"]
    34. ports:
    35. - containerPort: 80
    36. readinessProbe:
    37. tcpSocket:
    38. port: 80
    39. initialDelaySeconds: 5
    40. periodSeconds: 10
    41. livenessProbe:
    42. tcpSocket:
    43. port: 80
    44. initialDelaySeconds: 15
    45. periodSeconds: 20
    46. resources:
    47. requests:
    48. cpu: 500m
    49. memory: 512Mi
    50. limits:
    51. cpu: 1000m
    52. memory: 1024Mi
    53. volumeMounts:
    54. - name: timezone
    55. mountPath: /etc/localtime
    56. volumes:
    57. - name: timezone
    58. hostPath:
    59. path: /usr/share/zoneinfo/Asia/Shanghai
    60. ---
    61. apiVersion: v1
    62. kind: Service
    63. metadata:
    64. name: nginx-svc
    65. namespace: test
    66. spec:
    67. ports:
    68. - nodePort: 31330
    69. port: 80
    70. protocol: TCP
    71. targetPort: 80
    72. type: NodePort
    73. selector:
    74. app: nginx
    75. ---
    76. apiVersion: autoscaling/v2beta1
    77. kind: HorizontalPodAutoscaler
    78. metadata:
    79. name: nginx-hpa-c
    80. namespace: test
    81. labels:
    82. app: nginx-hpa-c
    83. spec:
    84. scaleTargetRef:
    85. apiVersion: apps/v1
    86. kind: Deployment
    87. name: nginx
    88. minReplicas: 1
    89. maxReplicas: 3
    90. metrics:
    91. - type: Resource
    92. resource:
    93. name: cpu
    94. targetAverageUtilization: 80
    95. ---
    96. apiVersion: autoscaling/v2beta1
    97. kind: HorizontalPodAutoscaler
    98. metadata:
    99. name: nginx-hpa-m
    100. namespace: test
    101. labels:
    102. app: nginx-hpa-m
    103. spec:
    104. scaleTargetRef:
    105. apiVersion: apps/v1
    106. kind: Deployment
    107. name: nginx
    108. minReplicas: 1
    109. maxReplicas: 3
    110. metrics:
    111. - type: Resource
    112. resource:
    113. name: memory
    114. targetAverageUtilization: 80
    115. [root@UR-20210425NAMA goctl]#

    https://github.com/zeromicro/go-zero-template/blob/main/kube/deployment.tpl