ConfigMap 使用 | kubernetes 篇

AlicFeng發表於2019-10-27

筆記分享 | Note Share

No matter where I am, I will reply you immediately when I see the email.My Email:

echo "YUBzYW1lZ28uY29tCg==" | base64 -d

目的

應用程式部署的資訊和程式進行模組分離

特點

  • 生成為容器內的環境變數

  • 設定容器啟動的命令引數

  • volume 形式掛載成容器內的檔案或目錄

示例

使用檔案建立 configmap
# 建立 configmap
➜ kubectl create configmap one.json --from-file=/Users/alicfeng/demo/configmap/one.json
configmap/one.json created

➜ kubectl create configmap one.json --from-file=/Users/alicfeng/demo/configmap/one.ini
configmap/one.json created

# 檢視 configmap
➜ kubectl get configmap
NAME       DATA   AGE
one.ini    1      43m
one.json   1      43m

# 檢視具體 configmap
➜ kubectl describe configmap one.ini
Name:         one.ini
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
one.ini:
----
name=alicfeng

Events:  <none>
Pod的yaml
apiVersion: v1
kind: Pod
metadata:
    name: web
    labels:
        name: web
spec:
    containers:
        - name: socket
          image: alicfeng/web:socket
          ports:
              - containerPort: 5200
          volumeMounts:
              # 掛載目錄
              - name: config
                mountPath: /var/www/config
              # 對映掛載檔案
              - name: one-ini
                mountPath: /var/www/config/one.ini
                subPath: one.ini
                readOnly: True
              - name: one-json
                mountPath: /var/www/config/one.json
                subPath: one.json
                readOnly: True
    volumes:
        # 宣告主機目錄
        - name: config
          hostPath:
              path: /Users/alicfeng/demo/configmap/config
        # 宣告定義configMap
        - name: one-ini
          configMap:
              name: one.ini
        - name: one-json
          configMap:
              name: one.json
建立pod容器

假設執行失敗 kubectl describe 檢視具體日誌

# 建立 pod
➜  configmap kubectl create -f demo.yaml
pod/web created

價值源於技術,貢獻源於分享 | 筆記分享歸檔
No matter where I am, I will reply you immediately when I see the email.
My Email: echo "YUBzYW1lZ28uY29tCg==" | base64 -d
個人比較喜歡分享,若有不對的地方非常感謝指出
相互學習、共同進步~

相關文章