当前位置:首页 > Web开发 > 正文

即如下容器的/etc/config目录

2024-03-31 Web开发

ConfigMaps可以使容器镜像与配置文件解耦,实现容器化应用措施的可移植性。此文供给一系列的要领示例讲述如何创建ConfigMaps,使用存储在ConfigMaps中的数据配置Pod。

备注:此文档参考官方文档,并加以本身的理解。如有误导性的内容,请攻讦斧正。

创建一个ConfigMap

我们可以使用kubectl create configmap或kustomization.yaml中的ConfigMap生成器创建一个ConfigMap。从Kubernetes 1.14版本开始,,kubectl开始撑持使用kustomization.yaml创建ConfigMap。

使用 kubectl create configmap 创建一个 ConfigMap

使用kubectl create configmap从目录、文件或字面值创建configmaps。

kubectl create configmap <map-name> <data-source>

<map-name>即配置的ConfigMap的名字,<data-source>是从目录、文件或字面值下读取数据的目录。

ConfigMap中的data-source应该是一个键值对:

key=文件名称或供给的命令行的key

value=提命令行中供给的文件内容或字面值

使用kubectl describe或kubectl get检察ConfigMap的信息。

从目录中创建 ConfigMaps

可以使用kubectl create configmap从同一个目录下的多个文件创建ConfigMap

# Create the local directory mkdir -p configure-pod-container/configmap/ # Download the sample files into `configure-pod-container/configmap/` directory wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties # Create the configmap kubectl create configmap game-config --from-file=configure-pod-container/configmap/

configure-pod-container/configmap/目录下包罗连个文件

game.properties ui.properties

检察配置的ConfigMap内容:

kubectl describe configmaps game-config

从功效可以看出:ConfigMap中的data块中包罗了configure-pod-container/configmap/目录下game.properties和ui.properties两个文件中的所有内容。

Name: game-config Namespace: default Labels: <none> Annotations: <none> Data ==== game-env-file.properties: ---- enemies=aliens lives=3 allowed="true" # This comment and the empty line above it are ignored 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 Events: <none>

以yaml格局输出如下:

# kubectl get configmaps game-config -o yaml apiVersion: v1 data: game-env-file.properties: | enemies=aliens lives=3 allowed="true" # This comment and the empty line above it are ignored 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 kind: ConfigMap metadata: creationTimestamp: "2020-01-17T07:18:08Z" name: game-config namespace: default resourceVersion: "10100181" selfLink: /api/v1/namespaces/default/configmaps/game-config uid: a447f523-ddf4-48f5-a0eb-6f24c732fee5 从文件中创建 ConfigMaps

使用kubectl create configmap从一个独立的文件或多个文件中创建ConfigMap。

例如:

# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties

检察内容:

# kubectl describe configmaps game-config-2

输出如下所示

Name: game-config-2 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 Events: <none>

也可以通过多次使用--from-file参数从多个数据源中创建ConfigMap

例如:

# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties

使用--from-env-file选项从env-file中创建ConfigMap:

例如:

# Env-files contain a list of environment variables. # These syntax rules apply: # Each line in an env file has to be in VAR=VAL format. # Lines beginning with # (i.e. comments) are ignored. # Blank lines are ignored. # There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)). # Download the sample files into `configure-pod-container/configmap/` directory wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties # The env-file `game-env-file.properties` looks like below cat configure-pod-container/configmap/game-env-file.properties enemies=aliens lives=3 allowed="true" # This comment and the empty line above it are ignored

创建ConfigMap

kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties

检察内容

# kubectl get configmap game-config-env-file -o yaml

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31151.html