一、简介
ConfigMap是一个配置中心,可以配置Pod
二、ConfigMap
常见创建ConfigMap的类型如下:
- 命令创建
- 文件创建
1.命令行创建ConfigMap
# --from-literal 表示指定不同的key、value
[root@redis-cluser-01 configmap]# kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# 查看cm
[root@redis-cluser-01 configmap]# kubectl get cm
NAME DATA AGE
my-config 2 62s
# 查看cm的具体值信息,可以看到键key1=config1 key2=config2
[root@redis-cluser-01 configmap]# kubectl describe cm my-config
Name: my-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
key1:
----
config1
key2:
----
config2
Events: <none>
2.文件创建ConfigMap
[root@redis-cluser-01 configmap]# vim www.conf
servcer {
server_name myaqpp.magedu.com;
listen 80;
root /data/web/html/;
}
[root@redis-cluser-01 configmap]# kubectl create configmap nginx-www --from-file=./www.conf
# 查看cm
[root@redis-cluser-01 configmap]# kubectl get cm
NAME DATA AGE
nginx-www 1 26s
# 查看cm的具体值信息 可以看到键www.conf value值则为其文件内容
[root@redis-cluser-01 configmap]# kubectl describe cm nginx-www
Name: nginx-www
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
www.conf:
----
servcer {
server_name myaqpp.magedu.com;
listen 80;
root /data/web/html/;
}
Events: <none>
三、ConfigMap配置至pod
配置了configmap后,因其本身就是一个配置中心,可以挂载至容器中让其更变为环境变量
1.envFrom配置环境变量
- 将cm中的key挂载至容器中
“`shell
<h1>创建cm</h1>
[root@redis-cluser-01 tmp]# kubectl create configmap config-test –from-literal=my_key=my_value
<h1>查看cm信息</h1>
[root@redis-cluser-01 tmp]# kubectl describe cm config-test
Name: config-test
Namespace: default
Labels: <none>
Annotations: <none><h1>Data</h1>
my_key:</p></li>
</ul><hr />
<p>my_value
Events: <none>[root@redis-cluser-01 configmap]# vim pod-configmap.yml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
– name: test-container
image: ikubernetes/myapp:v1
envFrom:
– configMapRef:
name: config-test
restartPolicy: Never# 查看容器的环境变量
[root@redis-cluser-01 configmap]# kubectl exec -it dapi-test-pod printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=dapi-test-pod
TERM=xterm
my_key=my_value //环境变量已经加载进来
KUBERNETES_SERVICE_PORT_HTTPS=443# 备注:此种方法比较宽泛,它统一将key value全部作为环境变量
“`
2.env配置环境变量
- env配置环境变量可以细化某个cm的key,也可以同时指定多个cm的key
[root@redis-cluser-01 configmap]# vim configmap-pod-2.yml apiVersion: v1 kind: Pod metadata: name: dapi-test-pod-2 spec: containers: - name: test-container image: ikubernetes/myapp:v1 env: - name: KEY1 valueFrom: configMapKeyRef: name: my-config key: key1 - name: MY_KEY valueFrom: configMapKeyRef: name: config-test key: my_key restartPolicy: Never # 查看容器环境变量 [root@redis-cluser-01 configmap]# kubectl exec -it dapi-test-pod-2 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=dapi-test-pod-2 TERM=xterm KEY1=config1 MY_KEY=my_value ....
四、ConfigMap语法
- 创建configmap
[root@redis-cluser-01 configmap]# vim configmap-1.yml apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: SPECIAL_LEVEL: very SPECIAL_TYPE: charm # 查看cm root@redis-cluser-01 configmap]# kubectl describe cm special-config Name: special-config Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"SPECIAL_LEVEL":"very","SPECIAL_TYPE":"charm"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"special-... Data ==== SPECIAL_LEVEL: ---- very SPECIAL_TYPE: ---- charm Events: <none>
- pod加载configmap
参考第三步骤
留言