将 Kubernetes ConfigMap 数据字段分离到单独的文件中
Separate Kubernetes ConfigMap data fields into individual files
我在玩 kubernetes ConfigMaps。在 official documentation 中,我在 data
字段中看到“类似文件的键”:
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# file-like keys
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
是否可以将这些“类文件键”分解成不同的文件并在此 ConfigMap 资源中引用它们?
我看到了这种方法的几个好处:
- 精简了 ConfigMap
- “类文件”配置的正确语法突出显示
- 可以 运行 自动格式化程序反对“类文件”配置
- Proper syntax highlighting for the "file-like" configurations
- Can run auto formatters against the "file-like" configurations
是的,在您的机器上和 Git 中将文件保存为正确的文件更容易。
我建议您使用 kustomize feature of kubectl and use configMapGenerator 来生成 ConfigMap。
示例 kustomization.yaml
(保存在与您的文件相同的目录中,例如 config/
)
configMapGenerator:
- name: game-demo
files:
- game.properties
- user-interface.properties
然后您可以应用(并生成 configMap)(如果您的配置在 config/
中):
kubectl -k config/
或者您可以使用以下方法预览“生成的”configMap:
kubectl kustomize config/
我在玩 kubernetes ConfigMaps。在 official documentation 中,我在 data
字段中看到“类似文件的键”:
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# file-like keys
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
是否可以将这些“类文件键”分解成不同的文件并在此 ConfigMap 资源中引用它们?
我看到了这种方法的几个好处:
- 精简了 ConfigMap
- “类文件”配置的正确语法突出显示
- 可以 运行 自动格式化程序反对“类文件”配置
- Proper syntax highlighting for the "file-like" configurations
- Can run auto formatters against the "file-like" configurations
是的,在您的机器上和 Git 中将文件保存为正确的文件更容易。
我建议您使用 kustomize feature of kubectl and use configMapGenerator 来生成 ConfigMap。
示例 kustomization.yaml
(保存在与您的文件相同的目录中,例如 config/
)
configMapGenerator:
- name: game-demo
files:
- game.properties
- user-interface.properties
然后您可以应用(并生成 configMap)(如果您的配置在 config/
中):
kubectl -k config/
或者您可以使用以下方法预览“生成的”configMap:
kubectl kustomize config/