I created a script (MicroHA) 
The script can be put in cron to run every 30 seconds
The structure of the script is
- Checking local LXD installation
- Checking the connection to the server using Ping
- Check port activity with Netcat
- Switching to the active server
- Checking if LXD is active on a remote server
- Select Master LXD Remote
The output will have the following format
Checking local server...
LXD installed and running on this machine ✅
Checking ping on the worker1 server...
Ping to worker1 is not active ❌
Checking ping on the worker2 server...
Ping to worker2 is active ✅
Cheking port on the worker2 server...
Port 8443 on worker2 is active ✅
Switching to another server...
Switching to worker2 server ✅
Checking LXD service on worker2 server...
LXD running on worker2 server ✅
worker2 server selected as master server ✅
Contents of the script
#!/bin/bash
# Local server check
echo "Checking local server..."
if lxc info --force-local > /dev/null 2>&1; then
echo "LXD installed and running on this machine ✅"
else
echo "LXD is not installed or not running on this machine ❌"
fi
# Define list of servers
SERVERS=("worker1" "worker2" "worker3" "worker4")
# Loop through servers
for SERVER in "${SERVERS[@]}"; do
echo "Checking ping on the $SERVER server..."
if ping -c 1 "$SERVER" > /dev/null 2>&1; then
echo "Ping to $SERVER is active ✅"
else
echo "Ping to $SERVER is not active ❌"
continue
fi
echo "Cheking port on the $SERVER server..."
if nc -z -w 1 "$SERVER" 8443 > /dev/null 2>&1; then
echo "Port 8443 on $SERVER is active ✅"
else
echo "Port 8443 on $SERVER is not active ❌"
continue
fi
echo "Switching to $SERVER server..."
if lxc remote switch "$SERVER" > /dev/null 2>&1; then
echo "Switched to $SERVER server ✅"
else
echo "Not switched to $SERVER server ❌"
continue
fi
echo "Checking LXD service on $SERVER server..."
if lxc info > /dev/null 2>&1; then
echo "LXD running on $SERVER server ✅"
echo "$SERVER server selected as master server ✅"
break
else
echo "LXD is not running on $SERVER server ❌"
continue
fi
done