Credits go to: Panja0 https://www.reddit.com/r/pihole/comments/d5056q/tutorial_v2_how_to_run_2_pihole_servers_in_ha/
#### Why do you want to run 2 Pi-hole servers?
When you only have 1 Pi-hole server running and it needs maintenance or updates you'll lose internet (DNS resolution) connection. Or if your Pi-hole server needs a reboot or has an outage you will lose that DNS resolution and thus cannot browse the internet properly. Or if the Pi-hole FTL service (DNS service) fails on your Pi-hole server you'll end up with the above problems. We all have that girlfriend, boyfriend, wife, husband, kid or pet that really really NEEDS the internet NOW and will fall apart if it doesn't work... For this situation you really need/want/must have 2 Pi-hole servers.
#### Why do you want to run in HA and why not use them as primary and secondary DNS servers?
Running 2 Pi-hole servers is a must, you must know that by now. You can let you DHCP server handout Pi-hole server 1 as primary DNS server and Pi-hole server 2 as secondary. This works, of course, and many prefer it this way. But I do not! Network client/machines randomly choose (not going into detail here) which DNS server they will use, thus sometimes they use the primary and sometimes the secondary DNS server. If you want to check your query logs you'll never know for sure which server (Pi-hole 1 or 2) the machine used for its DNS queries.
Also when using primary and secondary DNS servers on your network DNS resolution can be slow(er) when 1 of the 2 servers is down. Let me explain that... When one of the 2 DNS servers is down (maintenance, update, reboot etc) and a client is doing a DNS query it will randomly pick the primary or secondary. If the client hits the DNS server that is down it will automatically switch to the other DNS server (which is what you want), though this takes extra time. We're talking about mili seconds, but hey every (mili) second counts in this modern internet world.
There for I wanted to have 2 Pi-hole servers but only use 1 of them at the time. When the first one goes down or the DNS service is not working, I want to switch to the second. If the first one comes back online it may switch back to that one.
Makes sense, right?!
## Requirements:
2 machines running Raspbian Stretch/Debian Stretch, this can be a Raspberry Pi, desktop, VM etc.
2 machines running Pi-hole (obvious).
DHCP server disabled on the Pi-hole's (DHCP needs to be done on your firewall, router or other device).
SSH access installed and enabled on both machines.
Rsync installed on both machines.
A user with the same username on both machines, which also has sudo permissions and SSH access.
## Tutorial configuration:
In this tutorial I will use the following configuration, alter this to match your setup.
Pi-hole 1
Role: Master or Active server
Hostname: pihole-dns-01
IP: 192.168.1.11
Pi-hole 2
Role: Backup or Standby server
Hostname: pihole-dns-02
IP: 192.168.1.12
High Availability:
IP: 192.168.1.20
#### Tutorial part 1 - create a sync between the 2 Pi-hole machines:
All of the 11 steps below need to be taken on both Pi-hole machines.
01) Login to the machine with the user which will be used for the sync script.
Make sure this user has SSH and sudo permissions.
02) Change directory to /usr/local/bin
```
cd /usr/local/bin
```
03) Create a script file, named pihole-gemini, and edit this file.
You can use any preferred text editor, I used nano.
```
sudo vim pihole-gemini
```
04) Paste the script into the pihole-gemini script file.
I've edited and removed a few lines from the original pihole-gemini script to have it working correctly in a HA setup.
Scroll down to the very end of the script. The very last line should read:
“${PIHOLE_COMMAND}” status
We will be adding a new command directly ABOVE that line, so that “${PIHOLE_COMMAND}” status remains the last line of the file.
The line we need to add is:
```
su -c /usr/local/bin/pihole-gemini - pi
```
Note that the “pi” at the end of the line should be replaced with the username of your sync user account.
Save the file and exit.
You can now invoke the script directly by calling pihole-gemini at the command line.
Try this now and see if everything works as expected. From now on, it will run automatically whenever you update gravity, add or remove items from the white or black list, or add or remove items from the block list (including enabling or disabling block lists).
Important note:
When upgrading to a new version of Pi-hole you will have to repeat step 11 in order to re-enable the pihole-gemini sync.
**gemini_chk** shell script that can be called manually (or added to a cron job) to make sure it's there after performing an update to pihole.
Put it into the /etc/scripts/ directory with the other scripts for keepalived, and run it as root after doing a pihole update.
If you decide to incorporate this, make sure the variables at the top are correct for the command line (replace the <geminiuser> with the username in your implementation) and the gravity script for your installation.
06) Restart the keepalived service. Run this command on both machines.
```
sudo systemctl restart keepalived.service
```
07) Change your DHCP server settings to hand out 1 (primary) DNS server and use the HA IP address: 192.168.1.20
#### Profit:
You're done! You now have a HA in-sync Pi-holed network. Whenever you change a whitelist, blacklist, blocklist or do a gravity update it will sync to the other Pi-Hole. The first Pi-hole server is being used as DNS server and can be reached on ip 192.168.1.20, whenever this machine goes down or the pihole-FTL (DNS) service is down the HA IP (192.168.1.20) will switch to the second Pi-hole server. When the first Pi-hole server comes back online it switches the HA IP (192.168.1.20) back to this server.
#### Closing words:
I have witnessed that a lot of people loved my v1 tutorial as much as I did. Hopefully you'll enjoy this v2 even more! Of course I would like to thank GeorgeT (https://discourse.pi-hole.net/u/GeorgeT) on the Pi-hole forum for his pihole-gemini sync script. Also the people behind Pi-hole itself are greatly thanked! Pi-hole is truly a kickass piece of software! Last but certainly not least I would like to thank all of you! The kind words I received on the v1 tutorial are really appreciated.