FuFu KK8s
  • K~K8s index
  • Kubernetes 的基礎世界
  • Why container is not Docker
  • Startup Local Kubernetes via Minikube
  • K8s Master node Component 介紹
  • NameSpace、Deployments 概念說明
  • 常見的 kubectl 指令
  • 回顧第一次部署,淺談 Pod、Deployment
  • 回顧第一次部署,淺談 Service
  • 今日來介紹 Pod 靜態文件~ Manifest
  • Pod 的健康檢查方式
  • Pod 的健康檢查方式 Part-2
  • Pod 的資源請求、上限
  • Pod 資料,如何持久化存放、讀取
  • Pod 副本管理~ 描述 ReplicaSet 控制器
  • Pod 副本管理~ 實作篇
  • 淺談 DaemonSet,及相對 ReplicaSet 的差異
  • 來說說 Label
  • 從 Label 再回頭談 Service
  • Service 續集之 Cluster IP、Kube-proxy、LoadBalancer
  • 從 Service 發現 K8s 網路層全貌
  • Container Data Persistent
  • 發現、初談 StatefulSet
  • 再談 StatefulSet
  • 認識 ConfigMap
  • 第一次使用 Play with Kubernetes
  • 手工 Installing kubeadm
  • 手工 Installing CRI-O、kubeadm init
  • 繼上篇,排查 kubelet、kubeadm init 問題
  • Installing a pod network add-on
  • K8s add Nodes(join)
  • 驗證自建的 K8s
  • 筆記
    • kubectl get 筆記
    • kubectl 部署筆記
    • kubectl describe nodes
  • 實務記憶篇
    • 整理下記憶
    • 有哪些 Kubernetes 雲端服務
    • 如何從本機連線至 GKE Pod
    • GCP Memorystore 服務介紹
    • 如何連線至 GCP Memorystore
    • GCP Cloud SQL 服務介紹
    • 如何連線至 Cloud SQL
    • 關於 GCP VPC 網路
    • HELM 工具用途
    • 臨時題目:查修 prometheus
    • 繼續離題:繼續查修Prometheus
    • 臨時題目:限定 Pod 訪問外網時,固定 public ip
    • K8s 監控數據來源 Prometheus
    • 監控要告警啊 AlertManager
    • 監控要有圖表啊 Grafana
    • Grafana收集Kubernetes系統資訊
    • 系統 Log 資料
    • 系統 Log 資料收集至 EFK
    • 關於 EFK 角色
    • 系統 Log 資料 - fluent-bit 串接
    • 開放網站對外存取
    • 網站提供 https 安全連線服務 - 憑證管理
    • 網站提供 https 安全連線服務 - 憑證與Ingress整合
    • 網站提供 https 安全連線服務 - Istio 範例
    • 需要額外的 非http 連線
    • Istio 初略介紹
    • Istio 整合 Certmanager DNS01
    • 番外篇:Istio 如何限制訪問來源
    • 番外篇:如何擴充 PV PVC storage size
    • 番外篇:如何利用 Binlog 還原資料庫
  • Helm 實務學習心得
    • Helm requirements 見解
    • 同環境,一次部署多個相同App
  • Python
    • 11-1 Firebase 資料庫簡介
Powered by GitBook
On this page
  • Examples
  • 建立 config
  • 建立 pod
  • 生效、連線 pod
  • 查看 ConfigMap
  • 回頭解析 Pod YAML
  • 環境變數
  • Command - 引用變數

認識 ConfigMap

ConfigMap 此定義檔內容,記載了軟體運行中所需要的:設定、環境變數、指令等等影響著軟體運作的必載元件。 這樣的設計可以很明確將設定檔案抽離出來,方便用來管理軟體運作。 ConfigMap 很適合用來存放「非敏感性」的未加密設定資訊,如有敏感性資料,則需透過 Secret。 某程度上 ConfigMap 也是實現 infrastructure as code 精神上的一環。

K8s 上,軟體就是 pod、設定檔案就是 ConfigMap。 建立 ConfigMap 的指令:

kubectl create configmap [NAME] [DATA]
kubectl apply configmap.yaml

Examples

  # Create a new configmap named my-config based on folder bar
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new configmap named my-config with specified keys instead of file basenames on disk
  kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

  # Create a new configmap named my-config with key1=config1 and key2=config2
  kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

  # Create a new configmap named my-config from the key=value pairs in the file
  kubectl create configmap my-config --from-file=path/to/bar

  # Create a new configmap named my-config from an env file
  kubectl create configmap my-config --from-env-file=path/to/bar.env
  
  # Delete a configmap named my-config
  kubectl delete configmap my-config

建立 config

kubectl create configmap my-config --from-file=my-config.txt \
    --from-literal=another-param=config1 \
    --from-literal=extra-param=config2

建立 pod

# 11-133-kuard-config.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kuard-config
spec:
  containers:
    - name: test-container
      image: gcr.io/kuar-demo/kuard-amd64:1
      imagePullPolicy: Always
      command:
        - "/kuard"
        - "$(EXTRA_PARAM)"
      env:
        - name: ANOTHER_PARAM
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: another-param
        - name: EXTRA_PARAM
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: extra-param
      volumeMounts:
        - name: config-volume
          mountPath: /config
  volumes:
    - name: config-volume
      configMap:
        name: my-config
  restartPolicy: Never

生效、連線 pod

# 生效 pod
kubectl apply -f 11-133-kuard-config.yaml

# 連線 pod
kubectl port-forward --address 0.0.0.0 kuard-config 30333:8080

查看 ConfigMap

# kubectl get cm my-config -o yaml
apiVersion: v1
data:
  another-param: config1
  extra-param: config2
  my-config.txt: |
    another-param = value1
    extra-param = value2
kind: ConfigMap
metadata:
  creationTimestamp: "2019-01-29T03:51:25Z"
  name: my-config
  namespace: default
  resourceVersion: "1426607"
  selfLink: /api/v1/namespaces/default/configmaps/my-config
  uid: 2644b22a-2379-11e9-8813-08002730aeb3

回頭解析 Pod YAML

環境變數

引用環境變數,定義在env - valueFrom範圍中,這會參照該my-config (ConfigMap)裡頭的 key 作為環境變數。

      env:
        - name: ANOTHER_PARAM
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: another-param
        - name: EXTRA_PARAM
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: extra-param

Command - 引用變數

使用命令列參數,可透過上述valueFrom環境變數引用, K8s 將使用$(Environment Variable)語法作為變數表達與引用。

  containers:
    - name: test-container
      image: gcr.io/kuar-demo/kuard-amd64:1
      imagePullPolicy: Always
      command:
        - "/kuard"
        - "$(EXTRA_PARAM)"

檔案系統

Pod 內透過volumeMounts定義了一個磁碟區命名為config-volume,並掛載於 /config 路徑。 實際磁碟來源,是依據ConfigMap內的 my-config 來建立的檔案系統。 在操作中進入 /config 路徑內,會看見ConfigMap的每個項目建立了檔案或目錄,

      volumeMounts:
        - name: config-volume
          mountPath: /config
  volumes:
    - name: config-volume
      configMap:
        name: my-config
Previous再談 StatefulSetNext第一次使用 Play with Kubernetes

Last updated 6 years ago