把應用的代碼和配置分開,經過配置configmap管理pod,一種統一的集羣配置管理方案。
ConfigMap API資源提供了將配置數據注入容器的方式,同時保持容器是不知道Kubernetes的。ConfigMap能夠被用來保存單個屬性,也能夠用來保存整個配置文件或者JSON二進制等對象。php
ConfigMap是存儲通用的配置變量的。ConfigMap有點兒像一個統一的配置文件,使用戶能夠將分佈式系統中用於不一樣模塊的環境變量統一到一個對象中管理;而它與配置文件的區別在於它是存在集羣的「環境」中的,而且支持K8s集羣中全部通用的操做調用方式。html
而資源的使用者能夠經過ConfigMap來存儲這個資源的配置,這樣須要訪問這個資源的應用就能夠同經過ConfigMap來引用這個資源。至關經過建立Configmap封裝資源配置。java
configmap以一個或者多個key:value的形式保存在k8s系統中供應用使用,既能夠用於表示一個變量的值(eg.apploglevel:info),也能夠用於表示一個完整配置文件的內容(eg: server.xml=<?xml...>...)
能夠經過yaml配置文件或者直接用kubectl create configmap 命令行的方式來建立 ConfigMap。nginx
能夠經過創建ConfigMap來支持應用從環境變量和包含配置數據的文件讀取信息,以下面例子了展現了configMap如何得到這兩種配置的。git
data 一欄包括了配置數據。就如同大家看到的那樣,ConfigMap能夠被用來保存單個屬性,也能夠用來保存一個配置文件。github
(1) 從目錄中建立
該目錄中已經存在一些配置文件,並且目錄中全部的文件都將做爲configMap中的數據,一個文件爲一個data。
key的名字爲文件的名字,value爲文件的內容。kubectl create configmap game-config --from-file=/opt/configmap/file/
web
[root@master file]# ls game.properties ui.properties
[root@master file]# kubectl create configmap game-config --from-file=/opt/configmap/file/ [root@master file]# kubectl describe configmap game-config Name: game-config Namespace: default Labels: <none> Annotations: <none> Data ==== game.properties: 158 bytes ui.properties: 83 bytes
經過kubectl get configmap game-config -o yaml
能夠看到value的值。
(2) 從文件中建立apache
[root@master file]# kubectl create configmap game-config2 --from-file=/opt/configmap/file/game.properties --from-file=/opt/configmap/file/ui.properties [root@master file]# kubectl describe configmap game-config2 Name: game-config2 Namespace: default Labels: <none> Annotations: <none> Data ==== game.properties: 158 bytes ui.properties: 83 bytes
(3) 從文本中建立,直接指定key的名字vim
[root@master file]# kubectl create configmap game-config-3 --from-file=game-special-key=/opt/configmap/file/game.properties
[root@master file]# kubectl describe configmap game-config-3
Name: game-config-3
Namespace: default
Labels: <none>
Annotations: <none>
Data ==== game-special-key: 158 bytes
(4) 從文字值(literal values)中建立segmentfault
[root@master yaml]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm [root@master yaml]# kubectl get configmaps special-config -o yaml apiVersion: v1 data: special.how: very special.type: charm kind: ConfigMap metadata: creationTimestamp: 2017-03-02T15:28:29Z name: special-config namespace: default resourceVersion: "128440" selfLink: /api/v1/namespaces/default/configmaps/special-config uid: e30d9c0f-ff5c-11e6-9929-000c29962b4f [root@master yaml]# kubectl describe configmap special-config Name: special-config Namespace: default Labels: <none> Annotations: <none> Data ==== special.how: 4 bytes special.type: 5 bytes
例子1
apiVersion: v1 kind: ConfigMap metadata: name: special namespace: default data: special.how: very special.type: charm
例子2
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
example.property.1: hello example.property.2: world example.property.file: |- property.1=value-1 property.2=value-2 property.3=value-3
[root@master yaml]# kubectl describe configmap example-config
Name: example-config
Namespace: default
Labels: <none>
Annotations: <none>
Data ==== example.property.1: 5 bytes example.property.2: 5 bytes example.property.file: 56 bytes
例子3
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-serverxml
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>
ConfigMap供容器使用的典型用法以下:
1.生成爲容器內的環境變量。
2.設置容器啓動命令的啓動參數(需設置爲環境變量)
3.以Volume的形式掛載爲容器內部的文件或者目錄
(1)經過環境變量獲取定義好的ConfigMap 中的內容。
apiVersion: v1 kind: ConfigMap metadata: name: appvars data: apploglevel: info appdatadir: /var/data
apiVersion: v1 kind: Pod metadata: name: cm-test-pod spec: containers: - name: cm-test image: test-registry:5000/busybox imagePullPolicy: IfNotPresent command: [ "/bin/sh", "-c", "env | grep APP" ] env: - name: APPLOGLEVEL valueFrom: configMapKeyRef: name: appvars key: apploglevel - name: APPDATADIR valueFrom: configMapKeyRef: name: appvars key: appdatadir restartPolicy: Never
經過kubectl logs cm-test-pod
能夠看到pod曾經執行過的結果.
[root@master ~]# kubectl logs cm-test-pod APPDATADIR=/var/data APPLOGLEVEL=info
(2) 經過volume掛載的方式將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"
apiVersion: v1 kind: Pod metadata: name: cm-test-app spec: containers: - name: cm-test-app image: test-registry:5000/tomcat ports: - containerPort: 8080 volumeMounts: - name: serverxml mountPath: /configfiles volumes: - name: serverxml configMap: name: cm-appconfigfiles items: - key: key-serverxml path: server.xml - key: key-loggingproperties path: logging.properties
[root@master volume]# kubectl exec -it cm-test-app /bin/bash root@cm-test-app:/usr/local/tomcat# cd /configfiles/ root@cm-test-app:/configfiles# ls logging.properties server.xml
(3).設置容器啓動命令的啓動參數
apiVersion: v1 kind: ConfigMap metadata: name: var namespace: default data: special.how: very special.type: charm
apiVersion: v1 kind: Pod metadata: name: var-test spec: containers: - name: test-container image: test-registry:5000/busybox command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ] env: - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: var key: special.how - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: var key: special.type restartPolicy: Never
[root@master var]# kubectl logs var-test very charm
apiVersion: v1 kind: ConfigMap metadata: name: configmap-demo namespace: default data: index.html: | Hello Everyone
apiVersion: v1 kind: ReplicationController metadata: name: configmap-demo2 spec: template: metadata: labels: app: configmap-demo2 spec: containers: - name: configmap-demo2 image: test-registry:5000/nginx ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /usr/share/nginx/html/ volumes: - name: config-volume configMap: name: configmap-demo
[root@master change]# curl http://172.25.2.2:80 Hello Everyone
修改
apiVersion: v1 kind: ConfigMap metadata: name: configmap-demo namespace: default data: index.html: | Hello World!
[root@master change]# kubectl replace -f k8s-configmap.yaml [root@master change]# curl http://172.25.2.2:80 Hello World!
修改configmap會有延時,要過一段時間容器的配置纔會發生變化。
參考:
https://kubernetes.io/docs/user-guide/configmap/
http://www.javashuo.com/article/p-moxembsm-mp.html
https://github.com/thrawn01/configmap-microservice-demo