k8s study notes-P37-storage-configmap

Tutorial: Shang Silicon Valley Kubernetes Tutorial (K8s entry to proficient)_bilibili_bilibili

Notes taken from the video chapter: Chapter 7 Service Ingress


theme

Many applications rely on some configuration information during their initialization or runtime. Most of the time, there is a need to adjust the values ​​set by configuration parameters. ConfigMap is the method Kubernetes uses to inject configuration data into application Pods.

Can be understood as a configuration file management center

notes

Create from directory

root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# cat config_map_path/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

root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# cat config_map_path/ui.properties

color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl describe cm jjh-test-config


Name:         jjh-test-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



BinaryData
====

Events:  <none>
复制代码

Create from file

 $ kubectl create configmap game-config-2 --from-file=docs/user- guide/configmap/kubectl/game.properties
 $ kubectl get configmaps game-config-2 -o yaml
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl create configmap jjh-test-config-fromfile --from-file=./config_map_path/game.properties
configmap/jjh-test-config-fromfile created
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl describe cm jjh-test-config-fromfile
Name:         jjh-test-config-fromfile
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



BinaryData
====

Events:  <none>
复制代码

Create with literal value

 $ kubectl create configmap special-config --from-literal=special. how=very --from- literal=special.type=charm
 $ kubectl get configmaps special-config -o yaml

复制代码

Practice using configmap

use in env

  • list of portmaps
apiVersion: v1
kind: ConfigMap
metadata:
    name: special-config
    namespace: default
data:
    special.how: very
    special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
    name: env-config
    namespace: default
data:
    log_level: INFO
复制代码
  • pod manifest
apiVersion: v1
kind: Pod
metadata:
    name: portmap-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
复制代码
  • View setup results

root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl logs portmap-test-pod|grep -E "SPECIAL_LEVEL_KE|SPECIAL_TYPE_KEY" SPECIAL_TYPE_KEY=charm SPECIAL_LEVEL_KEY=very

pod command line parameters use

apiVersion: v1
kind: Pod
metadata:
    name: portmap-test-pord-cmd
spec:
    containers:
        - name: test-
          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
复制代码

portmapConfig is mounted under volume

  • List of resources
apiVersion: v1
kind: Pod
metadata:
    name: portmap-test-pod-volume
spec:
    containers:
        - name: test-container
          image: wangyanglinux/myapp:v1
          command: [ "/bin/sh", "-c", "sleep 360; cat /etc/config/special.how" ]
          volumeMounts:
              - name: config-volume
                mountPath: /etc/config
    volumes:
        - name: config-volume
          configMap:
              name: special-config
    restartPolicy: Never

复制代码
  • Apply it, then check the configuration
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl apply -f p38_portmap_pod_volume.yaml
pod/portmap-test-pod-volume created

root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl exec portmap-test-pod-volume -- cat  /etc/config/special.how
very

复制代码

The advantage of exporting the configmap configuration to the file in the pod is that it supports triggering rolling updates with a delay of about 10s. If in env, no rolling update

Guess you like

Origin juejin.im/post/7079651562468933669