To set up a Redis master-slave replication on Debian 12, you’ll follow these general steps on each server to configure the master and slave nodes:
Step 1: Install Redis
- Update your package list:
1 |
sudo apt update |
- Install Redis:
1 |
sudo apt install redis-server -y |
- Enable and start Redis on each server:
1 2 |
sudo systemctl enable redis-server sudo systemctl start redis-server |
Step 2: Configure Redis Master
- Edit the Redis configuration file on the master server (
/etc/redis/redis.conf
):
- Set a bind IP (default is
127.0.0.1
). You may want to change it to the server’s internal IP if necessary:bind 0.0.0.0
- Enable persistence (optional but recommended) by setting:
appendonly yes
- Restart Redis to apply changes:
bash sudo systemctl restart redis-server
- Verify the master server is running correctly by connecting locally or remotely (depending on your setup).
Step 3: Configure Redis Slave
On the slave server(s), edit the configuration to connect to the master server.
- Edit the Redis configuration file on each slave (
/etc/redis/redis.conf
):
- Set the IP for the slave to listen to, or leave it as the default
127.0.0.1
. - Add the master server’s IP address and port (default is
6379
):replicaof <master_ip> <master_port>
- Set a password if your master server has one (optional):
masterauth <your_master_password>
- Restart Redis on the slave to apply the configuration:
bash sudo systemctl restart redis-server
Step 4: Testing the Replication
- On the master server, add a key-value pair using the Redis CLI:
1 |
redis-cli set test_key "This is a replication test" |
- On the slave server, check if the data is available by fetching
test_key
:
1 |
redis-cli get test_key |
If replication is working, you should see the same value.
Step 5: Monitor Replication
To check the replication status:
- On the master server, run:
1 |
redis-cli info replication |
It should show connected_slaves: 1
or more, depending on the number of slaves.
- On each slave server, run:
1 |
redis-cli info replication |
It should show role:slave
and master_link_status: up
.
Optional Step: Securing Redis
- Set up a Redis password on the master by editing
/etc/redis/redis.conf
:
1 |
requirepass <your_password> |
- Update the
masterauth
parameter on the slave(s) to match this password. - Restart Redis on all servers to apply the changes.