configmap学习
# configMap
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
# 创建configMap
可以使用kubectl create configmap
或者kustomization.yaml
中的ConfigMap生成器来创建。需要注意的是kubectl
在1.14之后才支持kustomization.yaml
。
# 使用kubectl create configmap创建ConfigMap
kubectl create configmap <map-name> <data-source>
1
- 从目录中创建
# 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/
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
kubectl describe configmaps game-config
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
kubectl get configmaps game-config -o yaml
1
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T18:52:05Z
name: game-config
namespace: default
resourceVersion: "516"
uid: b4952dc3-d670-11e5-8cd0-68f728db1985
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 从文件创建
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
1
kubectl describe configmaps game-config-2
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
也可以使用多个数据源
ubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties
1
kubectl describe configmaps game-config-2
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 配置configmap中的键值对作为容器的环境变量
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
kubectl create -f configmap.yaml
1
demo-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
kubectl create -f demo-pod.yaml
1
Now, the Pod's output includes environment variables SPECIAL_LEVEL=very and SPECIAL_TYPE=charm.
# 在pod命令行中使用configmap定义的环境变量
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
produces the following output in the test-container container:
very charm
1
# 将configmap中的数据添加到volume中
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
When the pod runs, the command ls /etc/config/ produces the output below:
SPECIAL_LEVEL
SPECIAL_TYPE
1
2
2
# 将configmap中的数据添加到volume中指定的路径
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
When the pod runs, the command cat /etc/config/keys produces the output below:
very
1
# 练习
基于springboot的配置文件创建一个configmap
application.yml
logging:
config: classpath:conf/logback-dev.xml
server:
port: 80
spring:
application:
name: spring-boot-release-spring-boot-chart
1
2
3
4
5
6
7
2
3
4
5
6
7
kubectl create configmap demo-spring-boot-yaml --from-file=application.yml
1
kubectl get configmaps demo-spring-boot-yaml -o yaml
1
apiVersion: v1
data:
application.yml: |
logging:
config: classpath:conf/logback-dev.xml
server:
port: 80
spring:
application:
name: spring-boot-release-spring-boot-chart
kind: ConfigMap
metadata:
creationTimestamp: "2022-07-17T16:08:10Z"
name: demo-spring-boot-yaml
namespace: default
resourceVersion: "20116930"
uid: 9139b742-764b-4659-b46f-9e163170709b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
随便搞个容器来引用下这个configmap,将其挂载到volume中
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
- name: demo-spring-boot-yaml
mountPath: /opt/demo-spring-boot-yaml
ports:
- containerPort: 80
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx
- name: demo-spring-boot-yaml
configMap:
name: demo-spring-boot-yaml
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
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
进入容器内部/opt/demo-spring-boot-yaml,看下configmap的效果
kubectl exec -it nginx-86b94c8bbb-glp7p sh
1
# nginx 挂载index.html
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}
data:
{{ (.Files.Glob "**.html").AsConfig | indent 2 }}
1
2
3
4
5
6
2
3
4
5
6
volumes:
- name: nginx-html-file
configMap:
name: config-map-name
items:
- key: "html-file-name"
path: "index.html"
1
2
3
4
5
6
7
2
3
4
5
6
7
volumeMounts:
- name: nginx-html-file
mountPath: /usr/share/nginx/html
readOnly: true
1
2
3
4
2
3
4
helm install html-configmap .
1
上次更新: 2024/08/08, 06:59:58