Search code examples
kubernetescoredns

Resolving kubernetes service from pod running in hostNetwork mode


In k8n, I have a service svcFoo and two pods pod1 and pod2.

pod2 is running in hostNetwork mode.

If I exec into pod1 and run nslookup svcFoo, then domain get's successfully resolved.

But, if I exec into pod2 and run nslookup svcFoo, then it fails to resolve the domain.

Is there a way to resolve svcFoo or get ip address of svcFoo from pod2 which is running in hostNetwork mode?


Solution

  • To resolve the service svcFoo from a pod running in hostNetwork mode, you need to adjust the DNS policy for that pod. So you may have to use dnsPolicy as ClusterFirstWithHostNet.

    As per this official kubernetes document :

    "ClusterFirstWithHostNet": For Pods running with hostNetwork, you should explicitly set its DNS policy to "ClusterFirstWithHostNet". Otherwise, Pods running with hostNetwork and "ClusterFirst" will fallback to the behavior of the "Default" policy.

    Note: "Default" is not the default DNS policy. If dnsPolicy is not explicitly specified, then "ClusterFirst" is used.

    The example below shows a Pod with its DNS policy set to "ClusterFirstWithHostNet" because it has hostNetwork set to true.

    apiVersion: v1
    kind: Pod
    metadata:
     name: busybox
     namespace: default
    spec:
     containers:
     - image: busybox:1.28
       command:
         - sleep
         - "3600"
       imagePullPolicy: IfNotPresent
       name: busybox
     restartPolicy: Always
     hostNetwork: true
     dnsPolicy: ClusterFirstWithHostNet
    

    Refer to this Medium blog by Chimbu Chinnadurai for more information and also check this github issue which might be helpful for you.