Kubernetes--Pod管理与配置apiVersion: v1kind: ConfigMapmetadata: name: cm-appvarsdata: apploglevel:
作者:互联网
- 通过yaml文件创建
-
apiVersion: v1 kind: ConfigMap metadata: name: cm-appvars data: apploglevel: info appdatadir: /var/data
-
将两个配置文件server.xml和logging.properties定义为ConfigMap:
-
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 创建ConfigMap,也可以使用参数--from-file或--from-literal指定内容,并且可以在一行命令中指定多个参数。
(1)通过--from-file参数从文件中进行创建,可以指定key名字,也可以在一个命令行中创建多个key的ConfigMapkubectl create config NAME --from-file=[key=] source --from-file=[key=] source
2)通过--from-file参数在目录下进行创建,该目录下的每个配置文件名都被设置为key,文件的内容被设置为value,
kubectl create configmap NAME --from-file=config-files-dir
(3)使用--from-literal 时会从文本中进行创建,直接指定的key#=value#创建为ConfigMap内容
kubectl create configmap NAME --from-literal=key1=value1 --from-literal=key2=value2
3.在Pod中使用ConfigMap资源对象 将ConfigMap "cm-appvars"中的内容以环境变量方式设置为容器内部的环境变量,容器的启动命令显示这两个环境变量
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 #环境变量取自cm-appvars key: apploglevel #key为apploglevel对应的值 - name: APPDATADIR #定义环境变量名称 valueFrom: configMapKeyRef: name: cm-appvars #环境变量取自cm-appvars key: appdatadir #key为appdatadir restartPolicy: Never
查看Pod日志可以看出,ConfigMap值进行了正确配置
在kubernetes从1.6版本中引入了一个新的字段envFrom,实现了在Pod环境中将ConfigMap(也可以用于Secret资源对象)中所定义的key=value自动生成为环境变量。
apiVersion: v1 kind: Pod metadata: name: cm-test-pod spec: containers: - name: cm-test image: busybox command: [ "/bin/sh", "-c", "env" ] envFrom: - configMapRef name: cm-appvars #根据cm-appvars自动生成环境变量 restartPolicy: Never
通过volumeMount使用ConfigMap
apiVersion: v1 kind: Pod metadata: name: cm-test-app spec: containers: - name: cm-test-app image: kubeguide/tomcat-app:v1 ports: - containerPort: 8080 volumeMounts: - name: serverxml #引用volume的名称 mountPath: /configfiles #挂载到容器内的目录 volumes: - name: serverxml #定义volume的名称 configMap: name: cm-appconfigfiles #使用ConfigMap “cm-appconfigfiles” items: - key: key-serverxml #使用key=key-serverxml path: server.xml #value将 server.xml进行挂载 - key: key-loggingproperties #使用key=key-loggingproperties path: logging.properties #value将logging.properties进行挂载
进入到Pod cm-test-app 中的 cm-test-app container在/configfile目录下可以看到挂载的两个文件。
4.使用ConfigMap的限制条件
ConfigMap必须在Pod之前创建,Pod才能引用它 ConfigMap受命名空间限制,只有处于同一个命名空间的Pod才能引用它 ConfigMap无法用于静态pod 如果pod使用evnFrom基于ConfigMap定义环境变量,则无效的环境变量名称将会忽略,并记录在事件中
标签:ConfigMap,cm,name,Kubernetes,juli,key,apache,org 来源: https://blog.csdn.net/GaoChuang_/article/details/121174054