系統整理K8S的配置管理實戰-建議收藏系列

目錄

  • 一、ConfigMap
    • 1.1、創建
      • 1.1.1、from-file
      • 1.1.2、from-env-file
      • 1.1.3、from-literal
      • 1.1.4、基于yaml文件創建
    • 1.2、Pod使用ConfigMap
      • 1.2.1、valueFrom
      • 1.2.2、envFrom
      • 1.2.3、volumeMounts
      • 1.2.4、自定義文件名稱
      • 1.2.5、控制文件權限
      • 1.2.6、子目錄-subPath
      • 1.2.7、熱更新
      • 1.2.8、不可變的cm
    • 1.3、限制條件
  • 二、Secret
    • 2.1、創建
      • 2.1.1、from-file
      • 2.1.2、from-literal
      • 2.1.3、基于yaml文件創建
      • 2.1.4、from-env-file
    • 2.2、實戰
      • 2.2.1、配置阿里云私有倉庫密鑰
      • 2.2.2、管理https證書
      • 2.2.3、不可變的secret
原文公眾號鏈接:https://mp.weixin.qq.com/s/gZx-IIW1g9-Kl7yXw7tsEA原文公眾號鏈接:https://mp.weixin.qq.com/s/gZx-IIW1g9-Kl7yXw7tsEA
一、ConfigMap應用部署的最佳實踐都趨向于將應用所需要的配置信息和應用程序本身離開,以便實現通過不同的配置實現更靈活的功能 。
我們都知道,K8S中運行的是容器 , 若不將配置文件抽離 , 每一次配置文件的變動,都需要重新制作鏡像,這其實挺麻煩挺沒必要的 。
在K8S中提供了ConfigMap資源對象作為配置管理的統一管理方案,可以通過環境變量或者文件的方式 , 在創建容器的時候將配置信息動態注入到容器中~
下文開始對ConfigMap的實戰
官網文檔:https://kubernetes.io/zh/docs/concepts/configuration/configmap/
1.1、創建1.1.1、from-file準備目錄和文件
# 創建目錄及配置文件[root@master01 yamls]# ls conf/nginx.confredis.conf[root@master01 yamls]# cat conf/nginx.confapp.name=nginxlocation.prefix=/app[root@master01 yamls]# cat conf/redis.confip=10.10.10.101port=2379創建cm
[root@master01 yamls]# kubectl create cm -h# --from-file參數既可以指定文件又可以指定目錄[root@master01 yamls]# kubectl create cm cm-from-file --from-file=conf/configmap/cm-from-file created查看
[root@master01 yamls]# kubectl get cmNAMEDATAAGEcm-from-file238skube-root-ca.crt117d[root@master01 yamls]# kubectl get cm cm-from-file -oyamlapiVersion: v1data:# 默認的data的key值就是文件名# 可以在創建時通過參數 --from-file=abc=nginx.conf的方式將data.key改成abcnginx.conf: |app.name=nginxlocation.prefix=/appredis.conf: |ip=10.10.10.101port=2379kind: ConfigMapmetadata:creationTimestamp: "2022-03-31T00:52:21Z"name: cm-from-filenamespace: defaultresourceVersion: "759788"uid: 5a41bdbe-66d0-45fa-8a5e-b6f6b005d6711.1.2、from-env-fileenv環境變量的格式為:key=value
指定參數:--from-env-file而不是--from-file
[root@master01 yamls]# kubectl create cm cm-env-file --from-env-file=conf/nginx.confconfigmap/cm-env-file created[root@master01 yamls]# kubectl getcm cm-env-file -oyamlapiVersion: v1data:app.name: nginxlocation.prefix: /appkind: ConfigMapmetadata:creationTimestamp: "2022-03-31T01:04:12Z"name: cm-env-filenamespace: defaultresourceVersion: "761496"uid: 8e885d31-0a83-423c-a59f-6ea8b04731031.1.3、from-literalliteral(翻譯為:字面意義的)直接在命令行上指定好key和value
參數:--from-literal
[root@master01 ~]# kubectl create cm env-from-literal --from-literal=Level=INFO --from-literal=Name=Macconfigmap/env-from-literal created[root@master01 ~]# kubectl get cm env-from-literal -oyamlapiVersion: v1data:Level: INFOName: Mackind: ConfigMapmetadata:creationTimestamp: "2022-04-01T00:31:13Z"name: env-from-literalnamespace: defaultresourceVersion: "762641"uid: da2f8922-8385-46ba-82e9-b0dc197a1eb2[root@master01 ~]#1.1.4、基于yaml文件創建apiVersion: v1kind: ConfigMapmetadata:name: game-demodata:# 類屬性鍵;每一個鍵都映射到一個簡單的值player_initial_lives: "3"ui_properties_file_name: "user-interface.properties"# 類文件鍵game.properties: |enemy.types=aliens,monstersplayer.maximum-lives=5user-interface.properties: |color.good=purplecolor.bad=yellowallow.textmode=true1.2、Pod使用ConfigMap1.2.1、valueFrom推薦將下面這種kv格式的configMap注入為容器的配置文件
[root@master01 yamls]# kubectl get cm cm-env-file -oyamlapiVersion: v1data:app.name: nginxlocation.prefix: /appkind: ConfigMapmetadata:creationTimestamp: "2022-03-31T01:04:12Z"name: cm-env-filenamespace: defaultresourceVersion: "761496"uid: 8e885d31-0a83-423c-a59f-6ea8b0473103創建dp,并將cm掛在進去
【系統整理K8S的配置管理實戰-建議收藏系列】[root@master01 yamls]#kubectl create deployment dp-test-cm --image=nginx --dry-run=client -oyaml > dp-test-cm.yaml修改
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: dp-test-cmname: dp-test-cmspec:replicas: 1selector:matchLabels:app: dp-test-cmtemplate:metadata:labels:app: dp-test-cmspec:containers:- image: nginxname: nginxenv:# 直接定義ENV的kv- name: Levelvalue: Pro# 將cm中的指定key的值作為AppName這個環境變量的值- name: AppNamevalueFrom:configMapKeyRef:name: cm-env-filekey: app.name

推薦閱讀