High Availability Domoticz Cluster

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Post Reply
User avatar
Antori91
Posts: 79
Joined: Sunday 12 February 2017 18:12
Target OS: NAS (Synology & others)
Domoticz version: 3.5877
Contact:

High Availability Domoticz Cluster

Post by Antori91 » Thursday 05 July 2018 20:40

Hello,

I've completed the first version of my own basic High Availability Domoticz Cluster. This cluster is based on two servers (active/passive nodes) synchronized via MQTT. Each server has its own Domoticz instance and is also an MQTT server.

Under normal condition:
- the service is delivered by the main server and its Domoticz instance,
- the backup server monitors the main server (by sending every minute a JSON request to Domoticz) and synchronizes its own Domoticz database.

Failover happens if:
- Domoticz in the main server fails. The Backup Domoticz instance is then used to continue to deliver the service. This is done by republishing the MQTT incoming/outcoming messages between the two servers,
- or the main MQTT server fails (or even the server goes down). In this situation, the backup server becomes the main server by taking the IP address previously used by the main server.

All the stuff is done by only one (nodejs) script running only in the backup server.

The two servers have not to be the same one or even don't have to have the same hardware architecture. Also, the Domoticz versions and the databases structure on the two servers can be different. There is a basic repository within the script where you can declare some attributes for each Domoticz device you want to synchronize under normal condition. These attributes are :
- the device id,
- if it's incoming or outcoming message,
- the device type from a Domoticz perspective. For incoming message, it can be any device type. For outcoming message, it's currently only Light/Switch, SelectorSwitch, Thermostat or Temperature.

In my environment, it's an End to end High Availability solution because all my sensors/actuators are ESP8266. They are polled by scripts from the backup server when they are web server or they are able to reconnect themself to MQTT after the failover is done when they are MQTT client.

nigels0
Posts: 231
Joined: Thursday 23 January 2014 13:43
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: High Availability Domoticz Cluster

Post by nigels0 » Thursday 05 July 2018 23:00

Are you going to put together a wiki?

User avatar
Antori91
Posts: 79
Joined: Sunday 12 February 2017 18:12
Target OS: NAS (Synology & others)
Domoticz version: 3.5877
Contact:

Re: High Availability Domoticz Cluster

Post by Antori91 » Friday 06 July 2018 11:16

If it seems interesting to many people, I'm ok to write a Wiki. In the meantime, I have published the code of the script and an example of .sh file to launch it at Github : https://github.com/Antori91/Home_Automa ... %20Cluster

To try to run it, first you have to:
- install Domoticz and MQTT in each server. In my environnement the Backup server has Ethernet and WiFi interface (it's a Raspberry),
- install in each Domoticz the "MQTT Client Gateway with LAN interface" hardware with Remote address=localhost, port=1883 and Publish Topic=out,
- install node.js (with the mqtt module https://www.npmjs.com/package/mqtt ) in the Backup server,
- modify the setup of the main Domoticz, Local Networks (no username/password) parameter, in the Main server to accept connections from the Backup server without authentication (this for the Ethernet IP address and the WLAN0 WiFi IP address as well of the backup server). Also at least the Backup server must have "localhost;127.0.0.*" in this parameter to accept connection from this script without authentication,
- copy the two .js and the .sh files to your own installation directory in the Backup server,
- update the WiFi_DZ_MQTT_SecretKeys.js file with your own parameters,
- change line 18 of the script to tell it where to find your WiFi_DZ_MQTT_SecretKeys.js file (can be eventually another directory than your installation directory) : const MyJSecretKeys = require('/home/pi/iot_domoticz/WiFi_DZ_MQTT_SecretKeys.js');
- and of course update the basic repository in the script at line 75.

The repository looks like :

Code: Select all

// Here all Domoticz Idx to synchronize [$$SYNC_REPOSITORY]
var   myIDXtoSync      = [ new IDXtoSync( 50, "mqtt/out", "Light/Switch" ),  new IDXtoSync( 51, "mqtt/out", "Light/Switch" ),   new IDXtoSync( 34, "mqtt/in", ""),   // 50/51=Entree and Mezzanine lighting, 34=Hot Water Tank
                           new IDXtoSync( 27, "mqtt/in", "" ),  new IDXtoSync( 28, "mqtt/in", "" ),   new IDXtoSync( 29, "mqtt/in", ""), new IDXtoSync( 30, "mqtt/in", "" ),   new IDXtoSync( 31, "mqtt/in", ""),   // Ground Floor Heaters
                           new IDXtoSync( 35, "mqtt/in", "" ),  new IDXtoSync( 36, "mqtt/in", "" ),   new IDXtoSync( 37, "mqtt/in", ""), new IDXtoSync( 38, "mqtt/in", "" ),   // First Floor Heaters   
                           new IDXtoSync( 17, "mqtt/out", "SelectorSwitch" ), new IDXtoSync( 16, "mqtt/out", "Thermostat" ) ];   //  17=Main OFF/HORSGEL/ECO/CONFORT heating breaker, 16=Heating thermostat setpoint
                           // heating display/schedule to add to the repository in the future
// [$$SYNC_REPOSITORY]
For each device you would like to synchronize under normal condition, you have a declaration like new IDXtoSync( 50, "mqtt/out", "Light/Switch" ) or new IDXtoSync( 31, "mqtt/in", ""). For incoming MQTT message from a sensor/actuator, you just have to say new IDXtoSync( 31, "mqtt/in", "") where 31 is for example your device number. For outcoming MQTT message from Domoticz, you have to use IDXtoSync( 50, "mqtt/out", "Light/Switch") where 50 is for example your device number and "Light/Switch" your device type. For device type, you can use currently only "Light/Switch", "SelectorSwitch", "Thermostat" or "Temperature" (it is basic temperature device without humidity).

After a failover, before restarting the service of the main server, you must first restart this script (and eventually copy the backup Domoticz database to the main server). Currently, the failover happens after 15 mn of continuous failure. You can change this parameter at line 30: const TIMEOUT = 15; Also, in case of complete failover, I mean the Backup server becoming the main server, it is done by changing the WiFi IP address of the Backup server and not its Ethernet IP address. Can be changed but not tested at line 45 : commandLine = 'sudo ifconfig wlan0 ' + newIPaddress;

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests