goctl kube
概述
goctl kube 可以快速生成 kubernetes deployment 资源文件。
头疼的 k8s yaml 文件编写?相信你也遇到过:
- K8S yaml 参数很多,需要边写边查?
- 保留回滚版本数怎么设?
- 如何探测启动成功,如何探活?
- 如何分配和限制资源?
- 如何设置时区?否则打印日志是 GMT 标准时间
- 如何暴露服务供其它服务调用?
- 如何根据 CPU 和内存使用率来配置水平伸缩?
首先,你需要知道有这些知识点,其次要把这些知识点都搞明白也不容易,再次,每次编写依然容易出错!
goctl kube 指令
$ goctl kube --helpGenerate kubernetes filesUsage:goctl kube [command]Available Commands:deploy Generate deployment yaml fileFlags:-h, --help help for kubeUse "goctl kube [command] --help" for more information about a command.
goctl kube 目前支持生成 deployment yaml 文件。
goctl kube deploy 指令
$ goctl kube deploy --helpGenerate deployment yaml fileUsage:goctl kube deploy [flags]Flags:--branch string The branch of the remote repo, it does work with --remote-h, --help help for deploy--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority--image string The docker image of deployment (required)--imagePullPolicy string Image pull policy. One of Always, Never, IfNotPresent--limitCpu int The limit cpu to deploy (default 1000)--limitMem int The limit memory to deploy (default 1024)--maxReplicas int The max replicas to deploy (default 10)--minReplicas int The min replicas to deploy (default 3)--name string The name of deployment (required)--namespace string The namespace of deployment (required)--nodePort int The nodePort of the deployment to expose--o string The output yaml file (required)--port int The port of the deployment to listen on pod (required)--remote string 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--replicas int The number of replicas to deploy (default 3)--requestCpu int The request cpu to deploy (default 500)--requestMem int The request memory to deploy (default 512)--revisions int The number of revision history to limit (default 5)--secret string The secret to image pull from registry--serviceAccount string The ServiceAccount for the deployment--targetPort int The targetPort of the deployment, default to port
参数字段 | 参数类型 | 是否必填 | 默认值 | 参数说明 |
|---|---|---|---|---|
| branch | string | NO | 空字符串 | 远程模板所在 git 分支名称,仅当 remote 有值时使用 |
| home | string | NO | ${HOME}/.goctl | 本地模板文件目录 |
| image | string | YES | 空字符串 | 镜像名称 |
| imagePullPolicy | string | YES | 空字符串 | 镜像拉取策略,Always:总是拉取,Never:从不拉取,IfNotPresent:不存在时拉取 |
| limitCpu | int | NO | 1000 | cpu 资源使用上限 |
| limitMem | int | NO | 1024 | 内存资源使用上限 |
| maxReplicas | int | NO | 10 | 最大保副本数 |
| minReplicas | int | NO | 3 | 最小保副本数 |
| name | string | YES | 空字符串 | deployment 名称 |
| namespace | string | YES | 空字符串 | k8s 域名空间 |
| nodePort | int | YES | 0 | 需要暴露的服务端口 |
| o | string | YES | 空字符串 | yaml 文件名称 |
| port | int | YES | 0 | 需要监听的端口 |
| remote | string | NO | 空字符串 | 远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值 |
| replicas | int | NO | 3 | 副本数 |
| requestCpu | int | NO | 500 | cpu 需求额度 |
| requestMem | int | NO | 512 | 内存需求额度 |
| revisions | int | NO | 1 | 保留的版本数量,便于回滚 |
| secret | string | NO | 空字符串 | 拉取镜像的密钥 |
| serviceAccount | string | NO | 空字符串 | 服务账户 |
| targetPort | int | NO | 0 | 目标 port |
使用示例
我们以 redis 镜像为例子演示如何使用 goctl kube deploy 指令生成 deployment yaml 文件。
$ goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379Done.
执行上述命令后,会在当前目录下生成 redis.yaml 文件,内容如下:
apiVersion: apps/v1kind: Deploymentmetadata:name: redisnamespace: adhoclabels:app: redisspec:replicas: 3revisionHistoryLimit: 5selector:matchLabels:app: redistemplate:metadata:labels:app: redisspec:containers:- name: redisimage: redis:6-alpineports:- containerPort: 6379readinessProbe:tcpSocket:port: 6379initialDelaySeconds: 5periodSeconds: 10livenessProbe:tcpSocket:port: 6379initialDelaySeconds: 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: redis-svcnamespace: adhocspec:ports:- port: 6379targetPort: 6379selector:app: redis---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: redis-hpa-cnamespace: adhoclabels:app: redis-hpa-cspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: redisminReplicas: 3maxReplicas: 10metrics:- type: Resourceresource:name: cputargetAverageUtilization: 80---apiVersion: autoscaling/v2beta1kind: HorizontalPodAutoscalermetadata:name: redis-hpa-mnamespace: adhoclabels:app: redis-hpa-mspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: redisminReplicas: 3maxReplicas: 10metrics:- type: Resourceresource:name: memorytargetAverageUtilization: 80
参数字段