Set up a Redis cluster
Most guides I come across start with explaining what Redis is and/or how to install Ubuntu. I'm not doing that.. Most people looking to install Redis already know what it is and most of them aren't looking for a "This is a computer" kind of guide.
So, lets start shall we?
Requirements
Okay, some stuff needs te be in place before following this guide. I make the assumption you already know how to set those requirements up.
- minimum of 3 linux servers with:
- root or sudo access
- accessibility to ports 7000, 7001, 17000 and 17001
- emacs installed (kidding of course, any editor will do)
Install Redis
The method of installing Redis comes down to the distribution you (have to) use. For a RedHat based distro, this will be:
sudo dnf install redis
For a Debian based distro, it's:
sudo apt install redis-server
Configure the operating system
Because Redis is an in-memory database we have to make some ajustments to the OS. These are mostly (exclusively?) changes in the way the systems handles memory related events.
With your favorite editor (another change to mention emacs?), create the file
/etc/rc.local
with the following content:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. echo never > /sys/kernel/mm/transparent_hugepage/enabled sysctl -w net.core.somaxconn=65535 exit 0
Next, edit the file
/etc/sysctl.conf
and add the following line to the end of the file:
vm.overcommit_memory=1
Configure Redis
Now we've come to configuring Redis itself. First create some directories:
sudo mkdir /etc/redis/cluster sudo mkdir /etc/redis/cluster/7000 sudo mkdir /var/lib/redis/7000 sudo mkdir /etc/redis/cluster/7001 sudo mkdir /var/lib/redis/7001
Then, make a config file for Redis on ports 7000 and 7001 (replace [PORT] with the respective port number):
/etc/redis/cluster/[PORT]/redis_[PORT].conf
For the content of these files, again replace [PORT] with the actual port number and set a secure password in place of [PASS].
port [PORT] dir /var/lib/redis/[PORT]/ appendonly no protected-mode no cluster-enabled yes cluster-node-timeout 5000 cluster-config-file /etc/redis/cluster/[PORT]/nodes_[PORT].conf pidfile /var/run/redis/redis_[PORT].pid logfile /var/log/redis/redis_[PORT].log loglevel notice requirepass [PASS] masterauth [PASS]
Now give the redis user ownership and access rights to everything in the redis folders:
sudo chown redis:redis -R /var/lib/redis sudo chmod 770 -R /var/lib/redis sudo chown redis:redis -R /etc/redis
To start both redis instances (the one on port 7000 and the other on 7001) we need to create a service file for both: (again replace the [PORT] tag)
/etc/systemd/system/redis_[PORT].service
[Unit] Description=Redis key-value database on [PORT] After=network.target [Service] ExecStart=/usr/bin/redis-server /etc/redis/cluster/[PORT]/redis_[PORT].conf --supervised systemd ExecStop=/bin/redis-cli -h 127.0.0.1 -p [PORT] shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755 LimitNOFILE=65535 [Install] WantedBy=multi-user.target
Lastly, disable the default redis service and enable both the 7000 and 7001 versions we just created. Don't forget to reload systemctl so we can access the new service files.
systemctl daemon-reload systemctl disable redis systemctl enable redis_7000 systemctl enable redis_7001
Then reboot, so all changes can take effect and the Redis instances will be started.
Start cluster mode
You now have 3 servers with 2 instances of Redis each. Last thing to do is cluster them together. The only thing you need is the IP adresses of these servers to use in this command:
redis-cli --cluster create IP1:7000 IP1:7001 IP2:7000 IP2:7001 IP3:7000 IP3:7001 --cluster-replicas 1 -a [PASS]
Don't forget to substitute the IP adresses and the password you used earlier. Type 'yes' to accept the configuration
Check if everything works properly
From one of the servers login with the following command:
redis-cli -c -h IP -p 7000 -a [PASS]
On the redis-cli prompt, run the command:
CLUSTER NODES