如何利用 kubectl 补丁部署来更新环境变量?

How to leverage kubectl patch deployment to update an environment variable?

我正在尝试修补部署,但我一直在打部署。extensions/velero 未修补。

我尝试了以下几种不同的变体:

kubectl patch deployment velero -n velero -p '{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}]}]}}'

我的初始 deployment.yaml 文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: velero
  labels:
    app.kubernetes.io/name: velero
    app.kubernetes.io/instance: velero
    app.kubernetes.io/managed-by: Tiller
    helm.sh/chart: velero-2.1.1
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/instance: velero
      app.kubernetes.io/name: velero
  template:
    metadata:
      labels:
        app.kubernetes.io/name: velero
        app.kubernetes.io/instance: velero
        app.kubernetes.io/managed-by: Tiller
        helm.sh/chart: velero-2.1.1
    spec:
      restartPolicy: Always
      serviceAccountName: velero-server
      containers:
        - name: velero
          image: "gcr.io/heptio-images/velero:v1.0.0"
          imagePullPolicy: IfNotPresent
          command:
            - /velero
          args:
            - server
          volumeMounts:
            - name: plugins
              mountPath: /plugins
            - name: cloud-credentials
              mountPath: /credentials
            - name: scratch
              mountPath: /scratch
          env:
            - name: AWS_SHARED_CREDENTIALS_FILE
              value: /credentials/cloud
            - name: VELERO_SCRATCH_DIR
              value: /scratch
      volumes:
        - name: cloud-credentials
          secret:
            secretName: cloud-credentials
        - name: plugins
          emptyDir: {}
        - name: scratch
          emptyDir: {}

我现在有点卡住了,担心我可能会以错误的方式解决这个问题。任何建议将不胜感激。

当您为部署打补丁时,命令中的 JSON 补丁不准确。您可能想尝试以下操作:

kubectl patch deployment velero -p '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}]}]}}}}'

您已经在 default 命名空间中部署了 velera 部署,并且您正在尝试在 velera 命名空间中进行修补。

此外,您的补丁字符串无效。尝试以下方法:

$ kubectl patch deployment velero -p '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}],"name":"velero"}]}}}}'
deployment.extensions/velero patched

注意:我的客户端和服务器版本是:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

除了kubectl patch command, you can also make use of kubectl set env更新k8s部署的环境变量。

kubectl set env deployment/velero AWS_CLUSTER_NAME=test-cluster

希望对您有所帮助。

patch 外,还有另一个选项可以更改现有部署中的值。

您还可以执行 YAML 格式的编辑命令和 add/edit 值。那你得保存一下。

$ kubectl edit deployment velero -o yaml

或者,如果您不喜欢 vi,您可以在 nano 中完成

$ KUBE_EDITOR="nano" kubectl edit deployment/velero