使用 Kustomize 声明式管理 Kubernetes 对象
Kustomize 是一个独立工具,通过 kustomization 文件来定制 Kubernetes 对象。
自 1.14 版本起,kubectl 也支持使用 kustomization 文件管理 Kubernetes 对象。要查看包含 kustomization 文件的目录中的资源,请运行以下命令:
kubectl kustomize <kustomization_directory>
要应用这些资源,请运行带有 --kustomize
或 -k
标志的 kubectl apply
命令。
kubectl apply -k <kustomization_directory>
准备工作
安装 kubectl
。
你需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具已配置为与你的集群通信。建议在至少有两个不充当控制平面主机的节点的集群上运行本教程。如果你还没有集群,可以使用 minikube 创建一个,或者使用以下 Kubernetes 演练场之一。
要检查版本,请输入 kubectl version
。
Kustomize 概览
Kustomize 是一个用于定制 Kubernetes 配置的工具。它具有以下功能来管理应用程序配置文件:
- 从其他源生成资源
- 为资源设置交叉字段
- 组合和定制资源集合
生成资源
ConfigMaps 和 Secrets 包含由其他 Kubernetes 对象(如 Pod)使用的配置或敏感数据。ConfigMaps 或 Secrets 的真实来源通常在集群外部,例如 .properties
文件或 SSH 密钥文件。Kustomize 具有 secretGenerator
和 configMapGenerator
,它们可以从文件或字面量生成 Secret 和 ConfigMap。
configMapGenerator
要从文件生成 ConfigMap,请在 configMapGenerator
的 files
列表中添加一个条目。以下是一个从 .properties
文件生成带有数据项的 ConfigMap 的示例:
# Create a application.properties file
cat <<EOF >application.properties
FOO=Bar
EOF
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-1
files:
- application.properties
EOF
生成的 ConfigMap 可以通过以下命令检查:
kubectl kustomize ./
生成的 ConfigMap 如下:
apiVersion: v1
data:
application.properties: |
FOO=Bar
kind: ConfigMap
metadata:
name: example-configmap-1-8mbdf7882g
要从 env 文件生成 ConfigMap,请在 configMapGenerator
的 envs
列表中添加一个条目。以下是一个从 .env
文件生成带有数据项的 ConfigMap 的示例:
# Create a .env file
cat <<EOF >.env
FOO=Bar
EOF
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-1
envs:
- .env
EOF
生成的 ConfigMap 可以通过以下命令检查:
kubectl kustomize ./
生成的 ConfigMap 如下:
apiVersion: v1
data:
FOO: Bar
kind: ConfigMap
metadata:
name: example-configmap-1-42cfbf598f
注意
.env
文件中的每个变量都成为你生成的 ConfigMap 中的一个单独的键。这与上一个示例不同,上一个示例将名为 application.properties
的文件(及其所有条目)作为单个键的值嵌入。ConfigMaps 也可以从字面量键值对生成。要从字面量键值对生成 ConfigMap,请在 configMapGenerator 的 literals
列表中添加一个条目。以下是一个从键值对生成带有数据项的 ConfigMap 的示例:
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-2
literals:
- FOO=Bar
EOF
生成的 ConfigMap 可以通过以下命令检查:
kubectl kustomize ./
生成的 ConfigMap 如下:
apiVersion: v1
data:
FOO: Bar
kind: ConfigMap
metadata:
name: example-configmap-2-g2hdhfc6tk
要在 Deployment 中使用生成的 ConfigMap,请通过 configMapGenerator 的名称引用它。Kustomize 将自动将此名称替换为生成的名称。
这是一个使用生成的 ConfigMap 的 Deployment 示例:
# Create an application.properties file
cat <<EOF >application.properties
FOO=Bar
EOF
cat <<EOF >deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: my-app
volumeMounts:
- name: config
mountPath: /config
volumes:
- name: config
configMap:
name: example-configmap-1
EOF
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
configMapGenerator:
- name: example-configmap-1
files:
- application.properties
EOF
生成 ConfigMap 和 Deployment
kubectl kustomize ./
生成的 Deployment 将按名称引用生成的 ConfigMap。
apiVersion: v1
data:
application.properties: |
FOO=Bar
kind: ConfigMap
metadata:
name: example-configmap-1-g4hk9g2ff8
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: my-app
name: my-app
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: my-app
name: app
volumeMounts:
- mountPath: /config
name: config
volumes:
- configMap:
name: example-configmap-1-g4hk9g2ff8
name: config
secretGenerator
你可以从文件或字面量键值对生成 Secret。要从文件生成 Secret,请在 secretGenerator
的 files
列表中添加一个条目。以下是一个从文件生成带有数据项的 Secret 的示例:
# Create a password.txt file
cat <<EOF >./password.txt
username=admin
password=secret
EOF
cat <<EOF >./kustomization.yaml
secretGenerator:
- name: example-secret-1
files:
- password.txt
EOF
生成的 Secret 如下:
apiVersion: v1
data:
password.txt: dXNlcm5hbWU9YWRtaW4KcGFzc3dvcmQ9c2VjcmV0Cg==
kind: Secret
metadata:
name: example-secret-1-t2kt65hgtb
type: Opaque
要从字面量键值对生成 Secret,请在 secretGenerator
的 literals
列表中添加一个条目。以下是一个从键值对生成带有数据项的 Secret 的示例:
cat <<EOF >./kustomization.yaml
secretGenerator:
- name: example-secret-2
literals:
- username=admin
- password=secret
EOF
生成的 Secret 如下:
apiVersion: v1
data:
password: c2VjcmV0
username: YWRtaW4=
kind: Secret
metadata:
name: example-secret-2-t52t6g96d8
type: Opaque
与 ConfigMap 类似,生成的 Secret 可以通过引用 secretGenerator 的名称在 Deployment 中使用。
# Create a password.txt file
cat <<EOF >./password.txt
username=admin
password=secret
EOF
cat <<EOF >deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: my-app
volumeMounts:
- name: password
mountPath: /secrets
volumes:
- name: password
secret:
secretName: example-secret-1
EOF
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
secretGenerator:
- name: example-secret-1
files:
- password.txt
EOF
generatorOptions
生成的 ConfigMap 和 Secret 会附加一个内容哈希后缀。这确保了当内容更改时会生成新的 ConfigMap 或 Secret。要禁用附加后缀的行为,可以使用 generatorOptions
。此外,还可以为生成的 ConfigMap 和 Secret 指定交叉选项。
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-configmap-3
literals:
- FOO=Bar
generatorOptions:
disableNameSuffixHash: true
labels:
type: generated
annotations:
note: generated
EOF
运行 kubectl kustomize ./
查看生成的 ConfigMap
apiVersion: v1
data:
FOO: Bar
kind: ConfigMap
metadata:
annotations:
note: generated
labels:
type: generated
name: example-configmap-3
设置交叉字段
为项目中的所有 Kubernetes 资源设置交叉字段是很常见的。设置交叉字段的一些用例:
- 为所有资源设置相同的命名空间
- 添加相同的名称前缀或后缀
- 添加相同的标签集
- 添加相同的注解集
这是一个例子:
# Create a deployment.yaml
cat <<EOF >./deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
EOF
cat <<EOF >./kustomization.yaml
namespace: my-namespace
namePrefix: dev-
nameSuffix: "-001"
labels:
- pairs:
app: bingo
includeSelectors: true
commonAnnotations:
oncallPager: 800-555-1212
resources:
- deployment.yaml
EOF
运行 kubectl kustomize ./
查看这些字段是否都在 Deployment 资源中设置
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
oncallPager: 800-555-1212
labels:
app: bingo
name: dev-nginx-deployment-001
namespace: my-namespace
spec:
selector:
matchLabels:
app: bingo
template:
metadata:
annotations:
oncallPager: 800-555-1212
labels:
app: bingo
spec:
containers:
- image: nginx
name: nginx
组合和定制资源
在一个项目中组合一组资源并将它们管理在同一个文件或目录中是很常见的。Kustomize 提供了从不同文件组合资源并对其应用补丁或其他自定义的功能。
组合
Kustomize 支持不同资源的组合。kustomization.yaml
文件中的 resources
字段定义了要包含在配置中的资源列表。在 resources
列表中设置资源的配置文件的路径。以下是一个由 Deployment 和 Service 组成的 NGINX 应用程序的示例:
# Create a deployment.yaml file
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
EOF
# Create a service.yaml file
cat <<EOF > service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
EOF
# Create a kustomization.yaml composing them
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
- service.yaml
EOF
来自 kubectl kustomize ./
的资源包含 Deployment 和 Service 对象。
定制
补丁可用于对资源应用不同的自定义。Kustomize 通过 patches
字段支持通过 StrategicMerge
和 Json6902
的不同补丁机制。patches
可以是文件或内联字符串,目标可以是单个或多个资源。
patches
字段包含按指定顺序应用的补丁列表。补丁目标通过 group
、version
、kind
、name
、namespace
、labelSelector
和 annotationSelector
选择资源。
建议使用完成一件事的小补丁。例如,创建一个补丁用于增加 Deployment 副本数量,另一个补丁用于设置内存限制。目标资源通过补丁文件中的 group
、version
、kind
和 name
字段进行匹配。
# Create a deployment.yaml file
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
EOF
# Create a patch increase_replicas.yaml
cat <<EOF > increase_replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
EOF
# Create another patch set_memory.yaml
cat <<EOF > set_memory.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
template:
spec:
containers:
- name: my-nginx
resources:
limits:
memory: 512Mi
EOF
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
patches:
- path: increase_replicas.yaml
- path: set_memory.yaml
EOF
运行 kubectl kustomize ./
查看 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
run: my-nginx
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- image: nginx
name: my-nginx
ports:
- containerPort: 80
resources:
limits:
memory: 512Mi
并非所有资源或字段都支持 strategicMerge
补丁。为了支持修改任意资源中的任意字段,Kustomize 提供了通过 Json6902
应用 JSON 补丁。要为 Json6902
补丁找到正确的资源,必须在 kustomization.yaml
中指定 target
字段。
例如,增加 Deployment 对象的副本数量也可以通过 Json6902
补丁完成。目标资源通过 target
字段中的 group
、version
、kind
和 name
进行匹配。
# Create a deployment.yaml file
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
EOF
# Create a json patch
cat <<EOF > patch.yaml
- op: replace
path: /spec/replicas
value: 3
EOF
# Create a kustomization.yaml
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
patches:
- target:
group: apps
version: v1
kind: Deployment
name: my-nginx
path: patch.yaml
EOF
运行 kubectl kustomize ./
查看 replicas
字段是否已更新
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
run: my-nginx
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- image: nginx
name: my-nginx
ports:
- containerPort: 80
除了补丁,Kustomize 还提供了定制容器镜像或将其他对象的字段值注入容器而无需创建补丁的功能。例如,你可以通过在 kustomization.yaml
的 images
字段中指定新镜像来更改容器中使用的镜像。
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
EOF
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
images:
- name: nginx
newName: my.image.registry/nginx
newTag: "1.4.0"
EOF
运行 kubectl kustomize ./
查看正在使用的镜像是否已更新
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 2
selector:
matchLabels:
run: my-nginx
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- image: my.image.registry/nginx:1.4.0
name: my-nginx
ports:
- containerPort: 80
有时,在 Pod 中运行的应用程序可能需要使用来自其他对象的配置值。例如,Deployment 对象的 Pod 需要从 Env 或作为命令参数读取相应的 Service 名称。由于 Service 名称可能会随着 kustomization.yaml
文件中添加的 namePrefix
或 nameSuffix
而改变,因此不建议在命令参数中硬编码 Service 名称。对于此用法,Kustomize 可以通过 replacements
将 Service 名称注入容器中。
# Create a deployment.yaml file (quoting the here doc delimiter)
cat <<'EOF' > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
command: ["start", "--host", "MY_SERVICE_NAME_PLACEHOLDER"]
EOF
# Create a service.yaml file
cat <<EOF > service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
EOF
cat <<EOF >./kustomization.yaml
namePrefix: dev-
nameSuffix: "-001"
resources:
- deployment.yaml
- service.yaml
replacements:
- source:
kind: Service
name: my-nginx
fieldPath: metadata.name
targets:
- select:
kind: Deployment
name: my-nginx
fieldPaths:
- spec.template.spec.containers.0.command.2
EOF
运行 kubectl kustomize ./
查看注入容器中的 Service 名称是否为 dev-my-nginx-001
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-my-nginx-001
spec:
replicas: 2
selector:
matchLabels:
run: my-nginx
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- command:
- start
- --host
- dev-my-nginx-001
image: nginx
name: my-nginx
基础和叠加
Kustomize 具有基础和叠加的概念。基础是一个包含 kustomization.yaml
文件的目录,其中包含一组资源和相关的定制。基础可以是本地目录,也可以是远程仓库中的目录,只要其中存在 kustomization.yaml
。叠加是一个包含 kustomization.yaml
文件的目录,它将其他 kustomization 目录作为其 bases
引用。基础不知道叠加的存在,并且可以在多个叠加中使用。
叠加目录中的 kustomization.yaml
可以引用多个 bases
,将这些基础中定义的所有资源组合成一个统一的配置。此外,它还可以在这些资源之上应用自定义以满足特定要求。
这是一个基础的示例:
# Create a directory to hold the base
mkdir base
# Create a base/deployment.yaml
cat <<EOF > base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
EOF
# Create a base/service.yaml file
cat <<EOF > base/service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
EOF
# Create a base/kustomization.yaml
cat <<EOF > base/kustomization.yaml
resources:
- deployment.yaml
- service.yaml
EOF
这个基础可以在多个叠加中使用。你可以在不同的叠加中添加不同的 namePrefix
或其他交叉字段。以下是两个使用相同基础的叠加。
mkdir dev
cat <<EOF > dev/kustomization.yaml
resources:
- ../base
namePrefix: dev-
EOF
mkdir prod
cat <<EOF > prod/kustomization.yaml
resources:
- ../base
namePrefix: prod-
EOF
如何使用 Kustomize 应用/查看/删除对象
在 kubectl
命令中使用 --kustomize
或 -k
来识别由 kustomization.yaml
管理的资源。请注意,-k
应该指向一个 kustomization 目录,例如:
kubectl apply -k <kustomization directory>/
给定以下 kustomization.yaml
,
# Create a deployment.yaml file
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
EOF
# Create a kustomization.yaml
cat <<EOF >./kustomization.yaml
namePrefix: dev-
labels:
- pairs:
app: my-nginx
includeSelectors: true
resources:
- deployment.yaml
EOF
运行以下命令应用 Deployment 对象 dev-my-nginx
:
> kubectl apply -k ./
deployment.apps/dev-my-nginx created
运行以下命令之一查看 Deployment 对象 dev-my-nginx
:
kubectl get -k ./
kubectl describe -k ./
运行以下命令,将 Deployment 对象 dev-my-nginx
与应用清单后集群所处的状态进行比较:
kubectl diff -k ./
运行以下命令删除 Deployment 对象 dev-my-nginx
:
> kubectl delete -k ./
deployment.apps "dev-my-nginx" deleted
Kustomize 功能列表
字段 | 类型 | 说明 |
---|---|---|
bases | []string | 此列表中的每个条目都应解析为包含 kustomization.yaml 文件的目录 |
commonAnnotations | map[string]string | 要添加到所有资源的注解 |
commonLabels | map[string]string | 要添加到所有资源和选择器的标签 |
configMapGenerator | []ConfigMapArgs | 此列表中的每个条目都生成一个 ConfigMap |
configurations | []string | 此列表中的每个条目都应解析为包含 Kustomize 转换器配置的文件 |
crds | []string | 此列表中的每个条目都应解析为 Kubernetes 类型的 OpenAPI 定义文件 |
generatorOptions | GeneratorOptions | 修改所有 ConfigMap 和 Secret 生成器的行为 |
images | []Image | 每个条目用于修改一个镜像的名称、标签和/或摘要,而无需创建补丁 |
labels | map[string]string | 添加标签而无需自动注入相应的选择器 |
namePrefix | string | 此字段的值将添加到所有资源的名称前面 |
nameSuffix | string | 此字段的值将添加到所有资源的名称后面 |
patchesJson6902 | []Patch | 此列表中的每个条目都应解析为 Kubernetes 对象和 Json 补丁 |
patchesStrategicMerge | []string | 此列表中的每个条目都应解析为 Kubernetes 对象的战略合并补丁 |
replacements | []Replacements | 将资源字段的值复制到任意数量的指定目标。 |
resources | []string | 此列表中的每个条目都必须解析为现有的资源配置文件 |
secretGenerator | []SecretArgs | 此列表中的每个条目都生成一个 Secret |
vars | []Var | 每个条目用于从资源的字段中捕获文本 |