k8s-configMap使用案例
1. configmap配置文件示例
#### 1.1 configmap示例配置
apiVersion: v1
kind: ConfigMap
metadata:
name: configmap-demo
data:
# 类属性键;每一个键都映射到一个简单的值
abc: "123"
cde: "456"
# 类文件键
redis.properties: |
port: 6379
host: 10.100.24.71
password: 123456
1.2 configmap 的pod配置文件
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: nginx
env:
# 定义环境变量
- name: ABC_NAME # 请注意这里和 ConfigMap 中的键名是不一样的
valueFrom:
configMapKeyRef:
name: configmap-demo # 这个值来自 ConfigMap
key: abc # 需要取值的键
- name: CDE_NAME
valueFrom:
configMapKeyRef:
name: configmap-demo
key: cde
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
# 你可以在 Pod 级别设置卷,然后将其挂载到 Pod 内的容器中
- name: config
configMap:
# 提供你想要挂载的 ConfigMap 的名字
name: configmap-demo
# 来自 ConfigMap 的一组键,将被创建为文件
items:
- key: "redis.properties"
path: "redis.properties"
2. k8s-configMap使用案例
2.1 配置configmap
-
创建configmap路径
[root@k8s-master ~]# mkdir -p yaml/configmap/
-
编写configmap
[root@k8s-master configmap]# vi configmap.yaml [root@k8s-master configmap]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: configmap-demo data: # 类属性键;每一个键都映射到一个简单的值 abc: "123" cde: "456" # 类文件键 redis.properties: | port: 6379 host: 10.100.24.71 password: 123456
-
启动configmap
[root@k8s-master configmap]# kubectl apply -f configmap.yaml configmap/configmap-demo created
-
验证服务
[root@k8s-master configmap]# kubectl get -n default configmaps NAME DATA AGE configmap-demo 3 53s
2.2 创建一个configmap的pod应用
-
编写pod的配置文件
[root@k8s-master configmap]# vim configmap-pod.yaml [root@k8s-master configmap]# cat configmap-pod.yaml apiVersion: v1 kind: Pod metadata: name: configmap-demo-pod spec: containers: - name: demo image: nginx env: # 定义环境变量 - name: ABC_NAME # 请注意这里和 ConfigMap 中的键名是不一样的 valueFrom: configMapKeyRef: name: configmap-demo # 这个值来自 ConfigMap key: abc # 需要取值的键 - name: CDE_NAME valueFrom: configMapKeyRef: name: configmap-demo key: cde volumeMounts: - name: config mountPath: "/config" readOnly: true volumes: # 你可以在 Pod 级别设置卷,然后将其挂载到 Pod 内的容器中 - name: config configMap: # 提供你想要挂载的 ConfigMap 的名字 name: configmap-demo # 来自 ConfigMap 的一组键,将被创建为文件 items: - key: "redis.properties" path: "redis.properties"
-
启动配置文件
[root@k8s-master configmap]# kubectl apply -f configmap-pod.yaml pod/configmap-demo-pod created
-
查看服务是否启动
[root@k8s-master configmap]# kubectl get pods NAME READY STATUS RESTARTS AGE configmap-demo-pod 1/1 Running 0 2m27s
-
验证变量和挂载镜像
[root@k8s-master configmap]# kubectl exec -it configmap-demo-pod -- /bin/bash root@configmap-demo-pod:/# ls /config/ redis.properties root@configmap-demo-pod:/# cat /config/redis.properties port: 6379 host: 10.100.24.71 password: 123456 root@configmap-demo-pod:/# env KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_SERVICE_PORT=443 CDE_NAME=456 HOSTNAME=configmap-demo-pod PWD=/ PKG_RELEASE=1~buster HOME=/root KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 NJS_VERSION=0.5.0 TERM=xterm SHLVL=1 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 KUBERNETES_SERVICE_HOST=10.96.0.1 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PORT=443 ABC_NAME=123 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin NGINX_VERSION=1.19.6 _=/usr/bin/env