Kubernetes入门实验:configmap

作者注:本文仅为笔者学习记录,不具任何参考意义。

k8s configmap 实验。
注:本文为笔者实验记录,非教程,另会不定时更新。

环境

1
2
3
4
5
# kubectl get node
NAME STATUS ROLES AGE VERSION
edge-node Ready <none> 15m v1.17.0
edge-node2 Ready <none> 16m v1.17.0
ubuntu Ready master 67d v1.17.0

configmap

技术总结

测试要点:

  • 多方式创建,命令行指定,目录,(多)文件。
  • 使用,传递到pod,多个cm。
  • 更新cm。

简写:kubectl create cm

命令行指定

1
kubectl create configmap my-cm --from-literal=who=latelee --from-literal=how.many=250

从目录创建

1
2
3
4
5
wget https://kubernetes.io/examples/configmap/game.properties -O game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O ui.properties

# Create the configmap
kubectl create configmap game-config --from-file=. // 注!当前目录使用"."

输出yaml格式:

1
kubectl get configmaps game-config -o yaml

整理后config.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: ConfigMap
metadata:
name: game-config
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

可执行:

1
kubectl apply -f config.yaml

使用ConfigMapGenerator创建:

1
2
3
4
5
6
7
8
9
cat <<EOF > ./kustomization.yaml
configMapGenerator:
- name: game-config-4
files:
- game.properties # 这里指定文件
- ui.properties
EOF

kubectl apply -k . // ! 注意格式,不能指定kustomization.yaml文件

注:这种格式得到的创建cm带有随机数。

从json文件创建

deviceProfile.json 文件:

1
{"deviceInstances":[{"id":"led-light-instance-01","name":"led-light-instance-01","model":"led-light"}],"deviceModels":[{"name":"led-light","properties":[{"name":"power-status","dataType":"string","description":"Indicates whether the led light is ON/OFF","accessMode":"ReadWrite","defaultValue":"OFF"},{"name":"gpio-pin-number","dataType":"int","description":"Indicates whether the GPIO pin to which LED is connected","accessMode":"ReadOnly","defaultValue":18}]}],"protocols":[{"protocol_config":null}]}

创建:

1
kubectl create configmap led-config --from-file=deviceProfile.json

在deployment中使用示例:

1
2
3
4
5
6
7
volumeMounts:
- name: config-volume
mountPath: /opt/kubeedge/ # 挂载到容器的目录
volumes:
- name: config-volume
configMap:
name: led-config # 这是前面创建的configmap名称

此设置下,容器中存在 /opt/kubeedge/deviceProfile.json 。即挂载同名的json文件。json文件内容无须格式化。

参数传递

指定某几种变量。
pod配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat <<EOF > busybox-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600" ]
env:
- name: SPECIAL_WHO
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: my-cm
# Specify the key associated with the value
key: who
- name: SPECIAL_NUM
valueFrom:
configMapKeyRef:
name: my-cm
key: how.many
- name: SPECIAL_HOW
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
EOF

创建:

1
kubectl apply -f busybox-pod.yaml

验证:

1
2
3
4
5
kubectl exec -it busybox env
HOSTNAME=busybox
TERM=xterm
SPECIAL_WHO=latelee
SPECIAL_NUM=250

configmap的全部变量。

1
2
3
4
5
6
7
8
9
10
11
cat <<EOF > configmap-multikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config1
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
special.how: very
EOF

pod配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat <<EOF > pod-busybox-envFrom.yaml
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600" ]
envFrom:
- configMapRef:
name: special-config1
restartPolicy: Never
EOF

创建并测试:

1
2
3
4
5
6
7
# kubectl apply -f configmap-multikeys.yaml
# kubectl apply -f pod-busybox-envFrom.yaml

# kubectl exec -it busybox env
special.how=very
SPECIAL_LEVEL=very
SPECIAL_TYPE=charm

挂载示例pod-busybox-volume.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600" ]
volumeMounts:
- name: config-volume
mountPath: /etc/myconfig
volumes:
- name: config-volume
configMap:
name: special-config1
nodeSelector:
ntype: hello
restartPolicy: Never

添加节点的标签

1
2
3
kubectl label nodes edge-node2 ntype=hello

kubectl create -f pod-busybox-volume.yaml

查询结果:

1
2
3
4
# kubectl exec -it busybox ls /etc/myconfig
SPECIAL_LEVEL SPECIAL_TYPE special.how
# kubectl exec -it busybox cat /etc/myconfig/SPECIAL_LEVEL
very

结论:将key作为文件存在,value为该文件的值。

单个值传递pod-busybox-volume1.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: latelee/busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600" ]
volumeMounts:
- name: config-volume
mountPath: /etc/myconfig
volumes:
- name: config-volume
configMap:
name: special-config1
items:
- key: SPECIAL_LEVEL
path: keys
nodeSelector:
ntype: hello
restartPolicy: Never

查询结果:

1
2
3
4
# kubectl exec -it busybox ls /etc/myconfig
keys
# kubectl exec -it busybox cat /etc/myconfig/keys
very

结论:与上不同,此处只指定其中某一个或多个变量。

自动更新(热更新)

实时修改configmap的值:

1
# kubectl edit cm special-config1 

进入 vim 模式,修改 SPECIAL_LEVEL 的值为 veryveryveryloooooooong。

1
2
3
4
5
apiVersion: v1
data:
SPECIAL_LEVEL: veryveryveryloooooooong
SPECIAL_TYPE: charm
special.how: very

稍等片刻后,查看结果:

1
2
# kubectl exec -it busybox cat /etc/myconfig/keys
veryveryveryloooooooong

因为 kubelet 检测需要一定时间,所以需要延后查看。
注意,在指定 subPath 情况下,不会自动更新。

问题及记录

如果指定的变量字段不正确,会提示:

1
Error: couldn't find key sspecial.how in ConfigMap default/special-config  # 此处sspecial.how出错

如果pod使用到configmap,但又没有创建configmap,提示:

1
Error: configmap "special-config1" not found

再创建configmap时,pod会自动启动(即不需要先删除)。

注意,在 KubeEdge 中,如果configmap不存在,并不会明确提示,只会显示 Pending 状态。

参考:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-pod-configmap/