zoukankan      html  css  js  c++  java
  • add dns items for coredns

    coredns org

    hosts plguin and reverse, we can set reload durations.  

    add a dns item as follow:

    # $(kubectl get cm coredns -n kube-system  -o json | jq .data.Corefile |grep -o host)
    
    function add_coredns_host(){
    CM=${CM:-coredns}
     
    kubectl get cm $CM -n kube-system  -o json | jq .data.Corefile |grep -o hosts
    ret=$?
    
    if [[ $ret != 0 ]]; then
      # str=`kubectl get cm $CM -n kube-system -o json | jq .data.Corefile | 
      #      sed -e '0,/prometheus/s/prometheus/hosts {\
     '"$item"'\
     }\
     /'`
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |
               sed -e '0,/prometheus/s/prometheus/hosts {\n       '"$1"'\n    }\n    prometheus/')
    else
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |
               sed -e 's/(hosts {)/1\n       '"$1"'/')
    fi
    
    NATPATH="/data/Corefile"
    kubectl patch cm $CM -n kube-system --type='json' -p '[
    {"op":"replace","path":"'"$NATPATH"'","value": '"$str"'},
    ]'
    }
    
    
    item="172.16.30.4 gateway.sdwam.opennes"
    add_coredns_host  "$item"

    Add host item with fallthrough append 

    # $(kubectl get cm coredns -n kube-system  -o json | jq .data.Corefile |grep -o host)
    
    function add_coredns_host(){
      CM=${CM:-coredns}
      item=fallthrough
    
      kubectl get cm $CM -n kube-system  -o json | jq .data.Corefile |grep -o hosts
      ret=$?
      if [[ $ret != 0 ]]; then
        # str=`kubectl get cm $CM -n kube-system -o json | jq .data.Corefile | 
        #      sed -e '0,/prometheus/s/prometheus/hosts {\
     '"$item"'\
     }\
     /'`
        # str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |
        #          sed -e '0,/prometheus/s/prometheus/hosts {\n       '"$1"'\n    }\n    prometheus/')
        str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |
              sed -e '0,/prometheus/s/prometheus/hosts {\n       '"$item"'\n    }\n    prometheus/')
        echo "Create an empity hosts list"
        NATPATH="/data/Corefile"
        kubectl patch cm $CM -n kube-system --type='json' -p '[
          {"op":"replace","path":"'"$NATPATH"'","value": '"$str"'},
        ]'
      fi
    
    
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |
               sed -e 's/(hosts {)/1\n       '"$1"'/')
    
    
      NATPATH="/data/Corefile"
      kubectl patch cm $CM -n kube-system --type='json' -p '[
        {"op":"replace","path":"'"$NATPATH"'","value": '"$str"'},
      ]'
    }

    example:

    item="172.16.30.4 gateway.sdwam.opennes"
    # make sure the $item is in double quotes
    add_coredns_host  "$item"

    Get Coredns config

    function get_dns_corefile(){
      CM=${CM:-coredns}
      NS=${NS:-kube-system}
      kubectl get cm $CM -n $NS -o json | jq .data.Corefile | xargs printf
    }

    Get host dns item

    function list_dns_host(){
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |grep -o "[ ]*hosts[^}]*}")
      if  [[ -z "$str" ]] ;then 
        echo "no hosts list in coredns data file, exit."
        exit 1
      fi
      echo "The current hosts list:"
      printf "$str
    "
    }

    Remove host dns item

    function rm_dns_host(){
     
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile |grep -o "[ ]*hosts[^}]*}")
      if  [[ -z "$str" ]] ;then 
        echo "no hosts list in coredns data file, exit."
        exit 1
      fi
      echo "The current hosts list:"
      printf "$str
    "
      echo 
    
      echo "----------- New date file -----------"
      CM=${CM:-coredns}
      NS=${NS:-kube-system}
      str=$(kubectl get cm $CM -n kube-system -o json | jq .data.Corefile | sed -e 's/\n[^]*'"$1"'[^]*//g')
      printf "$str"
      
      NATPATH="/data/Corefile"
      kubectl patch cm $CM -n kube-system --type='json' -p '[
      {"op":"replace","path":"'"$NATPATH"'","value": '"$str"'},
      ]'
    }

    Restart pod

    可以不用重新启动,2分钟后会生效

    kubectl scale deployment coredns -n kube-system --replicas=0
    watch -n 1 "kubectl -n kube-system get pod |grep core"
    
    
    kubectl scale deployment coredns -n kube-system --replicas=2  
    watch -n 1 "kubectl -n kube-system get pod |grep core"

    or

    # https://coredns.io/2017/05/08/custom-dns-entries-for-kubernetes/
    
    kubectl exec -n kube-system coredns-980047985-g2748 -- kill -SIGUSR1 1

    validation

    # https://www.cyberciti.biz/faq/centos-7-dig-command-not-found-how-to-install-dig-on-centos/
    yum whatprovides '*bin/dig'
    
    yum install bind-utils -y
    
    # https://serverfault.com/questions/731238/contacting-dns-server-via-command-line-interface/731244
    dnssv=$(kubectl get svc -A |grep dns |awk '{print $4}')
    
    dns=demo4.xx.com dig
    +add +trace @$dnssv -p 53 $dns
    dig @$dnssv -p 53 $dns

    REF:

    CoreDNS Manual  

    CoreDNS for Kubernetes Service Discovery, Take 2 

    Linux: Using sed to insert lines before or after a match

  • 相关阅读:
    vue中使用clipboard.js复制分本
    聊聊IOCP,聊聊异步编程
    dubbo RPC超时异常小结
    redis4.0 集群,jedis客户端连接配置
    Centos7 Zookeeper
    阿里云Centos 7.4 mssql-server
    Redis4.0 Cluster — Centos7
    Elasticsearch学习笔记 一
    Centos7安装ES 和 Docker搭建ES
    使用Docker快速创建.Net Core2.0 Nginx负载均衡节点
  • 原文地址:https://www.cnblogs.com/shaohef/p/14877150.html
Copyright © 2011-2022 走看看