一:简介很多生产环境中的应用程序配置较为复杂,可能需要多个config文件、命令行参数和环境变量的组合。使用容器部署时,把配置应该从应用程序镜像中解耦出来,以保证镜像的可移植性。
ConfigMap是用来存储配置文件的kubernetes资源对象,所有的配置内容都存储在etcd中。ConfigMap让我们能够从容器镜像中把配置的详细信息给解耦出来。通过ConfigMap我们能够把配置以key-value对的形式传递到container或者别的系统组件(比如Controller)里面。
二:ConfigMap的创建方式1.From Literal Values 可以用kubectl create来创建一个ConfigMap,然后通过kubectl get来获取
-
# Create the ConfigMap
-
$ kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
-
configmap"my-config" created
-
-
# Get the ConfigMap Details for my-config
-
$ kubectl get configmaps my-config -o yaml
-
apiVersion: v1
-
data:
-
key1: value1
-
key2: value2
-
kind: ConfigMap
-
metadata:
-
creationTimestamp: 2017-05-31T07:21:55Z
-
name: my-config
-
namespace: default
-
resourceVersion: "241345"
-
selfLink: /api/v1/namespaces/default/configmaps/my-config
-
uid: d35f0a3d-45d1-11e7-9e62-080027a46057
2.From Configuration File 直接通过配置文件来创建
-
apiVersion: v1
-
kind: ConfigMap
-
metadata:
-
name: special-config
-
namespace: default
-
data:
-
special.how: very
-
apiVersion: v1
-
kind: ConfigMap
-
metadata:
-
name: env-config
-
namespace: default
-
data:
-
log_level: INFO
3. 当前目录下包含如果有配置文件donkey.properties 可以通过kubectl命令创建一个包含该文件内容的ConfigMap
kubectl create configmap donkey.properties --from-file=donkey.properties
三:ConfigMap的使用方式1.Pod 通过env使用ConfigMap定义环境变量
-
apiVersion: v1
-
kind: Pod
-
metadata:
-
name: dapi-test-pod
-
spec:
-
containers:
-
- name: test-container
-
image: gcr.io/google_containers/busybox
-
command: ["/bin/sh", "-c", "env" ]
-
env:
-
- name: SPECIAL_LEVEL_KEY
-
valueFrom:
-
configMapKeyRef:
-
name: special-config
-
key: special.how
-
- name: LOG_LEVEL
-
valueFrom:
-
configMapKeyRef:
-
name: env-config
-
key: log_level
-
restartPolicy: Never
从Kubernetes V1.6 开始,引入一个新的字段envFrom,实现在Pod环境内将ConfigMap中所以定义的key=value自动生成环境变量:
-
apiVersion: v1
-
kind: Pod
-
metadata:
-
name: dapi-test-pod
-
spec:
-
containers:
-
- name: test-container
-
image: gcr.io/google_containers/busybox
-
command: [ "/bin/sh", "-c", "env" ]
-
envFrom:
-
- configMapRef:
-
name: special-config
-
restartPolicy: Never
2.使用ConfigMap设置容器启动命令的启动参数
-
apiVersion: v1
-
kind: Pod
-
metadata:
-
name: dapi-test-pod
-
spec:
-
containers:
-
- name: test-container
-
image: k8s.gcr.io/busybox
-
command: ["/bin/sh", "-c", "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
3.在存储卷中使用ConfigMap
-
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: default
spec:
containers:
- image: 120.79.156.135/desktop/busybox:latest
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
name: busybox
volumeMounts:
- name: donkeyconfig
mountPath: /configfiles
volumes:
- name: donkeyconfig
configMap:
name: donkey.properties
items:
- key: donkey.properties
path: donkey.properties
restartPolicy: Always
imagePullSecrets:
- name: registry-secret
四:使用ConfigMap的限制条件
1.ConfigMap必须在Pod之前创建
2.ConfigMap受Namespace限制,只有处于相同Namespaces中的Pod可以引用它
3.ConfigMap 中的配额管理还未能实现
4.kubelet只支持可以被API Server管理的Pod使用ConfigMap
文章标题:kubernetes实践之三十五:Pod配置管理ConfigMap
路径分享:
http://cdxtjz.cn/article/pepghp.html