https://go-zero.dev/cn
一个旨在为开发人员提高工程效率、降低出错率的辅助工具
goctl是go-zero微服务框架下的代码生成工具。使用 goctl 可显著提升开发效率,让开发人员将时间重点放在业务开发上,其功能有:
- api服务生成
- rpc服务生成
- model代码生成
- 模板管理
安装
GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl

[root@UR-20210425NAMA ~]# goctl -v2022/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"}goctl version 1.2.5 linux/amd64[root@UR-20210425NAMA ~]#
[root@UR-20210425NAMA goctl]# goctl kube deploy -name nginx -namespace test -image nginx:alpine -o nginx-test.yaml -port 802022/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"}Done.[root@UR-20210425NAMA goctl]# ls -ltotal 4-rwxrwxrwx 1 root root 1944 Jan 20 13:23 nginx-test.yaml[root@UR-20210425NAMA goctl]# cat nginx-test.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: nginxnamespace: testlabels:app: nginxspec:replicas: 3revisionHistoryLimit: 5selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:alpinelifecycle:preStop:exec:command: ["sh","-c","sleep 5"]ports:- containerPort: 80readinessProbe:tcpSocket:port: 80initialDelaySeconds: 5periodSeconds: 10livenessProbe:tcpSocket:port: 80initialDelaySeconds: 15periodSeconds: 20resources:requests:cpu: 500mmemory: 512Milimits:cpu: 1000mmemory: 1024MivolumeMounts:- name: timezonemountPath: /etc/localtimevolumes:- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai---apiVersion: v1kind: Servicemetadata:name: nginx-svcnamespace: testspec:ports:- port: 80selector:app: nginx---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: nginx-hpa-cnamespace: testlabels:app: nginx-hpa-cspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxminReplicas: 3maxReplicas: 10metrics:- type: Resourceresource:name: cputargetAverageUtilization: 80---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: nginx-hpa-mnamespace: testlabels:app: nginx-hpa-mspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxminReplicas: 3maxReplicas: 10metrics:- type: Resourceresource:name: memorytargetAverageUtilization: 80[root@UR-20210425NAMA goctl]#
自定义参数
[root@UR-20210425NAMA goctl]# goctl kube deploy -h2022/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"}NAME:goctl kube deploy - generate deployment yaml fileUSAGE:goctl kube deploy [command options] [arguments...]OPTIONS:--name value the name of deployment--namespace value the namespace of deployment--image value the docker image of deployment--secret value the secret to image pull from registry--requestCpu value the request cpu to deploy (default: 500)--requestMem value the request memory to deploy (default: 512)--limitCpu value the limit cpu to deploy (default: 1000)--limitMem value the limit memory to deploy (default: 1024)-o value the output yaml file--replicas value the number of replicas to deploy (default: 3)--revisions value the number of revision history to limit (default: 5)--port value the port of the deployment to listen on pod (default: 0)--nodePort value the nodePort of the deployment to expose (default: 0)--minReplicas value the min replicas to deploy (default: 3)--maxReplicas value the max replicas of deploy (default: 10)--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--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 priorityThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure[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
[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 313302022/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"}Done.[root@UR-20210425NAMA goctl]# ls -ltotal 8-rwxrwxrwx 1 root root 2022 Jan 20 13:26 nginx-j.yaml-rwxrwxrwx 1 root root 1944 Jan 20 13:23 nginx-test.yaml[root@UR-20210425NAMA goctl]# cat nginx-j.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: nginxnamespace: testlabels:app: nginxspec:replicas: 1revisionHistoryLimit: 5selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:alpinelifecycle:preStop:exec:command: ["sh","-c","sleep 5"]ports:- containerPort: 80readinessProbe:tcpSocket:port: 80initialDelaySeconds: 5periodSeconds: 10livenessProbe:tcpSocket:port: 80initialDelaySeconds: 15periodSeconds: 20resources:requests:cpu: 500mmemory: 512Milimits:cpu: 1000mmemory: 1024MivolumeMounts:- name: timezonemountPath: /etc/localtimevolumes:- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai---apiVersion: v1kind: Servicemetadata:name: nginx-svcnamespace: testspec:ports:- nodePort: 31330port: 80protocol: TCPtargetPort: 80type: NodePortselector:app: nginx---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: nginx-hpa-cnamespace: testlabels:app: nginx-hpa-cspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxminReplicas: 1maxReplicas: 3metrics:- type: Resourceresource:name: cputargetAverageUtilization: 80---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: nginx-hpa-mnamespace: testlabels:app: nginx-hpa-mspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxminReplicas: 1maxReplicas: 3metrics:- type: Resourceresource:name: memorytargetAverageUtilization: 80[root@UR-20210425NAMA goctl]#
https://github.com/zeromicro/go-zero-template/blob/main/kube/deployment.tpl
