kustomize一直是备受欢迎的yaml配置管理之一,在过去kustomize一直在解决
“提供一种操作配置数据的方法,而不会使原始配置无法被 Kubernetes 使用。”,我们不用去和其他工具做比较,因为这就是kustomize的魅力所在。
但是kustomize也有自己的缺点,它无法像helm那样灵活多变,比如,在配置多个一个Ingress的域名的时候,这在helm中将会非常简单,但是在kustomize中,几乎无法通过kustomize本身来解决,但是官方提供了var,而后var被诟病,于是出现了valueAdd,但很可惜,valueAdd并不是为了解决这个问题。valueAdd是从vars演变而来,但是valueAdd并不是最好的方式
这么多的方式,均在解决一个核心的问题,环境变量env和字符自定义删除vars是计划的一部分,很显然,目前并没有更好的方式来解决更多的问题为了应对这个情况,使用最原始的envsubst成了一个选项。
如果你并不希望你的配置清单是原始的,而是一些环境变量,大量的模板语法,你可以尝试helm。但用helm来管理大量零散的清单配置,在我看来是条不归路。因为事情在演变的过程中会不断的超过预期,变得复杂。而一旦复杂只会增加额外的成本。
像往常一样去配置一个kustomize的目录
[root@linuxea.com ~/kustomize]# tree ./
./
└── env-path
├── base
│ ├── deployment.yaml
│ └── kustomization.yaml
├── kustomize.exe
├── overlays
│ ├── dev
│ │ ├── env.file
│ │ ├── kustomization.yaml
│ │ └── replacement.yaml
│ ├── pre-prod
│ │ ├── kustomization.yaml
│ │ └── patch-shared-env.yaml
│ └── prod
│ ├── kustomization.yaml
│ ├── patch-env-from.yaml
│ ├── prod-1-env.yaml
│ └── prod-2-env.yaml
└── transformers
└── setProject
├── kustomization.yaml
└── setProject.yaml
8 directories, 14 files
- deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 3
selector:
matchLabels:
template:
metadata:
labels:
spec:
containers:
- name: test
image: alpine:3.12.12
imagePullPolicy: Always
command: [ "/bin/sh"]
args: ["-c","echo $(ALT_GREETING) $(ENABLE_RISKY) $SW_AGENT_TRACE_IGNORE_PATH; sleep 36000" ]
ports:
- containerPort: 8080
env:
- name: ALT_GREETING
valueFrom:
configMapKeyRef:
name: envinpod
key: ALT_GREETING
- name: ENABLE_RISKY
valueFrom:
configMapKeyRef:
name: envinpod
key: ENABLE_RISKY
- name: SW_AGENT_TRACE_IGNORE_PATH
valueFrom:
configMapKeyRef:
name: envinpod
key: SW_AGENT_TRACE_IGNORE_PATH
我们分别传入了三个环境变量
$(ALT_GREETING) $(ENABLE_RISKY) $SW_AGENT_TRACE_IGNORE_PATH
首先通过env.valueFrom.configMapKeyRef来进行传递。因为这是k8s configmapkeyref的常用方式
env:
- name: ALT_GREETING
valueFrom:
configMapKeyRef:
name: envinpod
key: ALT_GREETING
- name: ENABLE_RISKY
valueFrom:
configMapKeyRef:
name: envinpod
key: ENABLE_RISKY
- name: SW_AGENT_TRACE_IGNORE_PATH
valueFrom:
configMapKeyRef:
name: envinpod
key: SW_AGENT_TRACE_IGNORE_PATH
kustomization导入
[root@linuxea.com ~/kustomize/env-path]# cat base/kustomization.yaml
resources:
- deployment.yaml
而在overlays下的dev中,引用了这些环境变量
[root@linuxea.com ~/kustomize/env-path/overlays]# cat dev/env.file
ALT_GREETING=Hiya
ENABLE_RISKY=false
TEST-NAME=marksugar
SW_AGENT_TRACE_IGNORE_PATH=GET:/health,GET:/aggreg/health,/eureka/**,xxl-job/**
在kustomization中的配置如下
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# 名称空间
# namespace: test1
# 前缀
namePrefix: fat-
bases:
- ../../base
# configmap变量
configMapGenerator:
- name: envinpod
env: env.file
# 副本数
replicas:
- name: test
count: 5
# 标签
commonLabels:
app.kubernetes.io/name: nginx
app: mark
# 镜像
images:
- name: alpine
newTag: 3.12.12
其中关键的在于
configMapGenerator:
- name: envinpod
env: env.file
我们渲染下看
[root@linuxea.com ~/kustomize/env-path]# kustomize build overlays/dev/
apiVersion: v1
data:
ALT_GREETING: Hiya
ENABLE_RISKY: "false"
SW_AGENT_TRACE_IGNORE_PATH: GET:/health,GET:/aggreg/health,/eureka/**,xxl-job/**
TEST-NAME: marksugar
kind: ConfigMap
metadata:
labels:
app: mark
app.kubernetes.io/name: nginx
name: fat-envinpod-8hbm9d86m9
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mark
app.kubernetes.io/name: nginx
name: fat-test
spec:
replicas: 5
selector:
matchLabels:
app: mark
app.kubernetes.io/name: nginx
template:
metadata:
labels:
app: mark
app.kubernetes.io/name: nginx
spec:
containers:
- args:
- -c
- echo $(ALT_GREETING) $(ENABLE_RISKY) $SW_AGENT_TRACE_IGNORE_PATH; sleep
36000
command:
- /bin/sh
env:
- name: ALT_GREETING
valueFrom:
configMapKeyRef:
key: ALT_GREETING
name: fat-envinpod-8hbm9d86m9
- name: ENABLE_RISKY
valueFrom:
configMapKeyRef:
key: ENABLE_RISKY
name: fat-envinpod-8hbm9d86m9
- name: SW_AGENT_TRACE_IGNORE_PATH
valueFrom:
configMapKeyRef:
key: SW_AGENT_TRACE_IGNORE_PATH
name: fat-envinpod-8hbm9d86m9
image: alpine:3.12.12
imagePullPolicy: Always
name: test
ports:
- containerPort: 8080
而后run起来查看镜像的内容
你可以使用kubectl -k 或者如我这样使用
[root@linuxea.com ~/kustomize/env-path]# kustomize build overlays/dev/ | kubectl --kubeconfig /root/.kube/marksugar-dev-1 apply -f -
configmap/fat-envinpod-8hbm9d86m9 created
deployment.apps/fat-test created
run起来后直接查看日志,环境变量有没有被传入
[root@linuxea.com ~/kustomize/env-path]# kubectl --kubeconfig /root/.kube/marksugar-dev-1 logs -f fat-test-f9d967c4-vsc28
Hiya false GET:/health,GET:/aggreg/health,/eureka/**,xxl-job/**
除此之外使用envsubst 可以参考变量实值与文件变量替换
配置清单在应用之前必须先通过kustomize进行重新转换成k8s的原始格式文件
并且可以通过环境变量传递给kubectl,如下
env is=true kubectl apply -k
推荐阅读
Kustomize command to add environment variables to containers in a kustomizationDocument the environment variable substitution feature of kustomize configMapGeneratorCreate environment variables from env file with KustomizeUse ConfigMap-defined environment variables in Pod commands combineConfigskustomize-with-multiple-envsGenerating ResourcesKustomize Vars exampleIntroduce a ReplacementTransformer to replace the vars feature.#3492kustomize vars - enhance or replace?#2052Replacement poc#1631airshipctlkv.gohttps://github.com/kubernetes-sigs/kustomize/pull/3737/files#diff-c3d1278453f2a6fb229ec8998df0f109d8605b5e46ba2a84d067083f5a543761R194Using Kustomize for per-environment deployment of cert-manager resourcesHow To Manage Your Kubernetes Configurations with KustomizeIntroduce a ReplacementTransformer to replace the vars feature.#3492Kustomize PluginsChanging 'imagePullPolicy' of all containers in all deployments#1493valueAdd.mdkustomize-with-multiple-envs使用 Kustomize 对 Kubernetes 对象进行声明式管理Using system environment variables with KustomizeDemo: combining config data from devops and developers
https://github.com/kubernetes-sigs/kustomize/issues/2052https://github.com/kubernetes-sigs/kustomize/blob/master/examples/valueAdd.md