标签:containe conf 方案 meta restart 就是 dir core 最佳实践
应用部署的一个最佳实践就是将应用所需的配置信息与程序分离,这样可以使应用更好地进行复用,通过不同的配置也可以实现更灵活的功能。
所以从v1.2
开始,kubernetes提供了一种统一的应用配置管理方案-configMap
环境变量
需设置为环境变量
)volume
形式挂载为容器内部的文件或目录configMap
以一个或者多个key:value
的形式保存,其中:
loglevel=info
的形式echo "{ \"id\": \"emm\", \"name\": \"tom\"}" >> test.json
key=文件名,value=文件内容
kubectl create cm map2 --from-file=test.json
kubectl describe cm map2
mkdir cmtest && echo "{ \"id\": \"emm\", \"name\": \"tom\"}" >> cmtest/test.json &&echo "{ \"id\": \"emm\", \"name\": \"tom\"}" >> cmtest/test2.json
key=文件夹下的每个文件名,value=文件内容
kubectl create cm map2 --from-file=cmtest/
使用文字值创建,利用--from-literal
参数传递配置信息
kubectl create cm testname --from-literal=key1=value1 --from-literal=key2=value2
下图分别演示了
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-name-1
namespace: default
data:
key1: "hahah"
key2: |
{
"property1": "value1",
"property2": true
}
共有两种使用方式:
k/v
映射成环境变量apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.coreqi.cn/library/myapp:v1
command: [ "/bin/sh", "-c", "env" ]
# 自己指定环境变量名字,使用configmap的值
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
# 将整个configmap里面的`k/v`映射成环境变量
envFrom:
- configMapRef:
name: env-config
restartPolicy: Never
导入方式和环境变量一样的,这里主要体现的是一种思想,如下
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.coreqi.cn/library/myapp:v1
# 这里输出使用环境变量,说明也能当启动参数
command: [ "/bin/sh","-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.coreqi.cn/library/myapp:v1
command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
# 把config-volume挂载到下面的路径下
volumeMounts:
- name: config-volume
mountPath: /etc/config
# 导入“special-config”的configmap,并命名为config-volume
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: hub.coreqi.cn/library/myapp:v1
command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
# 把config-volume下的key1的值。完整地赋值给appsettings.Production.json
volumeMounts:
- name: config-volume
mountPath: /app/appsettings.Production.json
subPath: key1
# 导入“special-config”的configmap,并命名为config-volume
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
kubectl edit configmap log-config
假如有资源挂载了这个configmap,等待大约十秒后就能热更新
kubernetes系列(十一) - 存储之configMap
标签:containe conf 方案 meta restart 就是 dir core 最佳实践
原文地址:https://www.cnblogs.com/baoshu/p/13258291.html