Dans Dev Diary 2.0 Help

Automating Dynamic IP Update to Cloudflare Using Kubernetes

Problem Statement

For a long time, my home IP was relitivly static. However a few months ago, my ISP moved onto a different setup which in-turn rotated my IP address and ever since, seems to be rotating weekly.

Since I am using residential internet and my ISP does not offer a static IP service, I was having to manually update my DNS records in Cloudflare every time my IP changed. This was a painful process, thus I did what I do best, automate it.

This document will walk you through how I automated the process of updating my Cloudflare DNS records using Kubernetes.

Project Setup

  • Kubernetes Cluster

  • Cloudflare Account

  • Cloudflare API Token

Solution

The solution is to create a Kubernetes CronJob that will run every 15 minutes to update the DNS record. Using python and click to create a CLI tool that will update the DNS record (source code can be found here. Coupled with a Kubernetes CronJob, this will automate the process of updating the DNS record.

apiVersion: batch/v1 kind: CronJob metadata: name: cloudflare-ip-updater namespace: default spec: schedule: "*/15 * * * *" jobTemplate: spec: template: spec: containers: - name: cloudflare-ip-updater image: ghcr.io/dfoulkes/k3s_cloudflare_dynamic_ip_updater:latest command: - cfcli - update env: - name: CURRENT_DOMAIN valueFrom: secretKeyRef: name: website-domain-secret key: website-domain - name: CF_TOKEN valueFrom: secretKeyRef: name: cf-secret key: cf-token restartPolicy: OnFailure

This will run the cfcli update command every 15 minutes to update the DNS record.

Last modified: 13 May 2024