title: How to run DNS Server (dnsmasq) in Kubernetes?
body: <p>I'm trying to run DNS Server (Dnsmasq) in Kubernetes cluster. The cluster has only one node. Everything works fine until I need to restart dnsmasq container (<code>kubectl rollout restart daemonsets dnsmasq-daemonset</code>) to apply changes made to <code>hosts</code> ConfigMap. As I found out this is needed as Dnsmasq that is already running will not otherwise load changes made into <code>hosts</code> ConfigMap.</p>
  <p>Soon as the container is restarted it is not able to pull dnsmasq image and it fails. It is expected behavior as it cannot resolve the image name as there are no other dns servers running, but I wonder what would be best way around it or what are the best practices with running DNS Server in Kubernetes in general. Is this something that CoreDNS is used for or what other alternatives are there? Maybe some high availability solution?</p>
  <p>hosts ConfigMap:</p>
  <pre><code>--- 
  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: dnsmasq-hosts
    namespace: core
  data:
    hosts: |
      127.0.0.1       localhost
  
      10.x.x.x        example.com
      ...
  </code></pre>
  <p>Dnsmasq deployment:</p>
  <pre><code>---
  apiVersion: apps/v1
  kind: DaemonSet
  metadata:
    name: dnsmasq-daemonset
    namespace: core
  spec:
    selector:
      matchLabels:
        app: dnsmasq-app
    template:
      metadata:
        labels:
          app: dnsmasq-app
        namespace: core
      spec:
        containers:
          - name: dnsmasq
            image: registry.gitlab.com/path/to/dnsmasqImage:tag
            imagePullPolicy: IfNotPresent
            resources:
              limits:
                cpu: &quot;1&quot;
                memory: &quot;32Mi&quot;
              requests:
                cpu: &quot;150m&quot;
                memory: &quot;16Mi&quot;
            ports:
              - name: dns
                containerPort: 53
                hostPort: 53
                protocol: UDP
            volumeMounts:
              - name: conf-dnsmasq
                mountPath: /etc/dnsmasq.conf
                subPath: dnsmasq.conf
                readOnly: true
              - name: dnsconf-dnsmasq
                mountPath: /etc/dnsmasq.d/dns.conf
                subPath: dns.conf
                readOnly: true
              - name: hosts-dnsmasq
                mountPath: /etc/dnsmasq.d/hosts
                subPath: hosts
                readOnly: true
        volumes:
          - name: conf-dnsmasq
            configMap:
              name: dnsmasq-conf
          - name: dnsconf-dnsmasq
            configMap: 
              name: dnsmasq-dnsconf
          - name: hosts-dnsmasq
            configMap:
              name: dnsmasq-hosts
        imagePullSecrets:
          - name: gitlab-registry-credentials
        nodeSelector:
          kubernetes.io/hostname: master
        restartPolicy: Always
  </code></pre>
  <p>I tried to use <code>imagePullPolicy: Never</code>, but it seems to fail anyway.</p>