赞
踩
应用部署的一个最佳实践是将应用所需的配置信息与程序进行分离,这样可以使应用程序被更好地复用,通过不同
的配置也能实现更灵活的功能。将应用打包为容器镜像后,可以通过环境变量或者外挂文件的方式在创建容器时进
行配置注入,但在大规模容器集群的环境中,对多个容器进行不同的配置将变得非常复杂。从 Kubernetes 1.2 开
始提供了一种统一的应用配置管理方案—ConfigMap。本节对 ConfigMap 的概念和用法进行详细描述。
ConfigMap 供容器使用的典型用法如下。
(1)生成为容器内的环境变量。
(2)设置容器启动命令的启动参数(需设置为环境变量)。
(3)以 Volume 的形式挂载为容器内部的文件或目录。
ConfigMap 以一个或多个 key:value
的形式保存在 Kubernetes 系统中供应用使用,既可以用于表示一个变量的
值(例如 apploglevel=info
),也可以用于表示一个完整配置文件的内容(例如 server.xml=<?xml...>...
)。
可以通过 YAML 配置文件或者直接使用 kubectl create configmap
命令行的方式来创建 ConfigMap。
下面的例子 004-cm-appvars.yaml
描述了将几个应用所需的变量定义为 ConfigMap 的用法:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-appvars
data:
apploglevel: info
appdatadir: /var/data
执行 kubectl create 命令创建该 ConfigMap:
[root@master cha3]# kubectl create -f 004-cm-appvars.yaml
configmap/cm-appvars created
查看创建好的 ConfigMap:
[root@master cha3]# kubectl get configmap
NAME DATA AGE
cm-appvars 2 17s
kube-root-ca.crt 1 5h50m
[root@master cha3]# kubectl describe configmap cm-appvars
Name: cm-appvars
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
appdatadir:
----
/var/data
apploglevel:
----
info
Events: <none>
[root@master cha3]# kubectl get configmap cm-appvars -o yaml
apiVersion: v1
data:
appdatadir: /var/data
apploglevel: info
kind: ConfigMap
metadata:
creationTimestamp: "2023-07-02T07:28:38Z"
name: cm-appvars
namespace: default
resourceVersion: "23481"
uid: 3324438f-da12-4bc6-824a-ee4ee3d447b5
下面的例子 005-cm-appconfigfiles.yaml
描述了将两个配置文件 server.xml
和 logging.properties
定义
为 ConfigMap 的用法,设置 key 为配置文件的别名,value 则是配置文件的全部文本内容:
apiVersion: v1 kind: ConfigMap metadata: name: cm-appconfigfiles data: key-serverxml: | <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> key-loggingproperties: "handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler\r\n\r\n.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler\r\n\r\n1catalina.org.apache.juli.FileHandler.level = FINE\r\n1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n1catalina.org.apache.juli.FileHandler.prefix = catalina.\r\n\r\n2localhost.org.apache.juli.FileHandler.level = FINE\r\n2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n2localhost.org.apache.juli.FileHandler.prefix = localhost.\r\n\r\n3manager.org.apache.juli.FileHandler.level = FINE\r\n3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n3manager.org.apache.juli.FileHandler.prefix = manager.\r\n\r\n4host-manager.org.apache.juli.FileHandler.level = FINE\r\n4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n4host-manager.org.apache.juli.FileHandler.prefix = host-manager.\r\n\r\njava.util.logging.ConsoleHandler.level = FINE\r\njava.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\r\n\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.FileHandler\r\n\r\n"
执行 kubectl create 命令创建该 ConfigMap:
[root@master cha3]# kubectl create -f 005-cm-appconfigfiles.yaml
configmap/cm-appconfigfiles created
查看创建好的 ConfigMap:
[root@master cha3]# kubectl get configmap cm-appconfigfiles
NAME DATA AGE
cm-appconfigfiles 2 14s
[root@master cha3]# kubectl describe configmap cm-appconfigfiles Name: cm-appconfigfiles Namespace: default Labels: <none> Annotations: <none> Data ==== key-loggingproperties: ---- handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.FileHandler.prefix = catalina. 2localhost.org.apache.juli.FileHandler.level = FINE 2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 2localhost.org.apache.juli.FileHandler.prefix = localhost. 3manager.org.apache.juli.FileHandler.level = FINE 3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 3manager.org.apache.juli.FileHandler.prefix = manager. 4host-manager.org.apache.juli.FileHandler.level = FINE 4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 4host-manager.org.apache.juli.FileHandler.prefix = host-manager. java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.FileHandler key-serverxml: ---- <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> Events: <none>
查看已创建的 ConfigMap 的详细内容,可以看到两个配置文件的全文:
[root@master cha3]# kubectl get configmap cm-appconfigfiles -o yaml apiVersion: v1 data: key-loggingproperties: "handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler\r\n\r\n.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler\r\n\r\n1catalina.org.apache.juli.FileHandler.level = FINE\r\n1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n1catalina.org.apache.juli.FileHandler.prefix = catalina.\r\n\r\n2localhost.org.apache.juli.FileHandler.level = FINE\r\n2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n2localhost.org.apache.juli.FileHandler.prefix = localhost.\r\n\r\n3manager.org.apache.juli.FileHandler.level = FINE\r\n3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n3manager.org.apache.juli.FileHandler.prefix = manager.\r\n\r\n4host-manager.org.apache.juli.FileHandler.level = FINE\r\n4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs\r\n4host-manager.org.apache.juli.FileHandler.prefix = host-manager.\r\n\r\njava.util.logging.ConsoleHandler.level = FINE\r\njava.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter\r\n\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler\r\n\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO\r\norg.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.FileHandler\r\n\r\n" key-serverxml: | <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> kind: ConfigMap metadata: creationTimestamp: "2023-07-02T07:32:21Z" name: cm-appconfigfiles namespace: default resourceVersion: "23858" uid: fd6af479-e130-4b97-8fd3-1f772427e1be
不使用 YAML 文件,直接通过 kubectl create configmap 也可以创建 ConfigMap,可以使用参数 --from-file
或 --from-literal
指定内容,并且可以在一行命令中指定多个参数。
(1)通过 --from-file
参数从文件中进行创建,可以指定 key 的名称,也可以在一个命令行中创建包含多个
key 的 ConfigMap,语法为:
$ kubectl create configmap NAME --from-file=[key=] source --from-file=[key=] source
例如,在当前目录下含有配置文件 server.xml
,可以创建一个包含该文件内容的 ConfigMap:
# server.xml文件的内容
<?xml version='1.0' encoding='utf-8'?>
<h>Hello</h>
[root@master cha3]# kubectl create configmap cm-server.xml --from-file=server.xml
configmap/cm-server.xml created
[root@master cha3]# kubectl get configmap
NAME DATA AGE
cm-appconfigfiles 2 9m19s
cm-appvars 2 13m
cm-server.xml 1 22s # 刚创建
kube-root-ca.crt 1 6h2m
[root@master cha3]# kubectl describe configmap cm-server.xml
Name: cm-server.xml
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
server.xml:
----
<?xml version='1.0' encoding='utf-8'?>
<h>Hello</h>
Events: <none>
创建的时候如果不指定 key 的值则默认为文件的名字,下面创建一个带 key 的。
例如,在当前目录下含有配置文件 hello.html
,可以创建一个包含该文件内容的 ConfigMap:
# hello.html文件的内容
<!DOCTYPE html>
<html>
<head>
<title>页面标题</title>
</head>
<body>
<p>Hello world </p>
</body>
</html>
[root@master cha3]# kubectl create configmap hello.html --from-file=hello-html=hello.html
configmap/hello.html created
[root@master cha3]# kubectl get configmap
NAME DATA AGE
cm-appconfigfiles 2 12m
cm-appvars 2 16m
cm-server.xml 1 3m54s
hello.html 1 13s # 刚创建
kube-root-ca.crt 1 6h6m
[root@master cha3]# kubectl describe configmap hello.html Name: hello.html Namespace: default Labels: <none> Annotations: <none> Data ==== hello-html: ---- <!DOCTYPE html> <html> <head> <title>页面标题</title> </head> <body> <p>Hello world </p> </body> </html> Events: <none>
(2)通过 --from-file
参数从目录中进行创建,该目录下的每个配置文件名都被设置为 key,文件的内容被设
置为 value,语法为:
$ kubectl create configmap NAME --from-file=config-files-dir
假设在 configfiles 目录下包含两个配置文件 server.xml
和 logging.properties
,创建一个包含这两个文件内
容的 ConfigMap:
[root@master configfiles]# cat server.xml
<m>TT</m>
[root@master configfiles]# cat logging.properties
level=DEBUG
[root@master cha3]# kubectl create configmap cm-appconf --from-file=./configfiles
configmap/cm-appconf created
[root@master cha3]# kubectl get configmap
NAME DATA AGE
cm-appconf 2 33s
cm-appconfigfiles 2 17m
cm-appvars 2 21m
cm-server.xml 1 8m27s
hello.html 1 4m46s
kube-root-ca.crt 1 6h11m
[root@master cha3]# kubectl describe configmap cm-appconf Name: cm-appconf Namespace: default Labels: <none> Annotations: <none> Data ==== server.xml: ---- <m>TT</m> logging.properties: ---- level=DEBUG Events: <none>
(3)使用 --from-literal
时会从文本中进行创建,直接将指定的 key#=value#
创建为 ConfigMap 的内容,
语法为:
$ kubectl create configmap NAME --from-literal=key1=value1 --from-literal=key2=value2
使用--from-literal
参数进行创建的示例如下:
[root@master cha3]# kubectl create configmap cm-appenv --from-literal=loglevel=info --from-literal=appdatadir=/var/data
configmap/cm-appenv created
[root@master cha3]# kubectl get configmap cm-appenv
NAME DATA AGE
cm-appenv 2 38s
[root@master cha3]# kubectl describe configmap cm-appenv
Name: cm-appenv
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
appdatadir:
----
/var/data
loglevel:
----
info
Events: <none>
容器应用对 ConfigMap 的使用有以下两种方法。
(1)通过环境变量获取 ConfigMap 中的内容。
(2)通过 Volume 挂载的方式将 ConfigMap 中的内容挂载为容器内部的文件或目录。
以前面创建的 ConfigMap 的 cm-appvars 为例:
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-appvars
data:
apploglevel: info
appdatadir: /var/data
在 pod cm-test-pod 的定义中,将 ConfigMap cm-appvars 中的内容以环境变量( APPLOGLEVEL
) 和
(APPDATADIR
) 方式设置为容器内部的环境变量,容器的启动命令将显示这两个环境变量的值
( env | grep APP
) :
配置文件 006-cm-test-pod.yaml
的内容如下:
apiVersion: v1 kind: Pod metadata: name: cm-test-pod spec: containers: - name: cm-test image: busybox command: [ "/bin/sh", "-c", "env | grep APP" ] env: - name: APPLOGLEVEL valueFrom: configMapKeyRef: name: cm-appvars key: apploglevel - name: APPDATADIR valueFrom: configMapKeyRef: name: cm-appvars key: appdatadir restartPolicy: Never
使用 kubectl create -f 命令创建该 Pod,由于是测试 Pod,所以该 Pod 在执行完启动命令后将会退出,并且不
会被系统自动重启(restartPolicy=Never
):
[root@master cha3]# kubectl create -f 006-cm-test-pod.yaml
pod/cm-test-pod created
使用 kubectl get pods 查看已经停止的 Pod:
[root@master cha3]# kubectl get pods
NAME READY STATUS RESTARTS AGE
cm-test-pod 0/1 Completed 0 68s
frontend 1/1 Running 0 6h4m
redis-php 2/2 Running 0 5h59m
volume-pod 2/2 Running 0 4h45m
查看该 Pod 的日志,可以看到启动命令 env | grep APP
的执行结果如下:
[root@master cha3]# kubectl logs cm-test-pod
APPDATADIR=/var/data
APPLOGLEVEL=info
说明容器内部的环境变量使用 ConfigMap cm-appvars
中的值进行了正确设置。
Kubernetes 从 1.6 版本开始,引入了一个新的字段 envFrom
,实现了在 Pod 环境中将 ConfigMap(也可用于
Secret资源对象)中所有定义的key=value
自动生成为环境变量:
配置文件 007-cm-test-pod.yaml
的内容如下:
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod2
spec:
containers:
- name: cm-test
image: busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: cm-appvars
restartPolicy: Never
[root@master cha3]# kubectl create -f 007-cm-test-pod.yaml
pod/cm-test-pod2 created
[root@master cha3]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cm-test-pod 0/1 Completed 0 4m40s
cm-test-pod2 0/1 Completed 0 20s
frontend 1/1 Running 0 6h7m
redis-php 2/2 Running 0 6h2m
volume-pod 2/2 Running 0 4h49m
通过这个定义,在容器内部将会生成如下环境变量:
[root@master cha3]# kubectl logs cm-test-pod2 apploglevel=info KUBERNETES_SERVICE_PORT=443 KUBERNETES_PORT=tcp://10.96.0.1:443 HOSTNAME=cm-test-pod2 SHLVL=1 HOME=/root KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_PROTO=tcp appdatadir=/var/data KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 KUBERNETES_SERVICE_HOST=10.96.0.1 PWD=/
通过这个定义,在容器内部将会生成如下环境变量:
apploglevel=info
appdatadir=/var/data
需要说明的是,环境变量的名称受 POSIX 命名规范([a-zA-Z_][a-zA-Z0-9_]*)
约束,不能以数字开头。如果包
含非法字符,则系统将跳过该条环境变量的创建,并记录一个 Event 来提示环境变量无法生成,但并不阻止 Pod
的启动。
上面我们通过 005-cm-appconfigfiles.yaml
配置文件创建了两个配置文件的定义:server.xml
和
logging.properties
。
[root@master cha3]# kubectl get configmap cm-appconfigfiles
NAME DATA AGE
cm-appconfigfiles 2 42m
在 pod cm-test-app 的定义中,将 ConfigMap cm-appconfigfiles 中的内容以文件的形式 mount 到容器内部
的 /configfiles
目录下。Pod 配置文件 008-cm-test-app.yaml
的内容如下:
apiVersion: v1 kind: Pod metadata: name: cm-test-app spec: containers: - name: cm-test-app image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 volumeMounts: # 引用volumn的名称 - name: serverxml # 挂载到容器内的目录 mountPath: /configfiles volumes: # 定义Volumn的名称 - name: serverxml configMap: # 使用ConfigMap "cm-appconfigfiles" name: cm-appconfigfiles items: # key=key-serverxml - key: key-serverxml # value将server.xml文件名进行挂载 path: server.xml # key=key-loggingproperties - key: key-loggingproperties # value将logging.properties文件名进行挂载 path: logging.properties
创建该Pod:
[root@master cha3]# kubectl create -f 008-cm-test-app.yaml
pod/cm-test-app created
[root@master cha3]# kubectl get pod cm-test-app
NAME READY STATUS RESTARTS AGE
cm-test-app 1/1 Running 0 43s
登录容器,查看到在 /configfiles
目录下存在 server.xml
和 logging.properties
文件,它们的内容就是
ConfigMap cm-appconfigfiles
中两个key定义的内容:
[root@master cha3]# kubectl exec -ti cm-test-app -- /bin/bash root@cm-test-app:/usr/local/tomcat# cd /configfiles/ root@cm-test-app:/configfiles# ls logging.properties server.xml root@cm-test-app:/configfiles# head -10 logging.properties handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.FileHandler.prefix = catalina. 2localhost.org.apache.juli.FileHandler.level = FINE 2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs root@cm-test-app:/configfiles# head -10 server.xml <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" root@cm-test-app:/configfiles# exit exit
如果在引用 ConfigMap时不指定 items,则使用 volumeMount 方式在容器内的目录下为每个item都生成一个文
件名为 key 的文件。
Pod 配置文件 009-cm-test-app.yaml
的内容如下:
apiVersion: v1 kind: Pod metadata: name: cm-test-app2 spec: containers: - name: cm-test-app image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 volumeMounts: # 引用volumn的名称 - name: serverxml # 挂载到容器内的目录 mountPath: /configfiles volumes: # 定义Volumn的名称 - name: serverxml configMap: # 使用ConfigMap "cm-appconfigfiles" name: cm-appconfigfiles
创建该Pod:
[root@master cha3]# kubectl create -f 009-cm-test-app.yaml
pod/cm-test-app2 created
[root@master cha3]# kubectl get pod cm-test-app2
NAME READY STATUS RESTARTS AGE
cm-test-app2 1/1 Running 0 56s
登录容器,查看到在/configfiles
目录下存在 key-loggingproperties
和 key-serverxml
文件,文件的名
称来自在 ConfigMap cm-appconfigfiles
中定义的两个 key 的名称,文件的内容则为 value 的内容:
[root@master cha3]# kubectl exec -ti cm-test-app2 -- /bin/bash root@cm-test-app2:/usr/local/tomcat# cd /configfiles/ root@cm-test-app2:/configfiles# ls key-loggingproperties key-serverxml root@cm-test-app2:/configfiles# head -10 key-loggingproperties handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.FileHandler.prefix = catalina. 2localhost.org.apache.juli.FileHandler.level = FINE 2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs root@cm-test-app2:/configfiles# head -10 key-serverxml <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" root@cm-test-app2:/configfiles# exit exit
使用 ConfigMap 的限制条件如下:
ConfigMap 必须在 Pod 之前创建。
ConfigMap 受 Namespace 限制,只有处于相同 Namespace 中的 Pod 才可以引用它。
ConfigMap 中的配额管理还未能实现。
kubelet 只支持可以被 API Server 管理的 Pod 使用 ConfigMap。kubelet 在本 Node 上通过 --manifest-url
或 --config 自动创建的静态 Pod 将无法引用 ConfigMap。
在 Pod 对 ConfigMap 进行挂载(volumeMount)操作时,在容器内部只能挂载为"目录",无法挂载为"文件"。
在挂载到容器内部后,在目录下将包含 ConfigMap 定义的每个 item,如果在该目录下原来还有其他文件,则
容器内的该目录将被挂载的 ConfigMap 覆盖。如果应用程序需要保留原来的其他文件,则需要进行额外的处
理。可以将 ConfigMap 挂载到容器内部的临时目录,再通过启动脚本将配置文件复制或者链接到(cp或link命
令)应用所用的实际配置目录下。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。