Problem statement:
Within Kubernetes you cannot assign a static IP address to a pod (due to the dynamic nature of Kubernetes). The IP of the pod can change everytime the pod is restarted. The Homematic bridge on the other hand needs an IP address that it binds to and at least in my system it always automatically bound to the loopback address 127.0.0.1. This led to a situation where no callback from the Homematic gateway (in my case homegear) was possible and the bridge always lost the connection after a short time. To prevent this you need to specify in the configuration of the bridge a bind address
and a callback address
.
As you can see in the following (very simplified) picture, the bind address needs to be the IP address which is visible and available to the Homematic bridge inside the pod. The callback address needs to be the IP address that is visible from outside, which is the address of the Kubernetes node (if your Kubernetes cluster has only one node like mine, I am running MicroK8s). If you want to learn more about Kubernetes networking please see https://medium.com/swlh/kubernetes-services-simply-visually-explained-2d84e58d70e5
Solution:
Of course we want to build an automated solution that configures the Homematic bridge to use the IP address of the pod as the bind address. And here is how it works:
- Install the
systeminfo
binding in OpenHAB and create a thing of typecomputer
. - Create a new item named
OpenHABHost_Network_IPAddress
that links to the channelnetwork#ip
(IP address) of thecomputer
- If you still have a Homematic bridge defined delete this thing in OpenHAB.
- Create a new rule that will be triggered when the IP address of the pod changes or the system (pod) is started. Make sure to replace 192.168.178.66 with the static IP of your Homematic gateway and to replace 192.168.178.77 with the static IP of your Kubernetes node.
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
rule "IP address changed"
when
Item OpenHABHost_Network_IPAddress received update or
System started
then
val Path path = Paths.get("/openhab/conf/things/homematic.things")
var String content = "//*******************************\n//DO NOT CHANGE THIS FILE - IT IS CREATED AUTOMATICALLY BY A RULE\n//*******************************\nBridge homematic:bridge:HMCFGUSB2 [ gatewayAddress=\"192.168.178.66\", callbackHost=\"192.168.178.77\", bindAddress=\"" + OpenHABHost_Network_IPAddress.state + "\" ]"
Files.write(path, content.getBytes())
content = new String(Files.readAllBytes(path))
logInfo("Network", "IP-Address has changed, homematic bridge updated in thing-file: " + content)
end
In my case the bridge is named HMCFGUSB2
because I use that USB stick. You can change that in the rule to whatever name your bridge should have.
That’s it. Now you can restart your pod and watch how the Homematic bridge gets updated automatically