【Kubernetes】DNSについて知る

スポンサーリンク

はじめに

Kubernetesでの名前解決について、PodやServiceの名前解決は誰がしているのか、実際に動かしながら確認してみたいと思います。

Kubernetes DNS

KubernetesのSeriveやPodの名前解決は、クラスタ内にあるDNSサーバが名前解決を行います。このDNSサーバはServiceとして動いています。

❯ kubectl get service -n=kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   2m31s

このServiceは、下記のDeploymentに紐づいています。

❯ kubectl get deployment -n=kube-system
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
coredns   2/2     2            2           2m21s

さらに、このDeploymentは、下記のPodから構成されています。

❯ kubectl get pods -n=kube-system
NAME                                     READY   STATUS    RESTARTS   AGE
coredns-558bd4d5db-hg75t                 1/1     Running   0          4m51s
coredns-558bd4d5db-ltbxt                 1/1     Running   0          4m51s

Podは、corednsのイメージを利用しています。

❯ kubectl describe -n=kube-system pod coredns-558bd4d5db-hg75t | grep -i image
    Image:         k8s.gcr.io/coredns/coredns:v1.8.0
    Image ID:      docker-pullable://k8s.gcr.io/coredns/coredns@sha256:cc8fb77bc2a0541949d1d9320a641b82fd392b0d3d8145469ca4709ae769980e
  Normal   Pulled     6m15s  kubelet            Container image "k8s.gcr.io/coredns/coredns:v1.8.0" already present on machine
GitHub - coredns/coredns: CoreDNS is a DNS server that chains plugins
CoreDNSisaDNSserverthatchainsplugins.Contributetocoredns/corednsdevelopmentbycreatinganaccountonGitHub.

このDNS ServiceのIPアドレス(ここでは10.96.0.10)が、クラスタ内のコンテナの/etc/resolv.confに設定されています。

ServiceとPodのFQDN

ServiceとPodのFQDNは下記のようになります。

ServiceのFQDN

<service name>.<namespace name>.svc.<cluster domain>

具体例

nginx-service.default.svc.cluster.local

PodのFQDN

<ip address>.<namespace name>.pod.<cluster domain>

具体例

10-1-4-118.default.pod.cluster.local

PodとServiceの名前解決してみる

実際にPodとServiceの名前解決をしてみます。

PodとServiceを用意する

まずは、下記3つを用意します。

  • 名前解決するPod: dnsutils-pod.yml
  • 名前解決されるPod: nginx-pod.yml
  • 名前解決されるService: nginx-service.yml
apiVersion: v1
kind: Pod
metadata:
  name: dnsutils
  labels:
    name: dnsutils
spec:
  containers:
  - name: dnsutils
    image: tutum/dnsutils
    command:
      - sleep
      - "9999"
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
kubectl apply -f dnsutils-pod.yml
kubectl apply -f nginx-pod.yml
kubectl apply -f nginx-service.yml

PodのIPアドレスなどは下記になります。

❯ kubectl get pod -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE             NOMINATED NODE   READINESS GATES
dnsutils    1/1     Running   0          37m   10.1.4.119   docker-desktop   <none>           <none>
nginx-pod   1/1     Running   0          37m   10.1.4.118   docker-desktop   <none>           <none>

/etc/resolv.confの確認

dnsutils-podに入って、/etc/resolv.confを確認してみます。

kubectl exec -it dnsutils -c dnsutils -- /bin/bash

シェルの取得方法は下記で解説しています。

【Kubernetes】Podのコンテナでコマンド実行とシェル取得
はじめにKubernetesのPodのコンテナでコマンドを実行する方法とコンテナのシェルを取得する方法を紹介します。コンテナでコマンド実行kubectlexecコマンドを使ってコンテナ内でコマンドを実行します。--でコマンド部分と分離してい...

/etc/resolv.confを確認してみると、最初に確認したDNSのIPアドレス10.96.0.10が設定されているのがわかります。また、searchに「default.svc.cluster.local svc.cluster.local cluster.local」が設定されています。

root@dnsutils:/# cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

Serviceの名前解決

nginx-serviceの名前解決をしてみます。

root@dnsutils:/# nslookup nginx-service
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx-service.default.svc.cluster.local
Address: 10.101.127.160

root@dnsutils:/# nslookup nginx-service.default
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx-service.default.svc.cluster.local
Address: 10.101.127.160

root@dnsutils:/# nslookup nginx-service.default.svc
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx-service.default.svc.cluster.local
Address: 10.101.127.160

root@dnsutils:/# nslookup nginx-service.default.svc.cluster.local
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   nginx-service.default.svc.cluster.local
Address: 10.101.127.160

searchに「default.svc.cluster.local svc.cluster.local cluster.local」が設定されていたので、「nginx-service」や「nginx-service.default」などでも名前解決ができているのがわかります。

Podの名前解決

次にnginx-podの名前解決をしてみます。

root@dnsutils:/# nslookup 10-1-4-118.default.pod
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   10-1-4-118.default.pod.cluster.local
Address: 10.1.4.118

root@dnsutils:/# nslookup 10-1-4-118.default.pod.cluster.local
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   10-1-4-118.default.pod.cluster.local
Address: 10.1.4.118

searchに「cluster.local」が設定されていたので、「10-1-4-118.default.pod」でも名前解決できています。

まとめ

  • クラスタ内のDNS Serviceが名前解決している
  • Podの/etc/resolv.confにDNSのIPアドレスが設定されている
  • /etc/resolv.confのsearchで同じNamespaceとクラスタのドメインが設定されている

参考

タイトルとURLをコピーしました