k8s-configmap

Posted ilinux_one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s-configmap相关的知识,希望对你有一定的参考价值。

一.目的

把应用的代码和配置分开,通过配置configmap管理pod,一种统一的集群配置管理方案。
ConfigMap API资源提供了将配置数据注入容器的方式,同时保持容器是不知道Kubernetes的。ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制等对象。

二.基本原理

ConfigMap是存储通用的配置变量的。ConfigMap有点儿像一个统一的配置文件,使用户可以将分布式系统中用于不同模块的环境变量统一到一个对象中管理;而它与配置文件的区别在于它是存在集群的“环境”中的,并且支持K8s集群中所有通用的操作调用方式。

而资源的使用者可以通过ConfigMap来存储这个资源的配置,这样需要访问这个资源的应用就可以同通过ConfigMap来引用这个资源。相当通过创建Configmap封装资源配置。

configmap以一个或者多个key:value的形式保存在k8s系统中供应用使用,既可以用于表示一个变量的值(eg.apploglevel:info),也可以用于表示一个完整配置文件的内容(eg: server.xml=<?xml...>...)
可以通过yaml配置文件或者直接用kubectl create configmap 命令行的方式来创建 ConfigMap。

三.ConfigMap 配置

可以通过建立ConfigMap来支持应用从环境变量和包含配置数据的文件读取信息,如下面例子了展示了configMap如何获得这两种配置的。

技术分享
图片.png


data 一栏包括了配置数据。就如同你们看到的那样,ConfigMap可以被用来保存单个属性,也可以用来保存一个配置文件。

1.kubectl 命令行方式创建

(1) 从目录中创建
该目录中已经存在一些配置文件,而且目录中所有的文件都将作为configMap中的数据,一个文件为一个data。
key的名字为文件的名字,value为文件的内容。
kubectl create configmap game-config --from-file=/opt/configmap/file/

[[email protected]master file]# ls
game.properties  ui.properties
[[email protected] file]# kubectl  create configmap game-config --from-file=/opt/configmap/file/
[[email protected] 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) 从文件中创建

[[email protected] file]# kubectl create configmap game-config2 --from-file=/opt/configmap/file/game.properties --from-file=/opt/configmap/file/ui.properties
[[email protected] 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的名字

[[email protected] file]# kubectl create  configmap game-config-3 --from-file=game-special-key=/opt/configmap/file/game.properties
[[email protected] 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)中创建

[[email protected] yaml]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
[[email protected] 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
[[email protected] yaml]# kubectl  describe configmap special-config
Name:        special-config
Namespace:    default
Labels:        <none>
Annotations:    <none>

Data
====
special.how:    4 bytes
special.type:    5 bytes

2.yaml文件方式创建

例子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
[[email protected] 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的使用

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曾经执行过的结果.

[[email protected] ~]# 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
[[email protected] volume]# kubectl  exec -it cm-test-app /bin/bash
[email protected]test-app:/usr/local/tomcat# cd /configfiles/
[email protected]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
[[email protected]master var]# kubectl  logs var-test
very charm

五. ConfigMap修改

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
[[email protected]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!
[[email protected]master change]# kubectl  replace -f k8s-configmap.yaml
[[email protected]master change]# curl http://172.25.2.2:80
Hello World!

修改configmap会有延时,要过一段时间容器的配置才会发生变化。

参考:
https://kubernetes.io/docs/user-guide/configmap/
https://segmentfault.com/a/1190000004940306
https://github.com/thrawn01/configmap-microservice-demo














以上是关于k8s-configmap的主要内容,如果未能解决你的问题,请参考以下文章

K8S-ConfigMap与Secret

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板