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

时间:2019-08-22 03:00:48

标签: kubernetes yaml kubectl

我正在尝试对部署进行修补,但是我一直在打Deployment.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: {}

我现在有点困惑,担心我可能会以错误的方式进行操作。任何建议将不胜感激。

4 个答案:

答案 0 :(得分:1)

除了kubectl patch命令之外,您还可以使用kubectl set env更新k8s部署的环境变量。

kubectl set env deployment/velero AWS_CLUSTER_NAME=test-cluster

希望这会有所帮助。

答案 1 :(得分:0)

在修补部署时,命令中用于修补的JSON不正确。您可能需要尝试以下方法:

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

答案 2 :(得分:0)

您已经将velera部署部署在default命名空间中,并且尝试在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"}

答案 3 :(得分:0)

除了patch之外,还有另一种方法可以更改现有部署中的价值。

您还可以执行编辑命令并以YAML格式添加/编辑值。然后,您必须保存它。

$ kubectl edit deployment velero -o yaml

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

$ KUBE_EDITOR="nano" kubectl edit deployment/velero