天天看点

Kubernetes - 6.1 Config and Storage - ConfigMap

什么是ConfigMap

ConfigMap为Pod中的容器提供了配置文件、环境变量等非敏感信息,通过ConfigMap可以将Pod和其他组件分开,这将使得Pod更加有移植性,使得配置更加容器更改及管理,也使得Pod更加规范。

Config基本操作

通过kubectl create configmap

kubectl create configmap nginx-configmap --from-literal=password=123456

Kubernetes - 6.1 Config and Storage - ConfigMap

通过yaml资源配置清单

kubectl apply -f nginx-configmap.yaml

apiVersion: v1
kind: List
metadata:
items:
- apiVersion: v1
  data:
    password: "123456"
  kind: ConfigMap
  metadata:
    name: nginx-configmap           

通过

kubectl get configmap

查看详细信息

Kubernetes - 6.1 Config and Storage - ConfigMap

kubectl describe configmap

Kubernetes - 6.1 Config and Storage - ConfigMap

ConfigMap作为存储卷被Pod调用

创建ConfigMap

kubectl create configmap database-config --from-literal=user=root --from-literal=password=123456

Kubernetes - 6.1 Config and Storage - ConfigMap

通过YAML资源定义清单创建Pod并绑定ConfigMap为存储卷

kubectl apply -f nginx-pod-configmap-volume.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.16
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: database-config            

查看Pod容器挂载的ConfigMap存储卷

kubectl exec -it nginx-pod /bin/bash

Kubernetes - 6.1 Config and Storage - ConfigMap

查看Pod详细信息,挂载了ConfigMap类型的Volumes

kubectl describe pod nginx-pod

Kubernetes - 6.1 Config and Storage - ConfigMap

ConfigMap作为环境变量被Pod调用

kubectl create configmap database-config --from-literal=user=root --from-literal=password=123456

kubectl create configmap env-config --from-literal=LOG_LEVEL=ERROR

Kubernetes - 6.1 Config and Storage - ConfigMap

kubectl apply -f nginx-pod-configmap-env.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
    - name: nginx
      image: nginx:1.16
      env:
        - name: DB_USER_CONFIG
          valueFrom:
            configMapKeyRef:
              name: database-config
              key: user
        - name: DB_PASSWORD_CONFIG
          valueFrom:
            configMapKeyRef:
              name: database-config
              key: password
      envFrom:
        - configMapRef:
            name: env-config           

查看Pod容器的环境变量

kubectl exec -it nginx-pod /bin/bash

Kubernetes - 6.1 Config and Storage - ConfigMap

查看Pod容器详细信息

kubectl describe pod nginx-pod

Kubernetes - 6.1 Config and Storage - ConfigMap

Config注意的事项

  1. ConfigMap文件大小限制: 1MB (etcd的限制)
  2. ConfigMap必须在Pod引用它之前创建

继续阅读