goctl kube

概述

goctl kube 可以快速生成 kubernetes deployment 资源文件。

头疼的 k8s yaml 文件编写?相信你也遇到过:

  • K8S yaml 参数很多,需要边写边查?
  • 保留回滚版本数怎么设?
  • 如何探测启动成功,如何探活?
  • 如何分配和限制资源?
  • 如何设置时区?否则打印日志是 GMT 标准时间
  • 如何暴露服务供其它服务调用?
  • 如何根据 CPU 和内存使用率来配置水平伸缩?

首先,你需要知道有这些知识点,其次要把这些知识点都搞明白也不容易,再次,每次编写依然容易出错!

goctl kube 指令

  1. $ goctl kube --help
  2. Generate kubernetes files
  3. Usage:
  4. goctl kube [command]
  5. Available Commands:
  6. deploy Generate deployment yaml file
  7. Flags:
  8. -h, --help help for kube
  9. Use "goctl kube [command] --help" for more information about a command.

goctl kube 目前支持生成 deployment yaml 文件。

goctl kube deploy 指令

  1. $ goctl kube deploy --help
  2. Generate deployment yaml file
  3. Usage:
  4. goctl kube deploy [flags]
  5. Flags:
  6. --branch string The branch of the remote repo, it does work with --remote
  7. -h, --help help for deploy
  8. --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
  9. --image string The docker image of deployment (required)
  10. --imagePullPolicy string Image pull policy. One of Always, Never, IfNotPresent
  11. --limitCpu int The limit cpu to deploy (default 1000)
  12. --limitMem int The limit memory to deploy (default 1024)
  13. --maxReplicas int The max replicas to deploy (default 10)
  14. --minReplicas int The min replicas to deploy (default 3)
  15. --name string The name of deployment (required)
  16. --namespace string The namespace of deployment (required)
  17. --nodePort int The nodePort of the deployment to expose
  18. --o string The output yaml file (required)
  19. --port int The port of the deployment to listen on pod (required)
  20. --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 priority
  21. The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
  22. --replicas int The number of replicas to deploy (default 3)
  23. --requestCpu int The request cpu to deploy (default 500)
  24. --requestMem int The request memory to deploy (default 512)
  25. --revisions int The number of revision history to limit (default 5)
  26. --secret string The secret to image pull from registry
  27. --serviceAccount string The ServiceAccount for the deployment
  28. --targetPort int The targetPort of the deployment, default to port
goctl kube - 图1 参数字段goctl kube - 图2 参数类型goctl kube - 图3 是否必填goctl kube - 图4 默认值goctl kube - 图5 参数说明
branchstringNO空字符串远程模板所在 git 分支名称,仅当 remote 有值时使用
homestringNO${HOME}/.goctl本地模板文件目录
imagestringYES空字符串镜像名称
imagePullPolicystringYES空字符串镜像拉取策略,Always:总是拉取,Never:从不拉取,IfNotPresent:不存在时拉取
limitCpuintNO1000cpu 资源使用上限
limitMemintNO1024内存资源使用上限
maxReplicasintNO10最大保副本数
minReplicasintNO3最小保副本数
namestringYES空字符串deployment 名称
namespacestringYES空字符串k8s 域名空间
nodePortintYES0需要暴露的服务端口
ostringYES空字符串yaml 文件名称
portintYES0需要监听的端口
remotestringNO空字符串远程模板所在 git 仓库地址,当此字段传值时,优先级高于 home 字段值
replicasintNO3副本数
requestCpuintNO500cpu 需求额度
requestMemintNO512内存需求额度
revisionsintNO1保留的版本数量,便于回滚
secretstringNO空字符串拉取镜像的密钥
serviceAccountstringNO空字符串服务账户
targetPortintNO0目标 port

使用示例

我们以 redis 镜像为例子演示如何使用 goctl kube deploy 指令生成 deployment yaml 文件。

  1. $ goctl kube deploy -name redis -namespace adhoc -image redis:6-alpine -o redis.yaml -port 6379
  2. Done.

执行上述命令后,会在当前目录下生成 redis.yaml 文件,内容如下:

  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. ports:
  23. - containerPort: 6379
  24. readinessProbe:
  25. tcpSocket:
  26. port: 6379
  27. initialDelaySeconds: 5
  28. periodSeconds: 10
  29. livenessProbe:
  30. tcpSocket:
  31. port: 6379
  32. initialDelaySeconds: 15
  33. periodSeconds: 20
  34. resources:
  35. requests:
  36. cpu: 500m
  37. memory: 512Mi
  38. limits:
  39. cpu: 1000m
  40. memory: 1024Mi
  41. volumeMounts:
  42. - name: timezone
  43. mountPath: /etc/localtime
  44. volumes:
  45. - name: timezone
  46. hostPath:
  47. path: /usr/share/zoneinfo/Asia/Shanghai
  48. ---
  49. apiVersion: v1
  50. kind: Service
  51. metadata:
  52. name: redis-svc
  53. namespace: adhoc
  54. spec:
  55. ports:
  56. - port: 6379
  57. targetPort: 6379
  58. selector:
  59. app: redis
  60. ---
  61. apiVersion: autoscaling/v2beta1
  62. kind: HorizontalPodAutoscaler
  63. metadata:
  64. name: redis-hpa-c
  65. namespace: adhoc
  66. labels:
  67. app: redis-hpa-c
  68. spec:
  69. scaleTargetRef:
  70. apiVersion: apps/v1
  71. kind: Deployment
  72. name: redis
  73. minReplicas: 3
  74. maxReplicas: 10
  75. metrics:
  76. - type: Resource
  77. resource:
  78. name: cpu
  79. targetAverageUtilization: 80
  80. ---
  81. apiVersion: autoscaling/v2beta1
  82. kind: HorizontalPodAutoscaler
  83. metadata:
  84. name: redis-hpa-m
  85. namespace: adhoc
  86. labels:
  87. app: redis-hpa-m
  88. spec:
  89. scaleTargetRef:
  90. apiVersion: apps/v1
  91. kind: Deployment
  92. name: redis
  93. minReplicas: 3
  94. maxReplicas: 10
  95. metrics:
  96. - type: Resource
  97. resource:
  98. name: memory
  99. targetAverageUtilization: 80