How to add custom nameserver under /etc/resolv.conf into pod?

11/3/2019

which service assigns nameservers under /etc/resolv.conf of pods , generally it should pickup from host /etc/resolv.conf , i'm seeing different nameservers under /etc/resolv.conf of pods, is there is any configuration on kbernetes(kubedns) which i can configure so that pods /etc/resolv.conf have 8.8.8.8

-- venkatesh pakanati
kubernetes
kubernetes-helm
kubernetes-pod

4 Answers

11/4/2019

Starting with k8s 1.9, if you want to set a specific dns config for a pod, you can use dns policy None.

It allows a Pod to ignore DNS settings from the Kubernetes environment. All DNS settings are supposed to be provided using the dnsConfig field in the Pod Spec.

by default, The nameserver IP is the Kubernetes service IP of kube-dns

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

kubectl get service -n kube-system
kube-dns               ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   2d21h

with this configuration in the deployment section:

  dnsConfig:
    nameservers:
      - 8.8.8.8
  dnsPolicy: "None"

cat /etc/resolv.conf nameserver 8.8.8.8

-- iliefa
Source: StackOverflow

11/21/2019

@venkatesh I think you are referring to podTemplate() used inside a jenkins file. If thats the case try this

podTemplate( yaml:"""
apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  containers:
  - name: ubuntu
    image: ubuntu:trusty
    command: ["echo"]
    args: ["Hello World"]
 dnsPolicy: "None"
 dnsConfig:
   nameservers:
   - 8.8.8.8
""")

a yaml file can be used directly inside a jenkins file as a template. More examples are present in https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/ .

-- shubham_asati
Source: StackOverflow

11/3/2019

kube-dns does modify this file (via Kubelet). kube-dns watches API server and observes changes to Service and Endpoints and keeps DNS records up to date. Within Cluster you should use internal Kubernetes DNS.

DNS is add-on controller, you can use any other implementation.

Take a look here.

If you want to override kube-dns

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-domain.example
      - my.dns.search.suffix
    options:
      - name: ndots
        value: "2"
      - name: edns0

DNS Policy

-- fg78nc
Source: StackOverflow

11/4/2019

You have two options:

1.- To put it in a configMap, and map it to /etc/resolv.conf, in which case it will be replaced by the content in the configMap.

2.- You can do this:

apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: my-pod
spec:
  containers:
    - name: nginx
      image: nginx
  dnsPolicy: "None"
  dnsConfig:
    nameservers:
      - 8.8.8.8

This way you are mapping whatever is in dnsConfig under /etc/resolv.conf

# cat /etc/resolv.conf
nameserver 8.8.8.8

There is another way actually, by "hacking" kube-dns, for upstreamNameservers:

apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-dns
  namespace: kube-system
data:
  upstreamNameservers: |
    ["8.8.8.8", "8.8.4.4"]

But in this case you wouldn't be doing anything within the pod (so /etc/resolv.conf would not be modified), but kube-dns would use these nameservers to resolve.

-- suren
Source: StackOverflow