coredns-customが効かない環境があるのでhostAliasesに追加する

podで/etc/hostsを修正したい...

configmap/corednsに直接書けばいいのだが、突如デフォルトに戻る場合があるため、coredns-customが望ましいが、
kubernetesの環境によっては無視された。

configmap/coredns内の

        import custom/*.override
    }
    import custom/*.server

の行がそれっぽいが、追加してもだめだった。

  • deploymentのspec.HostAliasesに書けば、podの/etc/hostsに直接追加される
  • 記述したワークロード外には影響されないので、不要なNamespaceにまで名前解決の影響が出ない。こっちのほうが良いかもしれません。

https://kubernetes.io/ja/docs/tasks/network/customize-hosts-file-for-pods/

root@k8s-cont01:/home/ocarina/kubernetes# kubectl edit deployment/nginx
deployment.apps/nginx edited
root@k8s-cont01:/home/ocarina/kubernetes# kubectl get deployment/nginx -o yaml | grep \ spec\: -A 30 |grep restartPolicy -B 30
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      hostAliases:
      - hostnames:
        - node01
        ip: 172.31.1.1
      - hostnames:
        - node02
        - worker01
        ip: 172.31.1.2
      - hostnames:
        - node03
        - worker02
        ip: 172.31.1.3
      restartPolicy: Always
root@k8s-cont01:/home/ocarina/kubernetes# kubectl exec -it nginx-5545dbfdfb-h7z6v -- /bin/bash
ccroot@nginx-5545dbfdfb-h7z6v:/# 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
10.0.0.215  nginx-5545dbfdfb-h7z6v

# Entries added by HostAliases.
172.31.1.1  node01
172.31.1.2  node02  worker01
172.31.1.3  node03  worker02

Posted by ocarina