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

zy1mlcev  于 2023-02-03  发布在  Kubernetes
关注(0)|答案(5)|浏览(340)

我试图修补部署,但我不断点击部署。extensions/velero not patched.
我尝试了以下几种不同的变体:

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

我的初始部署.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: {}

我现在有点卡住了,担心我可能会走错路。任何建议都将不胜感激。

57hvy0tb

57hvy0tb1#

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

kubectl set env deployment/velero AWS_CLUSTER_NAME=test-cluster

希望这个有用。

o7jaxewo

o7jaxewo2#

您已经在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"}
7z5jn7bk

7z5jn7bk3#

当您正在修补部署时,命令中用于修补的JSON不准确。您可能需要尝试以下操作:
kubectl patch deployment velero -p '{"spec":{"template":{"spec":{"containers":[{"env":[{"name":"AWS_CLUSTER_NAME","value":"test-cluster"}]}]}}}}'

cwxwcias

cwxwcias4#

patch外,还有另一个选项可用于更改现有部署中的值。
也可以执行编辑命令,对YAML格式的值进行新增、修改,然后保存即可。

$ kubectl edit deployment velero -o yaml

或者,如果您不喜欢vi,也可以使用nano

$ KUBE_EDITOR="nano" kubectl edit deployment/velero
5vf7fwbs

5vf7fwbs5#

根据容器名称选择容器,并更新其中一个环境变量

kubectl patch deployment deployment-name -p '{"spec":{"template":{"spec":{"containers":[{"name":"container_name","env":[{"name":"KEY","value":"NEW_VAL"}]}]}}}}'

使用bash变量

export NEW_VAL=XXX
kubectl patch deployment deployment-name -p '{"spec":{"template":{"spec":{"containers":[{"name":"container_name","env":[{"name":"KEY","value":"'${NEW_VAL}'"}]}]}}}}'

相关问题