Kubernetes之使用ConfigMap配置Pod
<p> 官方参考:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/</p><h3> 创建ConfigMap </h3>
<p> 可以在 <code>kustomization.yaml</code> 中使用 <code>kubectl create configmap</code> 或 ConfigMap 生成器来创建ConfigMap。注意,从 1.14 版本开始, <code>kubectl</code> 开始支持 <code>kustomization.yaml</code>。</p>
<h3> 使用kubectl创建ConfigMap</h3>
<p> 在目录,文件或文字值中使用kubelet create configmap命令创建configmap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap <map-name> <data-source>
</pre>
</div>
<p> 其中, <map-name> 是要分配给 ConfigMap 的名称,<data-source> 是要从中提取数据的目录,文件或者文字值。</p>
<p> 数据源对应于 ConfigMap 中的 key-value (键值对)</p>
<ul>
<li> key=在命令行上题库的文件名或者秘钥</li>
<li> value=在命令行上提供的文件内容或者文字值</li>
</ul>
<p> 可以使用 kubelet describe或者kubelet get检索有关ConfigMap的信息</p>
<h3> 根据目录创建ConfigMap</h3>
<p> 你可以使用 <code>kubectl create configmap</code> 从同一目录中的多个文件创建 ConfigMap。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#创建本地目录
mkdir -p configure-pod-container/configmap/
#将样板文件下载到创建的目录
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
#创建configmap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/
</pre>
</div>
<p> 合并了以下两个文件的内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat configure-pod-container/configmap/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
# cat configure-pod-container/configmap/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
</pre>
</div>
<p> 进入以下ConfigMap中</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl describe configmaps game-config
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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
Events:<none>
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl get configmap game-config -o yaml
</pre>
</div>
<p> 输出以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
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
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-17T08:12:49Z"
name: game-config
namespace: default
resourceVersion: "505452"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: 1f3868e4-7a6b-4bf3-9317-00911ebc6e91
</pre>
</div>
<h3> 根据文件创建ConfigMap</h3>
<p> 可以使用kubelet create configmap从单个文件或多个文件创建ConfigMap</p>
<p> 例如</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
</pre>
</div>
<p> 将产生以下ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl describe configmaps game-config-2
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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>
</pre>
</div>
<p> 可以传入多个 <code>--from-file</code> 参数,从多个数据源创建 ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties
</pre>
</div>
<p> 描述上面创建的game-config-2 configmap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl describe configmaps game-config-2
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">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
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events:<none>
</pre>
</div>
<p> 使用 <code>--from-env-file</code> 选项从环境文件创建 ConfigMap,例如:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># 环境文件包含环境变量列表。
# 语法规则:
# env 文件中的每一行必须为 VAR = VAL 格式。
# 以#开头的行(即注释)将被忽略。
# 空行将被忽略。
# 引号没有特殊处理(即它们将成为 ConfigMap 值的一部分)。
# 将样本文件下载到 `configure-pod-container/configmap/` 目录
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
# env文件 `game-env-file.properties` 如下所示
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"
# 注释及其上方的空行将被忽略
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
</pre>
</div>
<p> </p>
<p> 将产生以下ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl get configmap game-config-env-file -o yaml
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-17T08:25:11Z"
name: game-config-env-file
namespace: default
resourceVersion: "507620"
selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
uid: f22b64e7-2232-4c6a-aa85-afbbafb6bcac
</pre>
</div>
<p> 当使用多个 <code>--from-env-file</code> 来从多个数据源创建 ConfigMap 时,仅仅最后一个 env 文件有效:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># 将样本文件下载到 `configure-pod-container/configmap/` 目录
wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
# 创建 configmap
kubectl create configmap config-multi-env-files \
--from-env-file=configure-pod-container/configmap/game-env-file.properties \
--from-env-file=configure-pod-container/configmap/ui-env-file.properties
</pre>
</div>
<p> 将产生以下ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl get configmap config-multi-env-files -o yaml
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
data:
color: purple
how: fairlyNice
textmode: "true"
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-17T08:31:36Z"
name: config-multi-env-files
namespace: default
resourceVersion: "508738"
selfLink: /api/v1/namespaces/default/configmaps/config-multi-env-files
uid: d09564b2-b683-455c-8360-423edd3dbbbf
</pre>
</div>
<h3> 定义从文件创建 ConfigMap时要使用自定义建名</h3>
<p> 您可以在使用 <code>--from-file</code> 参数时,在 ConfigMap 的 <code>data</code> 部分中定义除文件名以外的其他键:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file>
</pre>
</div>
<p> <code><my-key-name></code> 是您要在 ConfigMap 中使用的建名, <code><path-to-file></code> 是您想要键表示数据源文件的位置。</p>
<p> 例如</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties
</pre>
</div>
<p> 将产生以下ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
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
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-17T08:36:25Z"
name: game-config-3
namespace: default
resourceVersion: "509581"
selfLink: /api/v1/namespaces/default/configmaps/game-config-3
uid: a9ca7b2b-28d1-4fc2-ac13-48e7147fcf87
</pre>
</div>
<p> PS:使用文件创建的configmap默认的建名是文件名,以上自定义了其他建名不指定建名创建做对比</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"> kubectl create configmap game-config-3-2 --from-file=configure-pod-container/configmap/game.properties
</pre>
</div>
<p> <img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200317165042788-2048350511.png"></p>
<h3> 根据文字值生成ConfigMap</h3>
<p> 您可以将 <code>kubectl create configmap</code> 与 <code>--from-literal</code> 参数一起使用,从命令行定义文字值:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
</pre>
</div>
<p> 您可以传入多个键值对。命令行中提供的每对在 ConfigMap 的 <code>data</code> 部分中均表示为单独的条目。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl get configmaps special-config -o yaml
</pre>
</div>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-17T08:54:43Z"
name: special-config
namespace: default
resourceVersion: "512776"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: b7f972bf-1eef-4c74-b496-8b90cf3476d2
</pre>
</div>
<p> 根据生成器创建ConfigMap</p>
<p> 自 1.14 开始, <code>kubectl</code> 开始支持 <code>kustomization.yaml</code>。 您还可以从生成器创建 ConfigMap,然后将其应用于 Apiserver 创建对象。生成器应在目录内的 <code>kustomization.yaml</code> 中指定。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># 使用 ConfigMapGenerator 创建 kustomization.yaml 文件
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
files:
- configure-pod-container/configmap/kubectl/game.properties
EOF
</pre>
</div>
<p> 使用 kustomization 目录创建 ConfigMap 对象</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl apply -k .
configmap/game-config-4-m9dm2f92bt created
</pre>
</div>
<p> PS:文件kustomization.yaml需要与文件夹configure-pod-container在同一个目录并且该文件夹下面没有其他文件</p>
<p> 可以检查ConfigMap是这样创建的</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl describe configmaps/game-config-4-m9dm2f92bt
Name: game-config-4-m9dm2f92bt
Namespace: default
Labels: <none>
Annotations:kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.p...
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>
</pre>
</div>
<p> 请注意,生成的 ConfigMap 名称具有通过对内容进行散列而附加的后缀,这样可以确保每次修改内容时都会生成新的 ConfigMap。</p>
<h3> 定义从文件生成ConfigMap是要使用建名</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat kustomization.yaml
configMapGenerator:
- name: game-config-5
files:
- game-special-key=configure-pod-container/configmap/kubectl/game.properties
</pre>
</div>
<p> 使用 Kustomization 目录创建 ConfigMap 对象</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -k .
configmap/game-config-5-m67dt67794 created
</pre>
</div>
<p> 对比</p>
<p><img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200317172339184-2002217084.png"></p>
<p> </p>
<h3> 从文字生成CofigMap</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#cat kustomization.yaml
configMapGenerator:
- name: special-config-2
literals:
- special.how=very
- special.type=charm
</pre>
</div>
<p> 使用 Kustomization 目录创建 ConfigMap 对象。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -k .
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl describe configmap special-config-2-c92b5mmcf2
</pre>
</div>
<p> </p>
<p> 输出类似以下内容</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">Name: special-config-2-c92b5mmcf2
Namespace: default
Labels: <none>
Annotations:kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","data":{"special.how":"very","special.type":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-co...
Data
====
special.how:
----
very
special.type:
----
charm
Events:<none>
</pre>
</div>
<h3> 使用 ConfigMap 数据定义容器环境变量</h3>
<p> 1.在ConfigMap中将环境变量定义为键值对</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl create configmap special-config --from-literal=special.how=very
</pre>
</div>
<p> 查看该键值对</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl get configmap special-config -o yaml
apiVersion: v1
data:
special.how: very
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-18T01:47:01Z"
name: special-config
namespace: default
resourceVersion: "689548"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: 89b580f2-714d-4e47-87f6-90d18aa9aa3d
</pre>
</div>
<p> 将 ConfigMap 中定义的 <code>special.how</code> 值分配给 Pod 规范中的 <code>SPECIAL_LEVEL_KEY</code> 环境变量。</p>
<p> 下载示例Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">wget https://kubernetes.io/examples/pods/pod-single-configmap-env-variable.yaml
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
# 定义容器内环境变量变量名为SPECIAL_LEVEL_KEY
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
# 包含键值映射的configmap为special-config
name: special-config
# Specify the key associated with the value
# 对应的key为special.how该key对应的值为very
key: special.how
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-single-configmap-env-variable.yaml
</pre>
</div>
<p> 该Pod运行日志输出环境变量,查看日志</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl logs dapi-test-pod
KUBERNETES_PORT=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT=443
MY_SERVICE_PORT_80_TCP=tcp://10.0.0.47:80
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
MY_SERVICE_SERVICE_PORT_HTTP=80
MY_SERVICE_SERVICE_HOST=10.0.0.47
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MY_SERVICE_SERVICE_PORT=80
MY_SERVICE_PORT=tcp://10.0.0.47:80
SPECIAL_LEVEL_KEY=very
MY_SERVICE_PORT_80_TCP_ADDR=10.0.0.47
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
MY_SERVICE_PORT_80_TCP_PORT=80
KUBERNETES_SERVICE_HOST=10.0.0.1
PWD=/
MY_SERVICE_PORT_80_TCP_PROTO=tcp
</pre>
</div>
<p> <img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200318100040041-1235890591.png"></p>
<p> </p>
<p> 该Pod执行完输出环境变量以后因为重启策略是Never就是完成状态了</p>
<p><img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200318100217746-2098894353.png"></p>
<p> </p>
<h3> 使用多个ConfigMap定义容器变量</h3>
<p> 创建ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat configmaps.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
</pre>
</div>
<p> 创建</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f configmaps.yaml
</pre>
</div>
<p> 在Pod中定义环境变量</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-multiple-configmap-env-variable.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: env-config
key: log_level
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-multiple-configmap-env-variable.yaml
</pre>
</div>
<p> 现在,Pod 的输出包含环境变量 <code>SPECIAL_LEVEL_KEY=very</code> 和 <code>LOG_LEVEL=INFO</code>。</p>
<p><img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200318100809506-1422555035.png"></p>
<p> </p>
<h3> 将 ConfigMap 中的所有键值对配置为容器环境变量</h3>
<p> 注意:Kubernetes v1.16和更高版本提供此功能</p>
<p> 创建一个包含多个键值对的ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat configmap-multikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
</pre>
</div>
<p> 创建ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f configmap-multikeys.yaml
</pre>
</div>
<p> 查看创建的ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl get configmap special-config -o yaml
apiVersion: v1
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
kind: ConfigMap
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","data":{"SPECIAL_LEVEL":"very","SPECIAL_TYPE":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-config","namespace":"default"}}
creationTimestamp: "2020-03-18T02:13:48Z"
name: special-config
namespace: default
resourceVersion: "694299"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: 6df7c234-7ec6-4fad-8c5e-c34aa32f948a
</pre>
</div>
<p> 使用 <code>envFrom</code> 将所有 ConfigMap 的数据定义为容器环境变量,ConfigMap 中的键成为 Pod 中的环境变量名称。 </p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-envFrom.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-configmap-envFrom.yaml
</pre>
</div>
<p> 现在,Pod 的输出包含环境变量 <code>SPECIAL_LEVEL=very</code> 和 <code>SPECIAL_TYPE=charm</code>。</p>
<p><img src="https://img2020.cnblogs.com/i-beta/1144139/202003/1144139-20200318102007994-1566281962.png"></p>
<p> </p>
<h3> 在 Pod 命令中使用 ConfigMap 定义的环境变量</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-env-var-valueFrom.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_LEVEL
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_TYPE
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-configmap-env-var-valueFrom.yaml
</pre>
</div>
<p> 在 <code>test-container</code> 容器中产生以下输出:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl logs dapi-test-pod
very charm
</pre>
</div>
<h3> 将ConfigMap数据添加到一个容器中</h3>
<p> 当您使用 <code>--from-file</code> 创建 ConfigMap 时,文件名成为存储在 ConfigMap 的 <code>data</code> 部分中的key,文件内容成为key的值。</p>
<p> 本节中的示例引用了一个名为 special-config 的 ConfigMap,如下所示:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat configmap-multikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
</pre>
</div>
<p> 创建ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f configmap-multikeys.yaml
</pre>
</div>
<h3> 使用存储在 ConfigMap 中的数据填充容器</h3>
<p> 在 Pod 规范的 <code>volumes</code> 部分下添加 ConfigMap 名称。 这会将 ConfigMap 数据添加到指定为 <code>volumeMounts.mountPath</code> 的目录(在本例中为<code>/etc/config</code>)。 <code>command</code> 引用存储在 ConfigMap 中的 <code>special.level</code>。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: special-config
restartPolicy: Never
</pre>
</div>
<p> </p>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-configmap-volume.yaml
</pre>
</div>
<p> 容器运行命令 <code>ls /etc/config/</code> 产生下面的输出:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl logs dapi-test-pod
SPECIAL_LEVEL
SPECIAL_TYPE
</pre>
</div>
<p> 注意:</p>
<p> 如果在/etc/config/目录中有一些文件,他们将被删除</p>
<p> 这里ls显示的其实是两个文件名称</p>
<p> 可以修改pod-configmap-volume.yaml增加一个sleep这样启动Pod就不会运行完马上退出</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "ls /etc/config/ && sleep 3600" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: special-config
restartPolicy: Never
</pre>
</div>
<p> 登录Pod查看</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl exec -it dapi-test-pod sh
# cd /etc/config/
# ls -l
total 0
lrwxrwxrwx 1 root root 20 Mar 18 09:09 SPECIAL_LEVEL -> ..data/SPECIAL_LEVEL
lrwxrwxrwx 1 root root 19 Mar 18 09:09 SPECIAL_TYPE -> ..data/SPECIAL_TYPE
#文件内容就是very和charm
# cat SPECIAL_LEVEL
very
# cat SPECIAL_TYPE
charm
</pre>
</div>
<p> </p>
<h3> 将 ConfigMap 数据添加到容器中的特定路径</h3>
<p> 使用 <code>path</code> 字段为特定的 ConfigMap 项目指定所需的文件路径。 在这种情况下, <code>SPECIAL_LEVEL</code> 将安装在 <code>/etc/config/keys</code> 目录下的 <code>config-volume</code> 容器中。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-volume-specific-key.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/keys" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: SPECIAL_LEVEL
path: keys
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl apply -f pod-configmap-volume-specific-key.yaml
</pre>
</div>
<p> 当 pod 运行时,命令 <code>cat /etc/config/keys</code> 产生以下输出</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl logs dapi-test-pod
very
</pre>
</div>
<p> 为什么输出是very 修改yaml文件注释</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat pod-configmap-volume-specific-key.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
#增加sleep防止运行完以后Pod处于compled状态
command: [ "/bin/sh","-c","cat /etc/config/keys && sleep 3600 " ]
volumeMounts:
#挂载对应的名称是volumes对应的config-volume
- name: config-volume
#挂载的目录是容器内目录/etc/config
#以keys名挂载以后的文件名称是/etc/config/keys
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
#取ConfigMap的key为SPECIAL_LEVE的值是very
- key: SPECIAL_LEVEL
#在volumeMount是挂载的名称是keys
path: keys
restartPolicy: Never
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl apply -f pod-configmap-volume-specific-key.yaml
pod/dapi-test-pod created
</pre>
</div>
<p> 登录Pod内部查看</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl exec -it dapi-test-pod sh
cd /etc/config/
ls
keys
cat keys
very
</pre>
</div>
<p> </p>
<h3> 了解ConfigMap和Pod</h3>
<p> ConfigMap API 资源将配置数据存储为键值对。数据可以在 Pod 中使用,也可以提供系统组件(如控制器)的配置。ConfigMap 与 Secrets类似,但是提供了一种使用不包含敏感信息的字符串的方法。用户和系统组件都可以在 ConfigMap 中存储配置数据。</p>
<p> ConfigMap 应该引用属性文件,而不是替换它们。可以将 ConfigMap 表示为类似于 Linux <code>/etc</code> 目录及其内容的东西。例如,如果您从 ConfigMap 创建Kubernetes Volume,则 ConfigMap 中的每个数据项都由该容器中的单个文件表示。</p>
<p> ConfigMap 的 <code>data</code> 字段包含配置数据。如下例所示,它可以很简单 – 就像使用 <code>--from-literal</code> – 定义的单个属性一样,也可以很复杂 – 例如使用 <code>--from-file</code> 定义的配置文件或 JSON blob。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: example-config
namespace: default
data:
# example of a simple property defined using --from-literal
example.property.1: hello
example.property.2: world
# example of a complex property defined using --from-file
example.property.file: |-
property.1=value-1
property.2=value-2
property.3=value-3
</pre>
</div>
<h3> 限制规定</h3>
<p> 在 Pod 规范中引用它之前,必须先创建一个 ConfigMap(除非将 ConfigMap 标记为”可选”)。如果引用的 ConfigMap 不存在,则 Pod 将不会启动。同样,对 ConfigMap 中不存在的键的引用将阻止容器启动。</p>
<p> 如果您使用 <code>envFrom</code> 从 ConfigMap 中定义环境变量,那么将忽略被认为无效的键。可以启动 Pod,但无效名称将记录在事件日志中(InvalidVariableNames)。日志消息列出了每个跳过的键。例如:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl get events
</pre>
</div>
<p> 如果没有key将事件中出现类似以下提示</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">15m Warning Failed pod/dapi-test-pod Error: couldn't find key log_level in ConfigMap default/special-config
</pre>
</div>
<p> ConfigMap 只能由位于相同命令空间中的 Pod 引用。</p>
<p> Kubelet 不支持将 ConfigMap 用于未在 API 服务器上找到的 Pod。这包括通过 Kubelet 的 <code>--manifest-url</code> 参数,<code>--config</code> 参数或者 Kubelet REST API 创建的容器。</p>
<h3> 使用ConfigMap来配置Redis</h3>
<p> 目标</p>
<p> 创建一个包含以下内容的<code>kustomization.yaml</code> 文件</p>
<ul>
<li>一个ConfigMap生成器</li>
<li>一个使用ConfigMap的Pod资源配置</li>
</ul>
<p> 使用kubectl apply -k ./应用整个路径的配置</p>
<p> 验证配置是否正确</p>
<p> 使用<code>kustomization.yaml</code> Kubernetes版本必须1.14及以上 查看版本信息使用命令</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl version
</pre>
</div>
<p> 按照以下步骤,可以使用ConfigMap中的数据类配置Redis缓存</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">curl -OL https://k8s.io/examples/pods/config/redis-config
#文件内容如下
maxmemory 2mb
maxmemory-policy allkeys-lru
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: example-redis-config
files:
- redis-config
EOF
</pre>
</div>
<p> 将Pod的资源配置添加到<code>kustomization.yaml</code> 文件中</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">curl -OL https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/config/redis-pod.yaml
</pre>
</div>
<p> 在kustomization.yaml追加</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">cat <<EOF >>./kustomization.yaml
resources:
- redis-pod.yaml
EOF
</pre>
</div>
<p> 此时完整的kustomization.yaml内容如下</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat kustomization.yaml
configMapGenerator:
- name: example-redis-config
files:
- redis-config
resources:
- redis-pod.yaml
</pre>
</div>
<p> 应用整个 kustomization 文件夹以创建 ConfigMap 和 Pod 对象:</p>
<p> 首先应用文件redis-config文件创建ConfigMap,然后使用文件redis-pod.yaml创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl apply -k .
configmap/example-redis-config-dgh9dg555m created
pod/redis created
</pre>
</div>
<p> 使用以下命令检查创建的对象</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl get -k .
NAME DATA AGE
configmap/example-redis-config-dgh9dg555m 1 36s
NAME READY STATUS RESTARTS AGE
pod/redis 1/1 Running 0 36s
</pre>
</div>
<p> 在示例中,配置卷挂载在/redis-master下。它使用path将redis-config的key添加到名为redis.conf的文件中。因此,redis配置的文件路径为<code>/redis-master/redis.conf</code>。 这是镜像将在其中查找 redis master 的配置文件的位置。</p>
<p> 使用kubectl exec进入Pod并运行redis-cli工具来验证配置已正确应用</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl exec -it redis redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"
</pre>
</div>
<p> PS:redis使用命令CONFIG GET 参数获取对应配置值 这里最大内存设置的是2mb换算成byte就是2097152即2*1024*1024</p>
<p> 删除创建的Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl delete pod redis
</pre>
</div>
<p> 不使用kustomization.yaml创建一遍</p>
<p> 删除刚刚创建的ConfigMap和Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">kubectl delete pod redis
kubectl delete configmap example-redis-config-dgh9dg555m
</pre>
</div>
<p> 使用文件创建ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#创建ConfigMap名为example-redis-config
kubectl create configmap example-redis-config --from-file=redis-config
configmap/example-redis-config created
</pre>
</div>
<p> 查看刚刚创建的ConfigMap</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#key为文件名redis-config内容及文件内部内容
kubectl get configmapexample-redis-config -o yaml
apiVersion: v1
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
creationTimestamp: "2020-03-18T06:06:54Z"
name: example-redis-config
namespace: default
resourceVersion: "735256"
selfLink: /api/v1/namespaces/default/configmaps/example-redis-config
uid: 35f4baf9-b786-450f-a22c-768fa75a2d08
</pre>
</div>
<p> 创建Pod</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># cat redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
#挂载配置文件从ConfigMap挂载目录为容器目录/redis-master最终挂载的文件为/redis-master/redis.conf
#内容即文件redis-config内内容
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
</pre>
</div>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl apply -f redis-pod.yaml
pod/redis created
</pre>
</div>
<p> 可以使用以上redis-cli验证</p>
<p> 也可以登录Pod查看配置文件内容是否一致</p>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;"># kubectl exec -it redis bash
# cat /redis-master/redis.conf
maxmemory 2mb
maxmemory-policy allkeys-lru
</pre>
</div>
<p> </p>
<p><br><br></p>
<p> </p>
<p> </p>
<h3> </h3>
<p> </p>
<p> </p>
<p> </p>
<p> </p><br><br>
来源:https://www.cnblogs.com/minseo/p/12512203.html
頁:
[1]