Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

KubernetesとCoreDNSについて理解する

bells17
December 18, 2023

 KubernetesとCoreDNSについて理解する

bells17

December 18, 2023
Tweet

More Decks by bells17

Other Decks in Programming

Transcript

  1. ▶ @bells 1 7 ▶ Software Engineer@ 3 -shake inc.

    ▶ kubernetes & kubernetes-csi member ▶ Kubernetes Internal Organizer ▶ Kubernetes Novice Tokyo Organizer ← New! ▶ #kubenews ▶ X(Twitter): @bells 1 7 _ ▶ GitHub: @bells 1 7
  2. kind-con fi g.yaml kind: Cluster apiVersion: kind.x-k 8 s.io/v 1

    alpha 4 nodes: - role: control-plane - role: worker - role: worker # クラスター起動コマンド $ kind create cluster \ --name kube-dns-example \ --con fi g kind-con fi g.yaml control-plane: 1 worker: 2 合計3台のノードを起動する
  3. apiVersion: apps/v 1 kind: Deployment metadata: name: example spec: replicas:

    2 selector: matchLabels: app: example template: metadata: labels: app: example spec: a ffi nity: podAntiA ff i nity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 podA ff i nityTerm: labelSelector: matchExpressions: - key: app operator: In values: - nginx topologyKey: "kubernetes.io/hostname" containers: - name: nginx image: nginx ports: - containerPort: 8 0 2台をpreferredDuringSchedulingIgnoredDuringExecutionで分散配置してるのでworkerノードに1台ずつ配置されるはず Deployment
  4. apiVersion: v 1 kind: Service metadata: name: example spec: type:

    ClusterIP selector: app: example ports: - protocol: TCP port: 8 0 Service
  5. $ kubectl exec -it $(kubectl get pod -l app=example -o=jsonpath='{.items[

    0 ].metadata.name}') -- bash # curl example.default.svc.cluster.local -o /dev/null -w '%{http_code}\n' -s 2 0 0 # cat /etc/resolv.conf search default.svc.cluster.local svc.cluster.local cluster.local nameserver 1 0 . 9 6 . 0 . 1 0 options ndots: 5 動作確認
  6. $ kubectl exec -it $(kubectl get pod -l app=example -o=jsonpath='{.items[

    0 ].metadata.name}') -- bash # curl example.default.svc.cluster.local -o /dev/null -w '%{http_code}\n' -s # ←CoreDNSは どうやって名前解決してる? 2 0 0 # cat /etc/resolv.conf search default.svc.cluster.local svc.cluster.local cluster.local nameserver 1 0 . 9 6 .0.10 # ← このIPアドレスはどこで設定される? options ndots: 5 動作確認
  7. $ kubectl -n kube-system get cm kubelet-con fi g -o

    yaml apiVersion: v 1 kind: Con fi gMap metadata: name: kubelet-con fi g namespace: kube-system data: kubelet: | apiVersion: kubelet.con fi g.k 8 s.io/v 1 beta 1 ... cgroupDriver: systems cgroupRoot: /kubelet clusterDNS: - 1 0 . 9 6 . 0 . 1 0 clusterDomain: cluster.local … kubelet-con fi gを⾒るとclusterDNSが10.96.0.10に設定されてるのがわかる これがresolve.confの”nameserver 1 0 . 9 6 . 0 . 1 0 ”に設定されている
  8. $ kubectl -n kube-system get svc kube-dns -o yaml apiVersion:

    v 1 kind: Service metadata: name: kube-dns namespace: kube-system spec: clusterIP: 1 0 . 9 6 . 0 . 1 0 clusterIPs: - 1 0 . 9 6 . 0 . 1 0 internalTra ff i cPolicy: Cluster ipFamilies: - IPv 4 ipFamilyPolicy: SingleStack ports: - name: dns port: 5 3 protocol: UDP targetPort: 5 3 - name: dns-tcp port: 5 3 protocol: TCP targetPort: 5 3 - name: metrics port: 9 1 5 3 protocol: TCP targetPort: 9 1 5 3 selector: k 8 s-app: kube-dns sessionA ff i nity: None type: ClusterIP 最終的にこんな感じでServiceが設定されている
  9. $ kubectl -n kube-system get cm coredns -o yaml kind:

    Con fi gMap metadata: name: coredns namespace: kube-system apiVersion: v 1 data: Core fi le: | .: 5 3 { errors health { lameduck 5 s } ready kubernetes cluster.local in-addr.arpa ip 6 .arpa { pods insecure fallthrough in-addr.arpa ip 6 .arpa ttl 3 0 } prometheus : 9 1 5 3 forward . /etc/resolv.conf { max_concurrent 1 0 0 0 } cache 3 0 loop reload loadbalance } ちなみにCoreDNSの設定はこんな感じになっている “kubernetes cluster-local ~” の部分がKubernetesプラグインの引数になっている
  10. 参考資料 ▶ https://github.com/kubernetes/kubernetes/tree/v 1 . 2 8 . 4 ▶

    https://github.com/coredns/coredns/tree/v 1 . 1 1 . 1 ▶ https://github.com/coredns/example ▶ https://github.com/coredns/coredns/blob/v 1 . 1 1 . 1 /plugin/kubernetes/README.md ▶ https://github.com/kubernetes/dns/blob/ 1 . 2 2 . 2 8 /docs/speci fi cation.md ▶ https://github.com/kubernetes/cri-api/blob/v 0 . 2 8 . 4 /pkg/apis/runtime/v 1 /api.proto ▶ https://coredns.io/ 2 0 1 7 / 0 3 / 0 1 /how-to-add-plugins-to-coredns/ ▶ https://coredns.io/ 2 0 1 6 / 1 2 / 1 9 /writing-plugins-for-coredns/ ▶ https://github.com/coredns/example ▶ https://github.com/coredns/coredns/blob/v 1 . 1 1 . 1 /plugin.md