定义
configMap 可以做成volume, Pod 启动后,通过volume形式映射到容器指定目录
容器中应用程序按照原有方式读取容器特定目录配置文件
注入方式
configMap 做成存储卷
configMap 通过env中configMapKeyRef注入到容器中
帮助命令查看指令
[root@k8smaster4 ~]# kubectl create configmap --help
Create a config map based on a file, directory, or specified literal value.
A single config map may package one or more key/value pairs.
When creating a config map based on a file, the key will default to the basename of the file, and the value will
default to the file content. If the basename is an invalid key, you may specify an alternate key.
When creating a config map based on a directory, each file whose basename is a valid key in the directory will be
packaged into the config map. Any directory entries except regular files are ignored (e.g. subdirectories, symlinks,
devices, pipes, etc).
Aliases:
configmap, cm
Examples:
# Create a new config map named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
# Create a new config map named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new config map named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new config map named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
指令方式创建
[root@k8smaster4 ~]# kubectl create configmap tomcat-config --from-literal=tomcat_port=8080 --from-literal=tomcat_server=myapp.tomcat.com
configmap/tomcat-config created
查看创建结果
[root@k8smaster4 ~]# kubectl get configmap
NAME DATA AGE
kube-root-ca.crt 1 85d
tomcat-config 2 5m10s
You have mail in /var/spool/mail/root
[root@k8smaster4 ~]# kubectl describe configmap tomcat-config
Name: tomcat-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
tomcat_port:
----
8080
tomcat_server:
----
myapp.tomcat.com
BinaryData
====
Events: <none>
当前目录文件创建
[root@k8smaster4 cm]# kubectl create configmap www-nginx --from-file=www=./nginx.conf
查看创建结果
[root@k8smaster4 cm]# kubectl describe configmap www-nginx
Name: www-nginx
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
www:
----
server {
server_name: www.nginx.com;
listen 80;
root /home/nginx/www/
}
BinaryData
====
Events: <none>
当前目录创建
[root@k8smaster4 mysql]# kubectl create configmap mysql-config --from-file=/root/cm/mysql/
configmap/mysql-config created
You have mail in /var/spool/mail/root
[root@k8smaster4 mysql]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 85d
mysql-config 2 8s
tomcat-config 2 36m
www-nginx 1 21m
[root@k8smaster4 mysql]# kubectl describe cm mysql-config
Name: mysql-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
my-server.conf:
----
mysql_server=1
my-slave.conf:
----
my_slave=2
BinaryData
====
Events: <none>