ConfigMap

一、Pod的配置管理

应用部署的一个最佳实践是将应用所需的配置信息和程序进行分离,这样就可以是应用程序被更好的服用,通过不同的配置也能实现更灵活的功能。ConfigMap允许将配置文件和镜像文件分离,以是容器化的应用程序具有可移植性。

二、ConfigMap概述

ConfigMap供容器使用的典型用法如下:

  1. 生成为容器内的环境变量

  2. 设置容器启动命令的启动参数(需设置为环境变量)

  3. 以volume的形式挂在为容器内部的文件或目录

     

    ConfigMap以一个或多个key:value的形式保存在Kubernetes系统中供应用使用,即可以表示一个变量的值(apploglevel=info),也可以表示一个完整的配置文件的内容(server.xml=<?xml...>...)

三、创建ConfigMap资源对象

3.1 cm-appvars.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-appvars
data:
  apploglevel: info
  appdatadir: /var/data

 

创建ConfigMap

kubectl create -f cm-appvars.yaml

 

查看ConfigMap

kubectl get configmap
NAME         DATA   AGE
cm-appvars   2      12s
kubectl describe configmap/cm-appvars
Name:         cm-appvars
Namespace:    default
Labels:       <none>
Annotations:  
Data
====
appdatadir:
----
/var/data
apploglevel:
----
info
Events:  <none>
kubectl get configmap/cm-appvars -o yaml
​
apiVersion: v1
data:
  appdatadir: /var/data
  apploglevel: info
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"appdatadir":"/var/data","apploglevel":"info"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-appvars","namespace":"default"}}
  creationTimestamp: "2020-06-10T02:45:21Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:appdatadir: {}
        f:apploglevel: {}
      f:metadata:
        f:annotations:
          .: {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
    manager: kubectl
    operation: Update
    time: "2020-06-10T02:45:21Z"
  name: cm-appvars
  namespace: default
  resourceVersion: "268969"
  selfLink: /api/v1/namespaces/default/configmaps/cm-appvars
  uid: e9f40932-d0de-4419-8e94-cc8941c20e6c

 

3.2 同一目录多个文件创建ConfigMap

分别设置key为配置文件别名,value则是配置文件文本内容

game.properties
​enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
 

使用上述两个文件创建ConfigMap

# kubectl create configmap cm-appconfigfiles --from-file=/usr/local/k8s/configmap/configmapfiles/
​
configmap/cm-appconfigfiles created

 

查看创建的cm-appconfigfiles

# kubectl get configmap
NAME                DATA   AGE
cm-appconfigfiles   2      10s


# kubectl describe configmap cm-appconfigfiles
Name:         cm-appconfigfiles
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
​
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events:  <none>
# kubectl get  configmap cm-appconfigfiles -o yaml
apiVersion: v1
data:
  game.properties: |-
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: "2020-06-10T05:56:50Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:game.properties: {}
        f:ui.properties: {}
    manager: kubectl
    operation: Update
    time: "2020-06-10T05:56:50Z"
  name: cm-appconfigfiles
  namespace: default
  resourceVersion: "283535"
  selfLink: /api/v1/namespaces/default/configmaps/cm-appconfigfiles
  uid: d3dbf64d-b381-47b6-9fc6-fbdc286f80c0
 

3.3 根据文字值创建ConfigMap

--from-literal

# kubectl create configmap cm-appenv --from-literal=loglevel=info --from-literal=appdatadir=/var/data

 

查看创建的configmap

# kubectl create configmap cm-appenv --from-literal=loglevel=info --from-literal=appdatadir=/var/data
​
# kubectl get configmap
NAME                DATA   AGE
cm-appconfigfiles   2      7m42s
cm-appenv           2      6s
cm-appvars          2      3h19m
​
​
kubectl describe configmap cm-appenv
Name:         cm-appenv
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
appdatadir:
----
/var/data
loglevel:
----
info
Events:  <none>

 

四、在Pod中使用ConfigMap

4.1 通过环境变量方式使用ConfigMap

以cm-appvars ConfigMap为例

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-appvars
data:
  apploglevel: info
  appdatadir: /var/data

 

在Pod “cm-test-pod”中,将ConfigMap “cm-appvars”中的内容以环境变量的方式设置为容器内部的环境变量

apiVersion: v1
kind: Pod
metadata:
  name: cm-test-pod
spec:
  containers:
  - name: cm-test
    image: busybox
    command: ["/bin/bash","-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 logs cm-test-pod
APPDATADIR=/var/data
APPLOGLEVEL=info

 

4.2 envFrom

Kubernetes从1.6版本开始,引入一个新的字段envFrom,实现了在Pod环境中将ConfigMap中所有定义的key=value自动生成为环境变量

apiVersion: v1
kind: Pod
metadata:
  name: cm-envfrom-pod
spec:
  containers:
  - name: envfrom-test
    image: busybox
    command: ["sh","-c","env"]
    envFrom:
    - configMapRef:
        name: cm-appvars
  restartPolicy: Never

 

查看容器环境变量

 

# kubectl logs cm-envfrom-pod
apploglevel=info
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=cm-envfrom-pod
MYWEB_PORT_8080_TCP_ADDR=10.111.193.215
MYWEB_SERVICE_HOST=10.111.193.215
SHLVL=1
HOME=/root
MYWEB_PORT_8080_TCP_PORT=8080
MYWEB_PORT_8080_TCP_PROTO=tcp
MYWEB_SERVICE_PORT=8080
MYWEB_PORT=tcp://10.111.193.215:8080
MYWEB_PORT_8080_TCP=tcp://10.111.193.215:8080
MYSQL_PORT_3306_TCP_ADDR=10.109.55.96
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
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_SERVICE_HOST=10.109.55.96
MYSQL_PORT_3306_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PROTO=tcp
appdatadir=/var/data
MYSQL_SERVICE_PORT=3306
MYSQL_PORT=tcp://10.109.55.96:3306
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
MYSQL_PORT_3306_TCP=tcp://10.109.55.96:3306
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/

 


 

posted @ 2020-08-14 09:39  Bigberg  阅读(460)  评论(0编辑  收藏  举报