kubernetes的存储ConfigMap

标签(空格分隔): kubernetes系列


  • 一: kubernetes的存储ConfigMap

一:kubernetes的存储

1.1 configMap 描述信息

ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配
置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也
可以用来保存整个配置文件或者 JSON 二进制大对象

配置中心:

image_1e2sbd27410d2q9p1s17fh1g5e9.png-171.8kB

image_1e2sbj9al12j771ci7sj081rs3m.png-71.3kB

1.1.1 configmap 的创建

1. 使用目录创建

$ ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties

$ cat docs/user-guide/configmap/kubectl/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

$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl
mkdir configmap/dir -p 

cd configmap/dir/

vim 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
---

vim ui.properties
---
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
---
—from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就
是文件的内容

kubectl create configmap game-config --from-file=../dir/

kubectl get cm game-config -o yaml

kubectl describe cm game-config

image_1e2sc97ne1lhg15c57k112kge5i1j.png-64.2kB

image_1e2scb81l113s9ag36jfne8ea20.png-145.8kB

image_1e2scfg2519h41a7t1u3abjn1hbp2d.png-104.6kB


1.1.2 使用文件创建

只要指定为一个文件就可以从单个文件中创建 ConfigMap

kubectl create configmap game-config-2 --from-file=game.properties

kubectl describe cm game-config-2

kubectl get configmaps game-config-2 -o yaml

—from-file 这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个
目录是一样的

image_1e2t54mu2j4j13jrdip1lrg11s02q.png-136.4kB

image_1e2t55hfs8dfuitkd89uq1kh237.png-114kB


1.1.3 使用字面值创建

使用文字值创建,利用  —from-literal 参数传递配置信息,该参数可以使用多次,格式如下
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

kubectl get cm 

kubectl get cm special-config -o yaml

image_1e2t5f66e1sii1fnn6c1rtp1tri3n.png-164.1kB

1.1.4 Pod 中使用 ConfigMap

使用 ConfigMap 来替代环境变量

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

----

----
vim  env.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

-----------
kubectl apply -f  env.yaml

vim evnpod.yaml

------
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: wangyanglinux/myapp:v1
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
      envFrom:
       - configMapRef:
           name: env-config
  restartPolicy: Never

---

kubectl apply -f evnpod.yaml

image_1e2t8v21j59ulfur7s1kvg1joq44.png-60.7kB

image_1e2t904b2gto166d1i5nslaflh4h.png-179.5kB

1.1.5 用 ConfigMap 设置命令行参数

vim pod1.yaml
----
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config-1
  namespace: default
data:
  special.how: very
  special.type: charm
---
kubectl apply -f pod1.yaml

image_1e36nais81d7f10ph1tmkscsqu9.png-126kB


vim pod2.yaml

----
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod1
spec:
  containers:
    - name: test-container
      image: wangyanglinux/myapp:v1
      command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config-1
              key: special.how
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config-1
              key: special.type
  restartPolicy: Never

----
kubectl apply -f pod2.yaml

image_1e36ndu95u181fk4n1j2m21h5p16.png-150.7kB

1.1.6 通过数据卷插件使用ConfigMap

vim stoage1.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config-2
  namespace: default
data:
  special.how: very
  special.type: charm
---

kubectl apply -f stoage1.yaml

image_1e36nss82uudr5v5h119gpemk2t.png-195.4kB

在数据卷里面使用这个 ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文
件名,键值就是文件内容

vim pod3.yaml
---

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod4
spec:
  containers:
    - name: test-container
      image: wangyanglinux/myapp:v1
      command: [ "/bin/sh", "-c", "sleep 600" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config-2
  restartPolicy: Never
---
kubectl apply -f pod3.yaml

image_1e36om2o910ms1b3u1v4h18hg5gs3a.png-145.2kB

1.1.7 ConfigMap 的热更新

vim log-cm.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: log-config
  namespace: default
data:
  log_level: INFO

---
kubectl apply -f log-cm.yaml

image_1e36oso8v1gcc1oga4cb1if91gqu3n.png-192.7kB


vim pod4.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
       containers:
       - name: my-nginx
         image: wangyanglinux/myapp:v1
         ports:
         - containerPort: 80
         volumeMounts:
         - name: config-volume
           mountPath: /etc/config
       volumes:
         - name: config-volume
           configMap:
             name: log-config
---
kubectl apply -f pod4.yaml 

image_1e36pj5c110hs1asi3nu52o1kff4k.png-99.8kB

image_1e36pmu2d1pen3h8h3c1sicff851.png-108.2kB

修改  log_level 的值为  DEBUG 等待大概 10 秒钟时间,再次查看环境变量的值

将 log—level 改为DEBUG 

kubectl edit cm log_config
---
log_level : DEBUG
---

kubectl exec my-nginx-c484b98b4-tgblg -ti -- cat /etc/config/log_level

image_1e36pt3k4gfdb9515g81ui81al45e.png-185.9kB

image_1e36pu2b7u2v1pjc1sfi19ue1c8k5r.png-122.5kB

ConfigMap 更新后滚动更新 Pod
更新 ConfigMap 目前并不会触发相关 Pod 的滚动更新,可以通过修改 pod annotations 的方式强制触发滚动更新
这个例子里我们在  .spec.template.metadata.annotations 中添加  version/config ,每次通过修改
version/config 来触发滚动更新

!!! 更新 ConfigMap 后:
使用该 ConfigMap 挂载的 Env 不会同步更新
使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新

kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations":
{"version/config": "20190411" }}}}}

image_1e36qd38cnp71tqmasf5bu1f0v68.png-39.8kB