k8s-存储-configmap

k8s-存储-configmap

1 目录创建

—from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就

是文件的内容

 

$ 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=/root/k8s/configmap/docs #目录

 

查看创建记录

 

[root@k8s-master01 docs]# kubectl get cm
NAME          DATA   AGE
game-config   2      8s

查看详细记录

 

[root@k8s-master01 docs]# kubectl describe cm
Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

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

Events:  <none>


2 文件创建

kubectl create configmap game-config2 --from-file=/root/k8s/configmap/docs/game.properties 

3 使用字面值创建

使用文字值创建,利用 —from-literal 参数传递配置信息,该参数可以使用多次,格式如下

 

[root@k8s-master01 docs]#  kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created

 

查看详细信息

 

[root@k8s-master01 docs]# kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2020-03-17T22:43:45Z"
  name: special-config
  namespace: default
  resourceVersion: "106584"
  selfLink: /api/v1/namespaces/default/configmaps/special-config
  uid: a0588895-d7dd-4dbe-8424-fa5185aa83f4


4 pod中引用configmap

 

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 # 导入special-config这个configmap
            key: special.how 
      - name: SPECIAL_TYPE_KEY
        valueFrom:
          configMapKeyRef:
            name: special-config
            key: special.type
    envFrom:
      - configMapRef:
          name: env-config #导入env-config这个configmap
  restartPolicy: Never

查看pod日志,

 

log is DEPRECATED and will be removed in a future version. Use logs instead.
SPECIAL_TYPE_KEY=charm
SPECIAL_LEVEL_KEY=very
[root@k8s-master01 docs]# kubectl log dapi-test-pod |grep log
log is DEPRECATED and will be removed in a future version. Use logs instead.
log_level=INFO


5 用 ConfigMap 设置命令行参数

 

[root@k8s-master01 docs]# cat pod2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-dir
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
            key: special.how
      - name: SPECIAL_TYPE_KEY
        valueFrom:
          configMapKeyRef:
            name: special-config
            key: special.type
  restartPolicy: Never

6 通过数据卷插件使用ConfigMap

 

apiVersion: v1
kind: Pod
metadata:
  name: pod3
spec:
  containers:
    - name: test-container
      image: wangyanglinux/myapp:v1
      command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
  restartPolicy: Never

然后查看pod3的日志

 

7 configmap的热更新

创建pod

 

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

---
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 exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /etc/config/log_level
INFO
#然后编辑log-config,由info改成error等待大概 10 秒钟时间,再次查看环境变量的值

[root@k8s-master01 docs]# kubectl edit configmap log-config
configmap/log-config edited
# 再次查看
[root@k8s-master01 docs]# kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /etc/config/log_level
ERROR


8 注意

ConfigMap 更新后滚动更新 Pod

更新 ConfigMap 目前并不会触发相关 Pod 的滚动更新,可以通过修改 pod annotations 的方式强制触发滚动更新

 

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

 

这个例子里我们在 .spec.template.metadata.annotations 中添加 version/config ,每次通过修改version/config 来触发滚动更新

 

!!! 更新 ConfigMap 后:

使用该 ConfigMap 挂载的 Env 不会同步更新

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

posted @ 2020-04-15 10:09  huningfei  阅读(209)  评论(0编辑  收藏  举报
levels of contents