Use kubectl patch to add DNS Rewrite Rule to CoreDNS Configmap

﹥>﹥吖頭↗ 提交于 2021-01-29 05:07:10

问题


I want to use the kubectl patch command to add a DNS rewrite rule to the coredns configmap, as described at Custom DNS Entries For Kubernetes. The default config map looks like this:

apiVersion: v1
data:
  Corefile: |
    .:53 {
        log
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           upstream
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }
        prometheus :9153
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
kind: ConfigMap
....

and I want to add the line

rewrite name old.name new.name

but how to specify adding a line within the ".:53" element is confounding me.

I know that I can get a similar result using kubectl get ... | sed ... | kubectl replace -f - but that would look kind of ugly, plus I want to expand my knowledge of kubctl patch using JSON. Thanks!


回答1:


You cannot modify ConfigMap using patch in your case.

data.Corefile is a key and its value (Corefile content) is of type: string.

It is treated by api-server as a string of bytes. You cannot patch part of a string with kubectl patch.

And second of all:

I want to expand my knowledge of kubctl patch using JSON

Corefile is not even a valid json file. Even if it was, api-server doesn't see a json/yaml, for api-server its just a string of random alphanumeric characters.


So what can you do?

You are left with kubectl get ... | sed ... | kubectl replace -f - , and this is a totally valid solution.



来源:https://stackoverflow.com/questions/63895949/use-kubectl-patch-to-add-dns-rewrite-rule-to-coredns-configmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!