
這樣我們就可以建立一個連結到外部文件的設定映射。
kubectl create configmap database-config --from-file=database.properties
但是當我們編輯 yaml 時,它會顯示文件的完整內容都會轉儲到那裡。
無論如何,是否也只指向 yaml 中的一個文件,以便我將屬性文件保留在與 configmap yaml 相同的資料夾中,並使用以下命令應用資料夾中的所有 yaml:
kubectl apply -f target_folder\
請建議。
答案1
什麼configmap
是
ConfigMap 是一個 API 對象,用於以鍵值對形式儲存非機密資料。 Pod 可以將 ConfigMap 作為環境變數、命令列參數或磁碟區中的設定檔使用。
ConfigMap 可讓您將特定於環境的配置與容器映像解耦,以便您的應用程式易於移植。
換句話說,當configmap
從外部文件(如您的案例)或其他值創建時,API 伺服器將驗證一切是否正確,然後將其保存到etcd
.這也解釋了為什麼當你edit
看到configmap
它的整個背景時。發生這種情況是因為configmap
是從 讀取的etcd
,而不是從任何外部來源讀取的。
這也是不建議將大檔案儲存為配置映射或機密的原因之一 - 它會影響 kubernetes 叢集的效能,因為所有叢集的物件都儲存在etcd
.
客製化
configmap
這是實現根據同一目錄中的檔案執行一個命令來建立/配置的要求的方法之一。它是一個透過 kustomization 檔案自訂 Kubernetes 物件的獨立工具。
我創建了一個簡短的範例來展示這個想法:
$ tree
.
├── application.properties
└── kustomization.yaml
0 directories, 2 files
$ cat kustomization.yaml
generatorOptions:
disableNameSuffixHash: true # this flag is used to avoid creation of new configmap, instead it will be modified when file context is changed
configMapGenerator:
- name: application-config
files:
- application.properties
$ cat application.properties
listen.url=localhost:9010
client.url=some_url:3000
測試一下,這個命令將只渲染configmap,它不會創建它:
$ kubectl kustomize application.settings/
apiVersion: v1
data:
application.properties: |
listen.url=localhost:9010
client.url=some_url:3000
kind: ConfigMap
metadata:
name: application-config
最後一步是申請它:
$ kubectl apply -k application.settings/
configmap/application-config created
$ kubectl get cm
NAME DATA AGE
application-config 1 23s
對上面命令的簡短解釋:
若要套用這些資源,請使用 --kustomize 或 -k 標誌來執行 kubectl apply。