As I hinted at in the end of my last post, I’ve been looking for a way to take the hostnames setup for Kubernetes Ingress endpoints and turn them into mDNS CNAME entries.
When I’m building things I like to spin up a local copy where possible (e.g. microk8s on a Pi 4 for the Node-RED on Kubernetes and the Docker Compose environment on another Pi 4 for the previous version). These setups run on my local network at home and while I have my own DNS server set up and running I also make extensive use of mDNS to be able to access the different services.
I’ve previously built little utilities to generate mDNS CNAME entries for both Nginx and Traefik reverse proxies using Environment Variables or Labels in a Docker environment, so I was keen to see if I can build the same for Kubernetes’ Ingress proxy.
Watching for new Ingress endpoints
The kubernetes-client
node module supports for watching certain endpoints, so can be used to get notifications when an Ingress endpoint is created or destroyed.
const stream = client.apis.extensions.v1beta1.namespaces("default").ingresses.getStream({qs:{ watch: true}})
const jsonStream = new JSONStream()
stream.pipe(jsonStream)
jsonStream.on('data', async obj => {
if (obj.type == "ADDED") {
for (x in obj.object.spec.rules) {
let hostname = obj.object.spec.rules[x].host
...
}
} else if (obj.type == "DELETED") {
for (x in obj.object.spec.rules) {
let hostname = obj.object.spec.rules[x].host
...
}
}
}
Creating the CNAME
For the previous versions I used a python library called mdns-publish to set up the CNAME entries. It works by sending DBUS messages to the Avahi daemon which actually answers the mDNS requests on the network. For this version I decided to try and send those DBUS messages directly from the app watching for changes in K8s.
The dbus-next
node module allows working directly with the DBUS interfaces that Avahi exposes.
const dbus = require('dbus-next');
const bus = dbus.systemBus()
bus.getProxyObject('org.freedesktop.Avahi', '/')
.then( async obj => {
const server = obj.getInterface('org.freedesktop.Avahi.Server')
const entryGroupPath = await server.EntryGroupNew()
const entryGroup = await bus.getProxyObject('org.freedesktop.Avahi', entryGroupPath)
const entryGroupInt = entryGroup.getInterface('org.freedesktop.Avahi.EntryGroup')
var interface = -1
var protocol = -1
var flags = 0
var name = host
var clazz = 0x01
var type = 0x05
var ttl = 60
var rdata = encodeFQDN(hostname)
entryGroupInt.AddRecord(interface, protocol, flags, name, clazz, type, ttl, rdata)
entryGroupInt.Commit()
})
Adding a signal handler to clean up when the app gets killed and we are pretty much good to go.
process.on('SIGTERM', cleanup)
process.on('SIGINT', cleanup)
function cleanup() {
const keys = Object.keys(cnames)
for (k in keys) {
//console.log(keys[k])
cnames[keys[k]].Reset()
cnames[keys[k]].Free()
}
bus.disconnect()
process.exit(0)
}
Running
Once it’s all put together it runs as follows:
$ node index.js /home/ubuntu/.kube/config ubuntu.local
The first argument is the path to the kubectl
config fileand the second is the hostname the CNAME should point to.
If the Ingress controller is running on ubuntu.local
then Ingress YAML would look like this:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: manager-ingress
spec:
rules:
- host: "manager.ubuntu.local"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: manager
port:
number: 3000
I’ve tested this with my local microk8s install and it is working pretty well (even on my folks really sketchy wifi). The code is all up here.