k8s-kubernetes storage

storage

configMap

configMap descriptions

ConfigMap feature introduced in version Kubernetes1.2, many from the application configuration file, command line parameters or environment variable to read configuration information. ConfigMap API provides us injection mechanism configuration information to the vessel, may be used to hold The ConfigMap single attribute may be used to save the entire configuration file or binary large object JSON

ConfigMap creation

1, create a directory
$1s docs/user-guide/configmap/kubect1/ 
game.properties
ui.properties

$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
livess=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/kubect1
GET cm kubectl 
kubectl GET cm Game-config
kubectl GET cm Game-config -o YAML - view the configuration
kubectl describe cm game-config - View configuration

-From-file all the documents specified in the directory will be used in ConfigMap which create a key-value pair, the key is the name of the file name, file content is the value of

Note: The key-value pairs with no spaces behind
Ⅱ, using file creation

Just specify ConfigMap you can create a single file from a file

kubectl create configmap game-config-2 --from-file=/root/configmap/dir/gameconfig.properties

kubectl get cm game-config-2 -o yaml

-from-file This parameter can be used multiple times, you can use two configuration files to specify the two previous instances, respectively, just specify the effect is the same as the entire directory

Ⅲ, using the literal creation

Use text value is created, using the configuration information transmitted -from-literal parameter, which can be used several times, the following format

kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

$ kubectl get configmaps special-config -o yaml

Pod used ConfigMap

Ⅰ, instead of using the environmental variables ConfigMap
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
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
  - name: test-container
    image: hub.atguigu.com/library/myapp:v1
    command: ["/bin/sh", "-c", "env"]
    env:
      - name: SPECIAL_TYPE_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
Ⅱ, provided with a command line parameter ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
  - name: test-container
    image: hub.atguigu.com/library/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
Ⅲ, plug-in data volume by using ConfigMap

Using this ConfigMap in data volumes inside, there are different options. The most basic is to fill in data files volume, in this document, the key is the file name, file content is key

apiversion: v1
kind: Pod
metadata:
name: dapi-test-pod3
spec:
containers:
  - name: test-container
    image: hub.atguigu.com/library/myapp:v1
    command: [ "/bin/sh", "-c", "sleep 600s" ]
    volumeMounts:
      - name: config-volume
        mountPath: /etc/config
volumes:
  - name: config-volume
    configMap:
      name: special-config
restartPolicy: Never

ConfigMap hot update

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: hub.atguigu.com/library/myapp:v1
        ports:
          - containerPort: 80
        volumeMounts:
          - name: config-volume
            mountPath: /etc/config
    volumes:
      - name: config-volume
        configMap:
          name: log-config
[root@k8s-master01 ~]# kubectl exec my-nginx-7b55868ff4-wqh2q -it -- cat /etc/config/log_level
INFO

Modify ConfigMap

$ kubectl edit configmap log-config

Modify 10g-level value of DEBUG wait about 10 seconds to view the values ​​of environment variables again

$ kubect1 exec 'kubectl get pods -1 run=my-nginx -osname|cut -d "/"-f2 cat /tmp/1oglevel DEBUG

Pod update after update rolling ConfigMap

Update ConfigMap currently does not trigger the rollover-related Pod, you can force the trigger rollover pod annotations by modifying the way

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

For this example we are. Annotations added version / config in. Spec.template.metadata, each time by modifying the version / config to trigger rollover

After updating ConfigMap:

  • Use the ConfigMap mounted Env not synchronize updates

  • Volume data using the ConfigMap mounted in the required period of time (measured about 10 seconds) to synchronize updates

Guess you like

Origin www.cnblogs.com/eadela/p/11987933.html