简体   繁体   English

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

[英]How to leverage kubectl patch deployment to update an environment variable?

I'm trying to patch a deployment, but I keep hitting deployment.extensions/velero not patched.我正在尝试修补部署,但我一直在点击 deployment.extensions/velero not patched。

I've tried a few different variations of the following:我尝试了以下几种不同的变体:

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

My initial deployment.yaml file我的初始 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: {}

I'm a bit stuck right now and fear I may be going about this the wrong way.我现在有点卡住了,担心我可能会以错误的方式解决这个问题。 Any suggestions would be much appreciated.任何建议将不胜感激。

Apart from kubectl patch command, you can also make use of kubectl set env to update environment variable of k8s deployment. 除了kubectl patch命令之外,您还可以使用kubectl set env更新k8s部署的环境变量。

kubectl set env deployment/velero AWS_CLUSTER_NAME=test-cluster

Hope this helps. 希望这可以帮助。

As you are patching the deployment, the JSON for patch in your command is not accurate. 在修补部署时,命令中用于修补的JSON不正确。 You may want to try the following: 您可能需要尝试以下方法:

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

You have deployed your velera deployment in default namespace and you are trying to patch in velera namespace. 您已在default名称空间中部署了velera部署,并尝试在velera名称空间中进行修补。

Moreover, your patch string is not valid. 此外,您的补丁程序字符串无效。 Try the following one: 请尝试以下方法:

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

Note: My client and server versions are: 注意:我的客户端和服务器版本是:

$ 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"}

There is also another option to change value in existing deployment except patch . 除了patch之外,还有另一个选项可以更改现有部署中的值。

You can also execute edit command and add/edit value in YAML format. 您还可以执行edit命令并以YAML格式添加/编辑值。 Then you have to save it. 然后,您必须保存它。

$ kubectl edit deployment velero -o yaml

Or if you don't like vi you can do it in nano 或者,如果您不喜欢vi也可以在nano中完成

$ KUBE_EDITOR="nano" kubectl edit deployment/velero

Select your container based on its name and update its one of the environment varaible Select 您的容器基于其名称并更新其环境变量之一

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

With bash variable带有 bash 变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM