7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pod の DNS 設定について

Posted at

公式ドキュメントを見つつ確認した時のメモ。

クラスター外の DNS を参照したい場合

DNS for Services and Pods

Pod 毎にポリシーを設定する事ができる。
デフォルトポリシーは ClusterFirst でクラスター内の DNS サーバーに問い合わせて、その後アップストリームの DNS サーバーに問い合わせを行う。
上記ポリシーで None を指定すると dnsConfig フィールドで設定した情報となる。

まずは特に何も設定せず、デフォルトポリシーの場合の設定を見る。

# /etc/resolv.conf を確認
$kubectl exec 1-sample-pod cat /etc/resolv.conf
nameserver 10.100.0.10
search default.svc.cluster.local svc.cluster.local cluster.local ap-northeast-1.compute.internal us-west-2.compute.internal
options ndots:5
custom-dns.yaml
apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: dns-example
spec:
  containers:
    - name: test
      image: nginx
  dnsPolicy: "None"
  dnsConfig:
    nameservers:
      - 1.2.3.4
    searches:
      - ns1.svc.cluster.local
      - my.dns.search.suffix
    options:
      - name: ndots
        value: "2"
      - name: edns0

# ドキュメントの Pod を作成
$kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/service/networking/custom-dns.yaml
pod "dns-example" created

# /etc/resolv.conf が dnsConfig に設定した内容となっている
$kubectl exec -it dns-example -- cat /etc/resolv.conf
nameserver 1.2.3.4
search ns1.svc.cluster.local my.dns.search.suffix
options ndots:2 edns0

静的な名前解決を行いたい場合

Adding entries to Pod /etc/hosts with HostAliases

/etc/hostsに設定を行うことで静的な名前解決が出来る。
これを Pod でも設定できる。

最初に特に設定しない場合の状態を見る。

# nginx の pod 作成
$kubectl run nginx --image nginx --generator=run-pod/v1
pod "nginx" created

# nginx pod の ip を確認
$kubectl get pods --output=wide |grep nginx
nginx                      1/1       Running   0          36s       172.31.25.247   ip-172-31-19-51.ap-northeast-1.compute.internal

# /etc/hosts を確認。自分コンテナ情報も書き込まれている事がわかる
$kubectl exec nginx -- cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
172.31.25.247   nginx

次に hostAliases フィールドが設定された Pod を起動して確認する。

hostaliases-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - "/etc/hosts"
# pod 作成
$kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/service/networking/hostaliases-pod.yaml
pod "hostaliases-pod" created

# pod の ip を特定
$kubectl get pod -o=wide |grep hostalias
hostaliases-pod            0/1       Completed   0          30s       172.31.9.21     ip-172-31-0-56.ap-northeast-1.compute.internal

# pod で cat /etc/hosts しているので logs で確認
$kubectl logs hostaliases-pod
# Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
172.31.9.21     hostaliases-pod

# Entries added by HostAliases.
127.0.0.1       foo.local
127.0.0.1       bar.local
10.1.2.3        foo.remote
10.1.2.3        bar.remote

上記のように下の方にマニュフェストファイルで書いた情報が追記されている事を確認。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?