본문 바로가기
Programming/Kubernetes

[Kubernets] coredns에 multiple consul dns 등록하기

by guru_k 2022. 2. 9.
728x90
반응형

consul dns를 coredns에 설정하는 법은 https://www.consul.io/docs/k8s/dns#coredns-configuration 를 참고하시면 됩니다.

그러면 만약 namespace로 분리된 환경에서 여러 namespace에서 consul dns를 설치하여 사용할 경우에 coredns에 어떻게 등록을 하는지 알아 봅시다.

아래처럼 coredns config를 편집하고 consul dns client의 주소를 필요한만큼 적용해줍니다. 

$ kubectl edit cm coredns -n kube-system

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health {
           lameduck 5s
        }
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }
        prometheus :9153
        forward . /etc/resolv.conf {
           max_concurrent 1000
        }
        cache 30
        loop
        reload
        loadbalance
    }
    consul {
      errors
      cache 30
      forward . 102.96.0.11 102.96.0.12 102.96.0.13    // multiple consul dns address
    }
kind: ConfigMap

namespace가 ns1, ns2, ns3 로 구성된 환경에서 각각 consul dns를 구축할 경우 위와 같이 각 namespace별 consul dns client 주소를 설정할 수 있습니다.

ns1 - 102.96.0.11

ns1 - 102.96.0.12

ns1 - 102.96.0.13

이후 각 namespace별 consul-client의 svc cluster ip를 각각 namespace에 할당된 ip에 맞게 설정한 이후 consul dns가 정상 동작하는 지 확인합니다.

apiVersion: apps/v1
kind: Service
apiVersion: v1
metadata:
  name: consul-client
spec:
  selector:
    app: consul-client
  ports:
  - name: consul-client-8500
    protocol: TCP
    port: 8500
    targetPort: 8500
  - name: consulclientdns-udp
    protocol: UDP
    port: 53
    targetPort: 8600
  - name: consulclientdns-tcp2
    protocol: TCP
    port: 8600
    targetPort: 8600
  - name: consulclientdns-udp2
    protocol: UDP
    port: 8600
    targetPort: 8600
  clusterIP: 102.96.0.11          // ns1

각각 namespace별 consul-client와 service를 등록한 이후 consul dns가 정상적으로 동작하는지 확인합니다.

apiVersion: v1
kind: Pod
metadata:
  name: dnsutils
  namespace: ns1     // 각각 설치된 namespace에 설치하여 dns가 정상동작하는 지 확인합니다.
spec:
  containers:
  - name: dnsutils
    image: gcr.io/kubernetes-e2e-test-images/dnsutils:1.3
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
$ kubectl apply -f dnsutils.yaml

$ kubectl exec -it dnsutils -- nslookup redis-service.service.consul
Server:		102.96.0.10
Address:	102.96.0.10#53

Name:	redis-service.service.consul
Address: 10.84.128.233
728x90
반응형

댓글