Kubernetes HostPort with dnsmasq issue

8/9/2021

I'm trying to setup a dnsmasq pod via kubernetes. Yaml file is like below:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: dnsmasq1
  labels:
    name: dnsmasq1
spec:
  serviceName: "dnsmasq1"
  replicas: 1
  selector:
    matchLabels:
      name: dnsmasq1
  volumeClaimTemplates:
  - metadata:
      name: dnsmasqconf-pv1
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: ceph-rbd-sc
  template:
    metadata:
      labels:
        name: dnsmasq1
    spec:
      hostNetwork: false
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                name: dnsmasq1
            topologyKey: "kubernetes.io/hostname"
      hostname: dnsmasq1
      containers:
      - name: dnsmasq1
        image: jpillora/dnsmasq
        ports:
        - containerPort: 8080
          hostPort: 8082
        imagePullPolicy: IfNotPresent
        env:
        - name: HTTP_USER
          value: "****"
        - name: HTTP_PASS
          value: "****"
        volumeMounts:
        - mountPath: /mnt/config
          name: dnsmasqconf-pv1
        resources:
          requests:
            memory: 250Mi
          limits:
            memory: 250Mi
      nodeSelector:
        etiket: worker
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 8.8.8.8
      volumes:
      - name: dnsmasqconf-pv1
        persistentVolumeClaim:
          claimName: dnsmasqconf-pv1

This works fine and I can reach to pod using the node's IP address. I decided to test the pod as a dns server on a test machine but the entries are not resolved. I think this is because I'm not using the Pod's IP as dns server but the node's. How can I set this pod to be used a dns server externally? I don't have a cloud provider so I don't think I can use loadbalancer IP here.

-- Nyquillus
dnsmasq
kubernetes

1 Answer

8/9/2021

I don't think you can use dnsmasq as external dns server as dnsmasq is a lightweight DNS forwarder, designed to provide DNSservices to a small-scale network. It can serve the names of local machines which are not in the global DNS. dnsmasq makes it simple to specify the nameserver to use for a given domain and it is ideal to manage communication in a kubernetes cluster.

In /etc/NetworkManager/NetworkManager.conf, add or uncomment the following line in the main section:

dns=dnsmasq

Create /etc/NetworkManager/dnsmasq.d/kube.conf with this line:

server=/cluster.local/10.90.0.10

This tells dnsmasq that queries for anything in the cluster.local domain should be forwarded to the DNS server at 10.90.0.10. This happens to be the default IP address of the kube-dns service in the kube-system namespace. If your cluster’s DNS service has a different IP address, you’ll need to specify it instead.

Now, after you run systemctl restart NetworkManager, your /etc/resolv.conf should look something like this:

#Generated by NetworkManager

search localdomain

nameserver 127.0.0.1

The important line is nameserver 127.0.0.1. This means DNS requests are sent to localhost, which is handled by dnsmasq.

-- Chandra Sekar
Source: StackOverflow