相关文章推荐
逆袭的核桃  ·  Code with GitHub ...·  3 月前    · 
苦恼的手套  ·  ESLint报错:Parsing ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Kubernetes ships with a ConfigMap called coredns that lets you specify DNS settings. I want to modify or patch a small piece of this configuration by adding:

apiVersion: v1
kind: ConfigMap
data:
  upstreamNameservers: |
    ["1.1.1.1", "1.0.0.1"]

I know I can use kubectrl edit to edit the coredns ConfigMap is there some way I can take the above file containing only the settings I want to insert or update and have it merged on top of or patched over the existing ConfigMap?

The reason for this is that I want my deployment to be repeatable using CI/CD. So, even if I ran my Helm chart on a brand new Kubernetes cluster, the settings above would be applied.

Hi @MuhammadRehanSaeed, Helm is for deploying and managing 'artifacts' or self contained systems. I don't think it's appropriate to use Helm to modify system configs. – Scott Boring Jul 17, 2019 at 20:05 This answer is correct; but, there are more ways to patch a resource. See: kubernetes.io/docs/tasks/run-application/… – Scott Boring Jul 17, 2019 at 20:07 This is invaluable for sooo many use cases. It's a pity this cannot be found on the docs. Thank you! – RndmSymbl May 3, 2022 at 7:45 @ Jordan Liggitt this way doesn't work if the whole config under data field is in form of embedded json string. – esahmo Jan 17 at 19:13 The question asks how to do this in a CI/CD environment where manually editing some files is not possible. – Muhammad Rehan Saeed Feb 7, 2019 at 12:22 This is a more general answer which is correct and you should be able to run this script in your CI/CD pipeline – Shaswat Lenka Apr 12 at 7:28 The question asks how to do this in a CI/CD environment where manually editing some files is not possible. – Muhammad Rehan Saeed Feb 7, 2019 at 12:22

As ConfigMaps are used to mount configuration files to Pod, it seems like this is what you are looking for. ConfigMaps inside of containers will update automatically if the underlying ConfigMap or Secret is modified.

You can specify configMap location:

configMapVolume(mountPath: '/etc/mount3', configMapName: 'my-config'),

Update:

Ok, I guess this does not solve your issue. Other thing that comes to my mind is kubectl create configmap with a pipe to kubectl replace So the whole command would look like this:

kubectl create configmap NAME --from-file file.name -o yaml --dry-run | kubectl replace -f -

Note that this replaces whole file, so just replace should work too.

Ain't this the same command as in the top answer where Jordan Liggitt additionally made the effort to fill in the attributes that would work for OP? – Jan Vítek Jun 1 at 6:11

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.