ConfigMaps允许您将配置工件与image内容分离,以保持容器化应用程序的便携性。 本页面提供了一系列使用示例,演示如何使用ConfigMaps中存储的数据创建ConfigMaps和配置Pod。
Create ConfigMaps from directories
[root@mhc config_dir]# ls
a.cnf b.cnf
kubectl create configmap test-config --from-file=`pwd`
[root@mhc config_dir]# kubectl describe configmaps test-config
Name: test-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
a.cnf:
----
haha
lala
balabala
b.cnf:
----
you are stupid!
heng heng
Events: <none>
===================================
kubectl create configmap test-config2 --from-file=`pwd`/a.cnf
kubectl create configmap test-config3 --from-file=`pwd`/a.cnf --from-file=`pwd`/b.cnf
[root@mhc config_dir]# kubectl create configmap test-config4 --from-file=key1=`pwd`/a.cnf --from-file=key2=`pwd`/b.cnf
configmap "test-config4" created
[root@mhc config_dir]#
[root@mhc config_dir]#
[root@mhc config_dir]# kubectl describe configmap test-config4
Name: test-config4
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
key1:
----
haha
lala
balabala
key2:
----
you are stupid!
heng heng
Events: <none>
===================================
[root@mhc config_dir]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap "special-config" created
[root@mhc config_dir]# kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: 2018-02-27T08:21:36Z
name: special-config
namespace: default
resourceVersion: "61698"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: 3a4a0483-1b97-11e8-a9e9-dcfe07d61067
========================================
kubectl create configmap consul-config --from-literal=datacenter=haha
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
io.kompose.service: consul2
name: consul
spec:
replicas: 1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: consul2
name: consul2
spec:
containers:
- args:
- agent
- -server
- -client=0.0.0.0
- -ui
- -bootstrap
- -bind=0.0.0.0
image: consul:0.8.4
name: consul
ports:
- containerPort: 8500
- containerPort: 8300
- containerPort: 8301
- containerPort: 8302
- containerPort: 8400
resources:
limits:
memory: "134217728"
env:
- name: DATACENTER
valueFrom:
configMapKeyRef:
name: consul-config
key: datacenter
restartPolicy: Always
status: {}
====================================================
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
=============================
Add ConfigMap data to a Volume
[root@mhc config_dir]# kubectl create configmap consul-config2 --from-file=`pwd`/my.cnf
configmap "consul-config2" created
[root@mhc config_dir]#
[root@mhc config_dir]# kubectl describe configmaps consul-config2
Name: consul-config2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
my.cnf:
----
[mysqld]
aa = bb
cc= ee
[client]
user = root
password = root.123
Events: <none>
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
io.kompose.service: consul3
name: consul
spec:
replicas: 2
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: consul3
name: consul3
spec:
containers:
- args:
- agent
- -server
- -client=0.0.0.0
- -ui
- -bootstrap
- -bind=0.0.0.0
image: consul:0.8.4
name: consul
resources:
limits:
memory: "134217728"
volumeMounts:
- name: config-volume
mountPath: /etc/myconfig
volumes:
- name: config-volume
configMap:
name: consul-config2
restartPolicy: Always
status: {}
[root@mhc config_dir]# kubectl exec -ti consul-7b7bddfff6-9rmbw sh
/ # ls /etc/myconfig/
my.cnf
/ # cat /etc/myconfig/my.cnf
[mysqld]
aa = bb
cc= ee
[client]
user = root
password = root.123
===========================================================
volumes:
- name: config-volume
configMap:
name: consul-config2
items:
- key: my.cnf
path: mysql/my.cnf
/ # ls /etc/myconfig/mysql/
my.cnf
====================================
kind: PodPreset
apiVersion: settings.k8s.io/v1alpha1
metadata:
name: consul-setting2
spec:
selector:
matchLabels:
name: consul
envFrom:
- configMapRef:
name: test-config