ConfigMap和Secret热更新

实际使用ConfigMap和Secret时,一般情况下都是直接使用文件创建,也就是使用--from-file=xxx的格式进行创建。创建的ConfigMap可能因为换行的问题,导致使用kubectl edit cm xxx时排版很乱,编辑起来很受影响

kubectl edit cm cm-dir
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  game.properties: "enemies=aliens\r\nlives=3\r\nenemies.cheat=true\r\nenemies.cheat.level=noGoodRotten\r\nsecret.code.passphrase=UUDDLRLRBABAS\r\nsecret.code.allowed=true\r\nsecret.code.lives=30"
  ui.properties: "color.good=purple\r\ncolor.bad=yellow\r\nallow.textmode=true\r\nhow.nice.to.look=fairlyNice"
kind: ConfigMap
metadata:
  creationTimestamp: "2023-11-30T11:26:18Z"
  name: cm-dir
  namespace: default
  resourceVersion: "23101"
  uid: 9c74cae6-db5a-4c3e-b143-b8d900c51e5a

使用kubectl edit secret xxx时,由于Secret数据为加密数据,无法直接编辑

kubectl edit secret db-user-pass
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  password.txt: MTIzNDU2
  username.txt: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2023-11-30T11:29:39Z"
  name: db-user-pass
  namespace: default
  resourceVersion: "23524"
  uid: 42377d3e-4fec-402a-9eb0-5b08b4ebc493
type: Opaque

因此一般情况下都是修改源文件,然后replace之前的ConfigMap或Secret,命令如下:

kubectl create cm cm-dir --from-file=k8s-ha-install --dry-run=client -o yaml | kubectl replace -f -
  • cm-dir:ConfigMap的名称

  • dry-run=client -o yaml:只运行命令,并不真正地创建,并以YAML的格式输出

  • kubectl replace -f -:通过文件创建的Secret和ConfigMap不能被直接替换,但是通过YAML文件创建可以被替换,所以先使用dry-run -oyaml生成YAML文件,再进行replace即可实现热更新,该方法可以用于其他资源类型,通过YAML文件替换已经创建的资源也是可以的

注意:

最后更新于

这有帮助吗?