Skip to content

Instantly share code, notes, and snippets.

@maksim-paskal
Created August 9, 2020 08:27
Show Gist options
  • Save maksim-paskal/8a7d1e9341e939ce52b4b546c4b5d9ec to your computer and use it in GitHub Desktop.
Save maksim-paskal/8a7d1e9341e939ce52b4b546c4b5d9ec to your computer and use it in GitHub Desktop.

Use dns cache in kubernetes

Install

https://kubernetes.io/docs/tasks/administer-cluster/nodelocaldns/

to retrieve hostIP from pod add env to daemonset - dig kubernetes-node - will return hostIP of current pod

env:
- name: POD_HOST_IP
  valueFrom:
    fieldRef:
      apiVersion: v1
      fieldPath: status.hostIP

Configuration. CoreDNS - Main server

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
      errors
      health

      rewrite continue {
        ttl regex .* 30
      }

      # ec2 fix
      rewrite stop name regex ^(ip-\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}\.ec2\.internal) {1}
      rewrite stop name regex .*\.ec2\.internal ec2.internal.invalid

      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
    }

Configuration. CoreDNS - kubernetes node cache

apiVersion: v1
kind: ConfigMap
metadata:
  name: node-local-dns
  namespace: kube-system
data:
  Corefile: |
    cluster.local:53 {
      errors
      cache 30
      reload
      loop
      bind 169.254.20.10
      forward . 10.100.0.10 {
        force_tcp
      }
      prometheus :9253
      health 169.254.20.10:8080
    }
    in-addr.arpa:53 {
      errors
      cache 30
      reload
      loop
      bind 169.254.20.10
      forward . 10.100.0.10 {
        force_tcp
      }
      prometheus :9253
    }
    ip6.arpa:53 {
      errors
      cache 30
      reload
      loop
      bind 169.254.20.10
      forward . 10.100.0.10 {
        force_tcp
      }
      prometheus :9253
    }
    ec2.internal:53 {
      errors
      cache 30
      reload
      loop
      bind 169.254.20.10
      forward . 10.100.0.10 {
        force_tcp
      }
      prometheus :9253
    }
    .:53 {
      errors
      cache 30
      reload
      loop
      bind 169.254.20.10

      template IN A kubernetes-node {
        answer "{{ .Name }} 30 IN A {$POD_HOST_IP}"
      }

      forward . 10.100.0.10 {
        force_tcp
      }
      prometheus :9253
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment