1, ConfigMap introduce
Con fi gMap features introduced in version Kubernetes1.2, many from the application configuration file, command line parameters or environment variable to read configuration information. Con fi gMap API provides us with a mechanism for injecting configuration information to the container,
Con GMAP Fi can be used to hold a single attribute may be used to save the entire configuration file or JSON blob.
2, Con fi gMap created
ConfigMap profile allows you to separate the image file, so that the container of the application portability. Use kubectl create configmap or ConfigMap generator to create ConfigMap.
kubectl create configmap <map-name>
<data-source> where, <map-name> is the name assigned to ConfigMap, <data-source> is the directory data is to be extracted, value document or text.
ConfigMap source data corresponding to the key-value (key-value pairs)
Key = command file name provided in the row or key
value = value document or text content available on the command line
using kubectl describe kubectl get or retrieve information about the ConfigMap .
1) Create directory ConfigMap The
[@ k8smaster the root Test] # pwd
/ the root / Test
[Test k8smaster the root @] LS # -lrt
Total. 8
-rw-R & lt - r-- 1 On Feb 18 is the root 12 is the root zimu 17:36. Properties
-rw-R & lt - r-- On Feb 38 is the root 18 is the root. 1 17:37 kemu.properties
[@ k8smaster the root Test] More zimu.properties #
A. 1 =
2 = B
C =. 3
[Test k8smaster the root @] More kemu.properties #
kemu1 = shuxue
kemu2 = Yuwen
kemu3 = Yingyu
[@ k8smaster the root Test] #
[@ k8smaster the root Test] # kubectl Create File-ConfigMap MULU --from = All files / root / test # from-file is specified in the directory will be used in the Con fi gMap inside to create a key-value pair, names of keys is the file name, value is the contents of the file
ConfigMap / MULU the created
[root @ k8smaster the Test] # GET ConfigMap kubectl
NAME of AGE the DATA
MULU 2 16S
[@ k8smaster the root Test] # kubectl DESCRIBE ConfigMap MULU
the Name: MULU
the Namespace: default
Labels: <none>
Annotations: <none>
the Data
====
kemu.properties:
----
kemu1 = shuxue
kemu2=yuwen
kemu3=yingyu
zimu.properties:
----
A=1
B=2
C=3
Events: <none>
[root@k8smaster test]#
2) Create a ConfigMap based on the file
[root @ k8smaster the Test] # kubectl ConfigMap Wenjian the --from the Create-File = / root / the Test / zimu.properties # from-File parameter can be used multiple times, the effect is just the same designated the entire directory
ConfigMap / Wenjian Created
[the root @ k8smaster Test] # kubectl GET ConfigMap
NAME the DATA of AGE
MULU 2 12m
Wenjian. 1 9S
[the root @ k8smaster Test] # kubectl DESCRIBE ConfigMap Wenjian
the Name: Wenjian
the Namespace: default
Labels: <none>
Annotations: <none>
the Data
====
zimu.properties:
----
A. 1 =
B 2 =
C =. 3
Events: <none>
[@ k8smaster the root Test] #
3) Create based on the character value ConfigMap
kubectl create configmap the parameter with --from-literal, the literal value defined in the command line
[Test k8smaster the root @] # kubectl create configmap canshu --from-literal. 1 = A = --from- literal = B = 2 # from- literal argument passing configuration information, the parameter may be used multiple times
ConfigMap / canshu Created
[@ k8smaster the root Test] GET # kubectl ConfigMap
NAME the DATA of AGE
canshu 2 9S
MULU the 16m 2
Wenjian. 1 4m36s
[the root @ k8smaster Test] # kubectl DESCRIBE ConfigMap canshu
the Name: canshu
the Namespace: default
Labels: <none>
Annotations: <none>
Data
====
A:
----
1
B:
----
2
Events: <none>
[root@k8smaster test]#
3、Pod的创建
1)使用 ConfigMap 来替代环境变量
[root@k8smaster test]# more env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: env-name
data:
env.name: javahome
env.type: java
[root@k8smaster test]# kubectl create -f env.yaml
configmap/env-name created
[root@k8smaster test]# kubectl get configmap -o yaml
apiVersion: v1
items:
- apiVersion: v1
data:
env.name: javahome
env.type: java
kind: ConfigMap
metadata:
creationTimestamp: "2020-02-18T14:20:59Z"
name: env-name
namespace: default
resourceVersion: "708005"
selfLink: /api/v1/namespaces/default/configmaps/env-name
uid: e7921366-ef80-4399-9758-349861e55118
kind: List
metadata:
resourceVersion: ""
selfLink: ""
[root@k8smaster test]#
[root@k8smaster test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
env-pod 0/1 Completed 0 80s
[root@k8smaster test]#
[root@k8smaster test]# more env-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: env-pod
spec:
containers:
- name: env-pod-ctn
image: 192.168.23.100:5000/tomcat:v2
command: ["/bin/bash","-c","env"]
env:
- name: JAVA_HOME
valueFrom:
configMapKeyRef:
name: env-name
key: env.name
- name: JAVA_TYPE
valueFrom:
configMapKeyRef:
name: env-name
key: env.type
restartPolicy: Never
[root@k8smaster test]# kubectl create -f env-pod.yaml
pod/env-pod created
[root@k8smaster test]# kubectl logs env-pod|grep java
JAVA_HOME=javahome
JAVA_TYPE=java
[root@k8smaster test]#
2)通过数据卷插件使用ConfigMap
[root@k8smaster test]# more env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: env-name
data:
env.name: javahome
env.type: java
[root@k8smaster test]# more env-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: volume-pod
spec:
containers:
- name: volume-pod-ctn
image: 192.168.23.100:5000/tomcat:v2
volumeMounts:
- name: config-volume
mountPath: /tmp/config
volumes:
- name: config-volume
configMap:
name: env-name
restartPolicy: Never
[root@k8smaster test]# kubectl create -f env-volume.yaml
pod/volume-pod created
[root@k8smaster test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
volume-pod 1/1 Running 0 8s
[root@k8smaster test]# kubectl exec -it volume-pod -- /bin/bash
root@volume-pod:/usr/local/tomcat# cd /tmp/config/
root@volume-pod:/tmp/config# ls -lrt
total 0
lrwxrwxrwx 1 root root 15 Feb 18 16:05 env.type -> ..data/env.type
lrwxrwxrwx 1 root root 15 Feb 18 16:05 env.name -> ..data/env.name
root@volume-pod:/tmp/config# more env.name
javahome
root@volume-pod:/tmp/config# more env.type
java
root@volume-pod:/tmp/config#
3)修改 ConfigMap
[root@k8smaster test]# kubectl edit configmap env-name
configmap/env-name edited
[root@k8smaster test]# kubectl exec -it volume-pod -- /bin/bash
root@volume-pod:/tmp/config# pwd
/tmp/config
root@volume-pod:/tmp/config# ls -lrt
total 0
lrwxrwxrwx 1 root root 15 Feb 18 16:05 env.type -> ..data/env.type
lrwxrwxrwx 1 root root 15 Feb 18 16:05 env.name -> ..data/env.name
root@volume-pod:/tmp/config# more env.type
java1
root@volume-pod:/tmp/config# more env.name
javahome1
root@volume-pod:/tmp/config#