介绍
ConfigMap 是 Kubernetes 中的一种资源对象,用于存储非敏感的配置数据,例如键值对、环境变量、配置文件等。它提供了一种将配置数据与应用程序解耦的方式,使得应用程序可以更灵活地部署和管理。
- 注意:ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1 MiB。如果你需要保存超出此尺寸限制的数据,可以考虑挂载存储卷 或者使用独立的数据库或者文件服务。
ConfigMaps
这是一个 ConfigMap 的示例,它的一些键只有一个值,其他键的值看起来像是 配置的片段格式
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo # ConfigMap 文件名字
data:
# 类属性键;每一个键都映射到一个简单的值
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"
# 类文件键 这个会创建两个文件,文件名字为 game.properties 和 user-interface.properties 文件内容如下显示。
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
创建 ConfigMap
基于一个目录来创建 ConfigMap
操作方法
# 创建本地目录
mkdir configmap
# 将示例文件下载到 `configure-pod-container/configmap/` 目录
wget https://kubernetes.io/examples/configmap/game.properties -O configmap/game.properties --no-check-certificate
wget https://kubernetes.io/examples/configmap/ui.properties -O configmap/ui.properties --no-check-certificate
# 创建ConfigMap
kubectl create configmap game-config --from-file=configmap/
查看创建的ConfigMap
kubectl get cm
NAME DATA AGE
game-config 2 13s
或者
kubectl describe cm
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
BinaryData
====
Events: <none>
基于文件创建 ConfigMap
使用 kubectl create configmap 基于单个文件或多个文件创建 ConfigMap
# 基于单个文件
kubectl create configmap game-config-2 --from-file=configmap/game.properties
# 也可以多次使用 --from-file 参数,从多个数据源创建 ConfigMap
kubectl create configmap game-config-2 --from-file=configmap/game.properties --from-file=configmap/ui.properties
定义从文件创建 ConfigMap 时要使用的键
在使用 --from-file 参数时,你可以定义在 ConfigMap 的 data 部分出现键名, 而不是按默认行为使用文件名:
# kubectl create configmap game-config-3 --from-file=<我的键名>=<文件路径>
kubectl create configmap game-config-3 --from-file=game-special-key=configmap/game.properties
查看创建的ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: game-config-3
namespace: default
data:
game-special-key: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
使用 --from-env-file 选项基于 env 文件创建 ConfigMap
# Env 文件包含环境变量列表。其中适用以下语法规则:
# 这些语法规则适用:
# Env 文件中的每一行必须为 VAR=VAL 格式。
# 以#开头的行(即注释)将被忽略。
# 空行将被忽略。
# 引号不会被特殊处理(即它们将成为 ConfigMap 值的一部分)。
# 将示例文件下载到 `configmap/` 目录
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configmap/game-env-file.properties --no-check-certificate
wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configmap/ui-env-file.properties --no-check-certificate
# Env 文件 `game-env-file.properties` 如下所示
cat configure-pod-container/configmap/game-env-file.properties

本文详细介绍了Kubernetes中的ConfigMap资源对象,包括其用途、数据存储限制、创建方式(基于文件、env文件和可选键值)、使用场景(环境变量、容器配置和不可变性)以及与之相关的最佳实践。
最低0.47元/天 解锁文章
4787

被折叠的 条评论
为什么被折叠?



