Containerized Home Assistant and wake_on_lan

Wanted to share how I got wake_on_lan to work properly when running home-assistant in a containerized environment, without giving the home-assistant container host networking privileges. (I’m running home-assistant on kubernetes+flannel, but the same should apply to folks using docker as well)

Context: wake_on_lan relies on broadcast traffic, but this is not usually possible within the overlay network used by most containerized setups.

My solution was to run a limited secondary container with with socat with host networking privileges. This accepts a unicast wake_on_lan from home-assistant and re-broadcasts to the network.

apiVersion: v1
kind: Namespace
metadata:
  labels:
    app: wakeonlan
  name: wakeonlan
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: wakeonlan
  name: wakeonlan
spec:
  selector:
    matchLabels:
      app: wakeonlan
  replicas: 1
  template:
    metadata:
      labels:
        app: wakeonlan
    spec:
      hostNetwork: true
      containers:
        - image: alpine/socat:latest
          name: socat
          args:
            - -dd
            - UDP-RECV:9999
            - UDP-SENDTO:255.255.255.255:9,broadcast
          ports:
            - name: wol
              containerPort: 9
              protocol: UDP
          resources: {}
---
apiVersion: v1
kind: Service
metadata:
  namespace: wakeonlan
  name: wakeonlan
spec:
  ports:
  - name: wol
    port: 9999
    protocol: UDP
    targetPort: 9999
  selector:
    app: wakeonlan
  type: LoadBalancer
  loadBalancerIP: 12.34.56.78 # my-wake-on-lan-doimain.home

And then inside home-assistant, whenever I need to wake_on_lan

service: wake_on_lan.send_magic_packet
data:
  mac: 78:5d:c8:12:8d:03
  broadcast_port: 9999
  broadcast_address: my-wake-on-lan-doimain.home