k8s- Storage -configmap

1 directories creating

—from-file Specifies that all files in the directory will be used in ConfigMap Create a key value pair in it , The name of the key is the file name , It's worth it

It's the content of the document

 

$ 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 # Catalog

 

See create record

 

[[email protected] docs]# kubectl get cm
NAME          DATA   AGE
game-config   2      8s

View detailed records

 

[[email protected] 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 File creation

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

3 Use literals to create

Use text values to create , utilize —from-literal Parameters pass configuration information , This parameter can be used multiple times , The format is as follows

 

[[email protected] docs]#  kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created

 

View details

 

[[email protected] 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 I quote 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 #  Import special-config This configmap
            key: special.how 
      - name: SPECIAL_TYPE_KEY
        valueFrom:
          configMapKeyRef:
            name: special-config
            key: special.type
    envFrom:
      - configMapRef:
          name: env-config # Import env-config This configmap
  restartPolicy: Never

see pod journal ,

 

log is DEPRECATED and will be removed in a future version. Use logs instead.
SPECIAL_TYPE_KEY=charm
SPECIAL_LEVEL_KEY=very
[[email protected] 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 use ConfigMap Set command line parameters

 

[[email protected] 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 Using the data volume plug-in 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

Then check it out pod3 Log

 

7 configmap Hot update of

establish 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

 

verification :

 

kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /etc/config/log_level
INFO
# Then edit log-config, from info Change to error Wait about  10  Seconds , Look at the value of the environment variable again
[[email protected] docs]# kubectl edit configmap log-config
configmap/log-config edited
#  Look again
[[email protected] docs]# kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat /etc/config/log_level
ERROR

8 Be careful

ConfigMap After updating, scroll to update Pod

to update ConfigMap It's not going to trigger at this point Pod The rolling update of , It can be modified by pod annotations To force a rollover

 

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

 

In this case, we are .spec.template.metadata.annotations Add version/config , Every time through modification version/config To trigger a rollover