The great thing about kubernetes is that you can spin up and down internet facing services in a mostly hustle-free way. But one pain point that you might encounter, when you have a bunch of services going up and down semi-frequently, is that it becomes quite tedious to manage their DNS records manually.

External-DNS is a Kubernetes controller that manages DNS records for your Kubernetes services. It can be used to automatically create and update DNS records for your kubernetes ingresses and/or services, regardless of which DNS provider you use. It supports a wide range of DNS providers, including Amazon Route 53, Google Cloud DNS, and Cloudflare.

Prerequisites:

  • A Kubernetes cluster up and running.
  • Helm
  • DNS provider (cloudflare for this guide)

Step 1: Install external-dns helm chart

#Add bitnami helm repo
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update

#Install helm chart
helm upgrade --atomic --install external-dns bitnami/external-dns --namespace external-dns --create-namespace -f values.yaml
# values.yaml
sources:
  - ingress
provider: cloudflare
cloudflare:
  email: "johndoe@example.com"
  apiToken: "my_api_token"
  proxied: false
policy: sync #How DNS records are synchronized between sources and providers (options: sync, upsert-only )

In this example, we're setting up external-dns to only sync Ingress hosts with the DNS provider. But you can also setup services to be watched. You can find all the available helm chart values & options here.

In the case of Cloudflare, you use an API token to authenticate with their API for update DNS records. You can find guides about each specific DNS provider that is supported by external-dns in the github repository.

Step 2: Verify that external-dns is working

kubectl apply -f test.yaml
# test.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: nginx
  labels:
    app: nginx
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  namespace: nginx
  labels:
    app: nginx
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: test.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx
                port:
                  number: 80

After a little while, you should have access to your service at the host that you provided in the ingress.

Congratulations! You've now successfully installed external-dns. Now, your Kubernetes cluster will automatically update your DNS records when you deploy new services and ingresses. This is a huge time-saver, and it will make it much easier to manage your Kubernetes cluster.

If you have any questions or problems, please feel free to to leave a comment. We are always happy to help.

And remember, if you're looking for more fun and easy ways to use Kubernetes, be sure to check out our other blog posts. We've got everything from tutorials to tips and tricks.

Thanks for reading!

Share this post