简体   繁体   English

如何使用 kubectl 给 statefulset envFrom 打补丁

[英]How to use to kubectl to patch statefulset envFrom

I have a Kube.netes Statefulset and im using envFrom to add environment variables from ConfigMaps and Secrets, by defining configMapRefs and secretRefs in an 'extra-values.yaml' file and including that file in my helm install command.我有一个 Kube.netes Statefulset ,我使用envFrom从 ConfigMaps 和 Secrets 添加环境变量,方法是在“extra-values.yaml”文件中定义 configMapRefs 和 secretRefs,并将该文件包含在我的helm install命令中。

The Statefulset.yaml snippet: Statefulset.yaml片段:

apiVersion: apps/v1
kind: StatefulSet
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    spec:
      containers:
        - name: {{ .Chart.Name | lower}}
          envFrom:
          {{- if .Values.envFrom }}
          {{- toYaml .Values.envFrom | nindent 10}}
          {{- end }}

The values.yaml file has a single envFrom: line with no children, and the extra-values.yaml file contains the configMapRefs and secretRefs: values.yaml 文件只有一个envFrom:行,没有子项, extra-values.yaml文件包含 configMapRefs 和 secretRefs:

envFrom:
- configMapRef:
    name: my-configmap-name
- configMapRef:
    name: another-configmap-name
- secretRef:
    name: my-secret-name
- secretRef:
    name: second-secret-name

The Helm install command:头盔安装命令:

helm install myapp /some-folder/myapps-chart-folder -f extra-values.yaml

What I want to do is install myapp without the extra-values.yaml file, and then use the kubectl patch command to add the configMapRefs and secretRefs to the statefulset and its pods.我想要做的是在没有extra-values.yaml文件的情况下安装myapp ,然后使用kubectl patch命令将 configMapRefs 和 secretRefs 添加到 statefulset 及其 pod。

I can manually do a kubectl edit statefulset to make these changes, which will terminate and restart the pod(s) with the correct environment variables.我可以手动执行kubectl edit statefulset来进行这些更改,这将终止并使用正确的环境变量重新启动 pod。

But I cannot for the life of me figure out the correct syntax and parameters for the kubectl patch command, despite hours of research, trial, and error, and repeated headbanging.但是我终其一生都无法找出kubectl patch命令的正确语法和参数,尽管进行了数小时的研究、试验和错误,并反复进行了猛烈抨击。 Help!帮助!

Thanks to mdaniel for the answer, which contains the clue to what I was missing.感谢mdaniel的回答,其中包含我所遗漏的线索。 Basically, I completely overlooked the fact that the containers element is an array (because my statefulset only specified one container, duh).基本上,我完全忽略了containers元素是一个array这一事实(因为我的statefulset只指定了一个容器,duh)。 In all of the kubectl patch command variations that I tried, I did not treat containers as an array, and never specified the container name, so kubectl patch never really had the correct information to act on.在我尝试过的所有kubectl patch命令变体中,我都没有将containers视为一个数组,也从未指定容器名称,因此kubectl patch从未真正获得正确的信息来执行操作。

So as suggested, the command that worked was something like this:因此,正如所建议的那样,有效的命令是这样的:

kubectl patch statefulset my-statefulset -p '{"spec": {"template": {"spec": {"containers": [{"name":"the-container-name", "envFrom": [{"configMapRef":{"name":"my-configmap-name"}}, {"configMapRef":{"name":"another-configmap-name"}}] }] }}}}'

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

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