Skip to main content

goctl kube

概述

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

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

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

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

goctl kube 指令

$ goctl kube --help
Generate kubernetes files

Usage:
goctl kube [command]

Available Commands:
deploy Generate deployment yaml file

Flags:
-h, --help help for kube


Use "goctl kube [command] --help" for more information about a command.

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

goctl kube deploy 指令

$ goctl kube deploy --help
Generate deployment yaml file

Usage:
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 priority
The 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
参数字段 参数类型 是否必填 默认值 参数说明
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 文件。

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

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

apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: adhoc
labels:
app: redis
spec:
replicas: 3
revisionHistoryLimit: 5
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:6-alpine
ports:
- containerPort: 6379
readinessProbe:
tcpSocket:
port: 6379
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 6379
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: redis-svc
namespace: adhoc
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis

---

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: redis-hpa-c
namespace: adhoc
labels:
app: redis-hpa-c
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: redis
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 80

---

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: redis-hpa-m
namespace: adhoc
labels:
app: redis-hpa-m
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: redis
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
targetAverageUtilization: 80