Exercise 9.3: Working with CoreDNS

  1. CP 노드에 연결된 터미널로 이동

  2. Pod 생성

    cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: net-tools
    spec:
      containers:
      - name: net-tools
        image: praqma/network-multitool
        command: [ "sleep" ]
        args: [ "infinity" ]
    EOF
  3. 생성한 Pod에 Bash 실행

    kubectl exec -it net-tools -- /bin/bash
  4. DNS 서버 정보 확인

    dig 
  5. 로컬 DNS 설정 확인

    cat /etc/resolv.conf
  6. DNS 서버 상세 정보 확인

    dig @DNS_SERVER -x DNS_SERVER

    OR

    dig @10.96.0.10 -x 10.96.0.10
  7. CURL 명령어로 이전 실습에서 생성한 Service의 FQDN로 접근

    curl nginx-one.accounting.svc.cluster.local
  8. CURL 명령어로 이전 실습에서 생성한 Service의 이름로 접근

    curl nginx-one
  9. CURL 명령어로 이전 실습에서 생성한 Service의 이름에 Namespace 이름을 추가하고 접근

    curl nginx-one.accounting
  10. Bash 종료

    exit
  11. kube-system Namespace 안에 있는 Service 확인

    kubectl -n kube-system get svc
  12. kube-dns Service의 Selector 조건 확인

    kubectl -n kube-system get svc kube-dns \
    -o=jsonpath='{.spec.selector}' | jq
  13. k8s-app Label을 가진 Pod 확인

    kubectl get pod -l k8s-app --all-namespaces
  14. k8s-app=kube-dns Label을 가진 Pod 확인

    kubectl get pod -l k8s-app=kube-dns --all-namespaces
  15. coredns Pod 상세 내용 확인

    kubectl -n kube-system get pod COREDNS_POD_NAME -o yaml

    OR

    kubectl -n kube-system get pod $(kubectl get pod -l k8s-app=kube-dns -o=jsonpath='{.items[0].metadata.name}' -n kube-system) -o yaml
  16. kube-system Namespace 안에 있는 ConfigMap 확인

    kubectl -n kube-system get configmaps
  17. coredns ConfigMap 상세 내용 확인

    kubectl -n kube-system get configmaps coredns -o yaml
  18. coredns ConfigMap을 수정하도록 텍스트 에디터 실행

    kubectl -n kube-system edit configmaps coredns
  19. 아래와 같이 수정

    apiVersion: v1
    data:
      Corefile: |
        .:53 {
            rewrite name regex (.*)\.douzone\.com {1}.default.svc.cluster.local
            errors
            health {
  20. 18, 19번 단계를 수행하지 않고 아래의 명령어로 대체 가능

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: coredns
      namespace: kube-system
    data:
      Corefile: |
        .:53 {
            rewrite name regex (.*)\.douzone\.com {1}.default.svc.cluster.local
            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
        }
    EOF
  21. coredns Pod 재생성

    {
        kubectl scale deployment coredns --replicas=0 -n kube-system
        kubectl scale deployment coredns --replicas=2 -n kube-system
    }
  22. coredns Pod가 재생성 되었는지 확인

    kubectl -n kube-system get pod -l k8s-app=kube-dns
  23. NGINX Deployment 생성

    kubectl create deployment nginx --image=nginx
  24. NGINX Deployment에 Service 생성

    kubectl expose deployment nginx --type=ClusterIP --port=80
  25. 생성된 Service 확인

    kubectl get svc nginx
  26. net-tools Pod로 Bash 연결

    kubectl exec -it net-tools -- /bin/bash
  27. 위에서 생성된 Service의 IP 주소로 DNS 주소 검색 (Reverse Lookup)

    dig -x SERVICE_CLUSTER_IP
  28. 위에서 확인한 Service의 DNS 주소로 IP 주소 검색 (Forward Lookup)

    dig nginx.default.svc.cluster.local
  29. CoreDNS에 추가한 Rewrite 규칙 테스트

    dig nginx.joins.com
  30. Rewrite 규칙에 명시한 도메인 주소로 접근 시도

    curl nginx.joins.com
  31. Bash 프로세스 종료

    exit
  32. coredns ConfigMap을 수정하도록 텍스트 에디터 실행

    kubectl -n kube-system edit configmaps coredns
  33. 아래와 같이 수정

    apiVersion: v1
    data:
      Corefile: |
        .:53 {
            rewrite stop {
                name regex (.*)\.douzone\.com {1}.default.svc.cluster.local
                answer name (.*)\.default\.svc\.cluster\.local {1}.douzone.com
            }
            errors
            health {
  34. 32, 33번 단계를 수행하지 않고 아래의 명령어로 대체 가능

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: coredns
      namespace: kube-system
    data:
      Corefile: |
        .:53 {
            rewrite stop {
                name regex (.*)\.douzone\.com {1}.default.svc.cluster.local
                answer name (.*)\.default\.svc\.cluster\.local {1}.douzone.com
            }
            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
        }
    EOF
  35. coredns Pod 재생성

    {
        kubectl scale deployment coredns --replicas=0 -n kube-system
        kubectl scale deployment coredns --replicas=2 -n kube-system 
    }
  36. coredns Pod가 재생성 되었는지 확인

    kubectl -n kube-system get pod -l k8s-app=kube-dns
  37. net-tools Pod로 Bash 연결

    kubectl exec -it net-tools -- /bin/bash
  38. CoreDNS에 추가한 Rewrite 규칙 확인

    dig nginx.joins.com
  39. CoreDNS에 추가한 Rewrite 규칙 테스트

    curl nginx.joins.com
  40. Bash 프로세스 종료

    exit
  41. net-tools Pod 삭제

    kubectl delete pod net-tools

Last updated