k8s动态配置/etc/hosts

###使用命令行修改hosts,完事后保存退出,立即生效
kubectl  edit configmap  coredns  -n  kube-system

 

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# configmaps "coredns" was not valid:
# * <nil>: Invalid value: "The edited file failed validation": [yaml: line 16: found a tab character where an indentation space is expected, invalid character 'a' looking for beginning of value]
#
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
           lameduck 15s
        }
        ready

        kubernetes apicloud.com in-addr.arpa ip6.arpa {

          pods verified
          fallthrough in-addr.arpa ip6.arpa
        }
#下面hosts字段为新增 hosts { 47.95.x.120 pre.xxx.com 47.95.x.120 www.xxx.com fallthrough } prometheus :9153 forward . /etc/resolv.conf cache 30 loop reload loadbalance } kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"Corefile":".:53 {\n errors\n health {\n lameduck 15s\n }\n ready\n\n kubernetes xxxcloud.com in-addr.arpa ip6.arpa {\n\n pods verified\n fallthrough in-addr.arpa ip6.arpa\n }\n prometheus :9153\n forward . /etc/resolv.conf\n cache 30\n loop\n reload\n loadbalance\n}\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"coredns","namespace":"kube-system"}} creationTimestamp: "2021-08-07T10:20:37Z" name: coredns namespace: kube-system resourceVersion: "960" uid: 292160fc-e4b6-417f-876a-57d02932352a

  

 参考:https://help.aliyun.com/document_detail/188179.html?spm=5176.smartservice_service_chat.0.0.76e53f1bOJhMwX

解决的问题:

  1.内网体系测试使用;

  2.宿主机和本地hosts自定义解析;

  3.容器依然读取公共dns解析的情况下,配置上述信息可以与本地host解析同步

终极解决目标

   设置pod内DNS解析规则跟随node结点:  dnsPolicy:Default  

参考自:https://help.aliyun.com/document_detail/188179.html?spm=5176.smartservice_service_chat.0.0.76e5709aYtcZMI

集群dnsPolicy配置和场景说明

ACK支持通过dnsPolicy字段为每个Pod配置不同的DNS策略。目前ACK集群支持四种策略:
  • ClusterFirst:通过CoreDNS来做域名解析,Pod内/etc/resolv.conf配置的DNS服务地址是集群DNS服务的kube-dns地址。该策略是集群工作负载的默认策略。
  • None:忽略集群DNS策略,需要您提供dnsConfig字段来指定DNS配置信息。
  • Default:Pod直接继承集群节点的域名解析配置。即在ACK集群直接使用ECS的/etc/resolv.conf文件(文件内配置的是阿里云DNS服务)。
  • ClusterFirstWithHostNet:强制在hostNetWork网络模式下使用ClusterFirst策略(默认使用Default策略)。
posted @ 2021-08-17 15:09  南非波波  阅读(1391)  评论(0)    收藏  举报