天天看點

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引用它之前建立

繼續閱讀