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 -v
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"}
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 -p
ort 80
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"}
Done.
[root@UR-20210425NAMA goctl]# ls -l
total 4
-rwxrwxrwx 1 root root 1944 Jan 20 13:23 nginx-test.yaml
[root@UR-20210425NAMA goctl]# cat nginx-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: test
labels:
app: nginx
spec:
replicas: 3
revisionHistoryLimit: 5
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
lifecycle:
preStop:
exec:
command: ["sh","-c","sleep 5"]
ports:
- containerPort: 80
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 15
periodSeconds: 20
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1024Mi
volumeMounts:
- name: timezone
mountPath: /etc/localtime
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: test
spec:
ports:
- port: 80
selector:
app: nginx
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-c
namespace: test
labels:
app: nginx-hpa-c
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-m
namespace: test
labels:
app: nginx-hpa-m
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
targetAverageUtilization: 80
[root@UR-20210425NAMA goctl]#
自定义参数
[root@UR-20210425NAMA goctl]# goctl kube deploy -h
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"}
NAME:
goctl kube deploy - generate deployment yaml file
USAGE:
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 priority
The 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 31330
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"}
Done.
[root@UR-20210425NAMA goctl]# ls -l
total 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.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: test
labels:
app: nginx
spec:
replicas: 1
revisionHistoryLimit: 5
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
lifecycle:
preStop:
exec:
command: ["sh","-c","sleep 5"]
ports:
- containerPort: 80
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 15
periodSeconds: 20
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1024Mi
volumeMounts:
- name: timezone
mountPath: /etc/localtime
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: test
spec:
ports:
- nodePort: 31330
port: 80
protocol: TCP
targetPort: 80
type: NodePort
selector:
app: nginx
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-c
namespace: test
labels:
app: nginx-hpa-c
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa-m
namespace: test
labels:
app: nginx-hpa-m
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: memory
targetAverageUtilization: 80
[root@UR-20210425NAMA goctl]#
https://github.com/zeromicro/go-zero-template/blob/main/kube/deployment.tpl