ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也可以用来保存整个配置文件或者 JSON 二进制对象
[root@k8s-master01 dir]# ls
game.propertie ui.propertie
[root@k8s-master01 dir]# cat game.propertie
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
[root@k8s-master01 dir]# cat ui.propertie
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
#从目录创建configmap
#—from-file指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容
[root@k8s-master01 dir]# kubectl create configmap game-config --from-file=./
configmap/game-config created
[root@k8s-master01 dir]# kubectl get configmap
NAME DATA AGE
game-config 2 14s
[root@k8s-master01 dir]# kubectl get cm
NAME DATA AGE
game-config 2 18s
[root@k8s-master01 dir]# kubectl get cm game-config -o yaml
apiVersion: v1
data:
game.propertie: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.propertie: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: "2020-02-04T05:13:30Z"
name: game-config
namespace: default
resourceVersion: "144337"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: 0fc2df14-d5ee-4092-ba9e-cc733e8d5893
[root@k8s-master01 dir]# kubectl describe cm game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.propertie:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.propertie:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events: <none>只要指定为一个文件就可以从单个文件中创建 ConfigMap
—from-file这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的
[root@k8s-master01 dir]# kubectl create configmap game-config-2 --from-file=./game.propertie
configmap/game-config-2 created
[root@k8s-master01 dir]# kubectl get cm game-config-2
NAME DATA AGE
game-config-2 1 16s
[root@k8s-master01 dir]# kubectl get cm game-config-2 -o yaml
apiVersion: v1
data:
game.propertie: |
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-02-04T05:25:13Z"
name: game-config-2
namespace: default
resourceVersion: "145449"
selfLink: /api/v1/namespaces/default/configmaps/game-config-2
uid: df209574-202d-4564-95ca-19532abd6b7b使用文字值创建,利用—from-literal参数传递配置信息,该参数可以使用多次
[root@k8s-master01 dir]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm configmap/special-config created [root@k8s-master01 dir]# kubectl get cm special-config -o yaml apiVersion: v1 data: special.how: very special.type: charm kind: ConfigMap metadata: creationTimestamp: "2020-02-04T05:27:43Z" name: special-config namespace: default resourceVersion: "145688" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: 72b58e0e-f055-43dd-aa04-a7b2e9007227
#创建config文件
[root@k8s-master01 usage]# cat config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
[root@k8s-master01 usage]# kubectl apply -f config.yaml
configmap/special-config created
configmap/env-config created
#将ConfigMap文件注入到pod环境中
[root@k8s-master01 usage]# cat dapi-test.pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.dianchou.com/library/myapp:v1
command: ["/bin/sh","-c","env"]
env: #第一种导入方案
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config #从哪个configMap导入
key: special.how #导入的是键名,就是将special.how键的键值赋予SPECIAL_LEVEL_KEY
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom: #第二种导入方案
- configMapRef:
name: env-config
restartPolicy: Never
[root@k8s-master01 usage]# kubectl create -f dapi-test.pod.yaml
pod/dapi-test-pod created
[root@k8s-master01 usage]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod 0/1 Completed 0 4s
#查看环境变量
[root@k8s-master01 usage]# kubectl logs dapi-test-pod
MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
MYAPP_SVC_PORT_80_TCP_PORT=80
HOSTNAME=dapi-test-pod
SHLVL=1
MYAPP_SVC_PORT_80_TCP_PROTO=tcp
HOME=/root
SPECIAL_TYPE_KEY=charm
MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80
NGINX_VERSION=1.12.2
KUBERNETES_PORT_443_TCP_ADDR=10.96.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
MYAPP_SVC_SERVICE_HOST=10.98.57.156
SPECIAL_LEVEL_KEY=very
log_level=INFO
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
MYAPP_SVC_SERVICE_PORT=80
MYAPP_SVC_PORT=tcp://10.98.57.156:80
[root@k8s-master01 usage]# cat dapi-test-pod-command.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod2
spec:
containers:
- name: test-container-command
image: hub.dianchou.com/library/myapp:v1
command: ["/bin/sh","-c","echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"]
env: #第一种导入方案
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config #从哪个configMap导入
key: special.how #导入的是键名,就是将special.how键的键值赋予SPECIAL_LEVEL_KEY
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
[root@k8s-master01 usage]# kubectl create -f dapi-test-pod-command.yaml
configmap/special-config created
pod/dapi-test-pod2 created
[root@k8s-master01 usage]# kubectl get cm
NAME DATA AGE
special-config 2 4s
[root@k8s-master01 usage]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod2 0/1 Completed 0 10s
[root@k8s-master01 usage]# kubectl logs dapi-test-pod2
very charm在数据卷里面使用这个 ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容
[root@k8s-master01 usage]# cat dapi-test-pod-volume.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod3
spec:
containers:
- name: test-container
image: hub.dianchou.com/library/myapp:v1
command: ["/bin/sh","-c","sleep 600s"]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
[root@k8s-master01 usage]# kubectl create -f dapi-test-pod-volume.yaml
configmap/special-config created
pod/dapi-test-pod3 created
[root@k8s-master01 usage]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dapi-test-pod3 1/1 Running 0 6s
[root@k8s-master01 usage]# kubectl exec dapi-test-pod3 -it -- /bin/sh
/ # cd /etc/config
/etc/config # ls
special.how special.type
/etc/config # cat special.how
/etc/config # cat special.type
charm/etc/config # kubernetes(五)--存储之configmap/secret/volume/Persistent Volume
原文:https://www.cnblogs.com/hujinzhong/p/12258966.html