Configure Redis Master and Slave Replication On Linux

Configure Redis Master and Slave Replication On Linux

We can set up and configure the Redis Master and Slave replication Cluster. You must have connectivity between both the servers and Port 6379 should be opened in the firewall rules. Make sure the versions should be the same on both Master and Slave Server otherwise you will get replication issues.

Check Out: Error NOAUTH Authentication required while Accessing Redis

Redis Master Cluster Configuration:

Install Redis and start the services using the below commands.

[root@redis-master ~]# yum install epel-release redis -y
[root@redis-master ~]# systemctl start redis
[root@redis-master ~]# systemctl enable redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

Now take the backup of an existing redis.conf and run the commands to open the required parameters. 

[root@redis-master ~]# cd /etc/
[root@redis-master etc]# cp redis.conf redis.conf-back
[root@redis-master etc]# vim redis.conf

You must have pong output when you ping the server. We can also check the version using the below commands.

[root@redis-master ~]# redis-cli --version
redis-cli 5.0.3
[root@redis-master ~]# redis-cli ping
pong

Check Out: How To Create Account On sslforfree To Generate Certificates

Open the redis.conf and look for bind we need to change the localhost IP address to our local Master server IP and port should have listened to 6379. 

bind 192.168.185.147
port 6379

Next, we will set the password for Redis Master for unauthorized access and look for requirepass in the same file. This helps Redis to detect network or service problems. Bt default, the value would be 300 seconds. 

requirepass redistest
tcp-keepalive 60

Now restart the service and log in to the Redis server using the below commands. 

[root@redis-master ~]# redis-cli -h 192.168.185.147 -p 6379 -a redistest
192.168.185.147:6379>

As you can see we’re able to log in to the Redis server using the password. We will test the Redis Master Cluster. If requirepass is set and you try to access the Redis CLI, check the replication information it should not display until you don’t provide the password. The output should look like: 

[root@redis-master ~]# redis-cli
127.0.0.1:6379> info replication
NOAUTH Authentication required.

Now Provide the auth password and check the replication information. This should show you the information about the Redis.

127.0.0.1:6379> auth redistest
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:c04380207eadd4e9a5e9fc8c7a0a81319af8b884
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

Check Out: How To Make EBS Volume To Use On AWS Cloud

We have to set a key to test the replication later. 

127.0.0.1:6379> set test "This is the Redis master server we're trying to setup master and slave"
OK
127.0.0.1:6379> exit

Check the test is stored on Master using the below commands. 

[root@redis-master ~]# redis-cli
127.0.0.1:6379> auth redistest
OK
127.0.0.1:6379> keys *               ## This will show you the sessions ID
1) "test"
127.0.0.1:6379> get test
"This is the Redis master server we're trying to setup master and slave"

If you want to connect Redis Master and Slave with securely with a password, we will have to add the masterauth password in Master redis.conf and the same password we will put in the slave file otherwise it won’t work. Open the configuration file then look for masterauth and set the password. you can use requirepass as masterauth in Slave server.

Restart the service. 

Redis Slave Configuration:

You can either use the same port or change it to another one. I will use the same port as 6379 to make the connection between both servers. The bind IP would be 127.0.0.1.

[root@redis-slave ~]# netstat -tnlp | grep redis
tcp  0  0   127.0.0.1:6379    0.0.0.0:*       LISTEN 1903/redis-server 1

Check Out: How To Install Prometheus Monitoring Tool On Linux

Install and open the redis.conf file to make changes and look for slaveof in version 3 and you will see the replicaof in version 5. The replica mode would be read-only on the Slave server. 

# slaveof <masterip> <masterport> 
slaveof 192.168.184.147 6379
replica-read-only yes

Next, uncomment the masterauth and put the Redis master password here. 

# masterauth <master-password>
masterauth redistest

In a similar way, Set the requirepass to secure the unauthorized access to the slave Redis. 

# requirepass foobared
requirepass redisslave

Save the file and restart the service. 

Check Out: How To Setup Multi-Factor authentication In Amazon Web Service

The Issue can be faced:

When you face the below issue on the Slave server. So, This can be fixed by setting a few parameters in the Redis Master Server.  you can check logs using the below commands.

tail -f /var/log/redis/redis.log
MASTER <-> REPLICA sync started
Error condition on socket for SYNC: Connection refused

When you see this warning in Redis log file. To fix it, overcommit_memory is set to 0! Then we need to set this parameter to 1. 

[root@redis-master ~]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@redis-master ~]# systcl --system            ## or Reboot the machine

If still, you face the same issue on Slave, we have to set another parameter on Master using the below command. This will create latency and memory usage issues with Redis. you can find these warnings in the log file.

[root@redis-master ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

To make it permanent, add it to /etc/rc.local.

Check Out: How To Install And Configure Redis Server On Linux

Check log on Master Redis, you will see the synchronization succeeded.

[root@redis-master ~]# tail -f /var/log/redis/redis.log 
1778:M 21 Apr 2020 10:09:29.249 * DB loaded from disk: 0.000 seconds
1778:M 21 Apr 2020 10:09:29.249 * Ready to accept connections
1778:M 21 Apr 2020 10:09:30.020 * Replica 192.168.185.143:6379 asks for synchronization
1778:M 21 Apr 2020 10:09:30.020 * Full resync requested by replica 192.168.185.143:6379
1778:M 21 Apr 2020 10:09:30.020 * Starting BGSAVE for SYNC with target: disk
1778:M 21 Apr 2020 10:09:30.020 * Background saving started by pid 1782
1782:C 21 Apr 2020 10:09:30.023 * DB saved on disk
1782:C 21 Apr 2020 10:09:30.023 * RDB: 0 MB of memory used by copy-on-write
1778:M 21 Apr 2020 10:09:30.051 * Background saving terminated with success
1778:M 21 Apr 2020 10:09:30.052 * Synchronization with replica 192.168.185.143:6379 succeeded

Test the Data synchronization:

Check this on Redis Slave server and you must be receiving the data which we have created earlier on the Redis Master. you can also check the Replication information.

[root@redis-slave ~]# redis-cli
127.0.0.1:6379> auth redisslave
OK
127.0.0.1:6379> get test
"This is the Redis master server we're trying to setup master and slave"
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.185.147
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:1120
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:0b0d37d21403eb75663a806fa9f46994f7e64bcc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1120
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1120
Promoting Slave As Master Configuration:

Suppose, when the Master goes down and Slave won’t be able to receive the events generated on Master Redis. So, we can promote Slave as a Master to prevent data loss and downtime. we will test it on Slave creating a set value on Slave Server using the following commands. 

[root@redis-slave ~]# redis-cli
127.0.0.1:6379> auth redisslave
OK
127.0.0.1:6379> set test "we're trying to make it as master"
(error) READONLY You can't write against a read only replica.

Check Out: Docker Swarm Error response from daemon: RPC error: code

You should get these an error because Slave is configured as read-only. 

We will disable it using the below commands. 

127.0.0.1:6379> slaveof no one
OK
127.0.0.1:6379> set test "we're trying to make it as master"
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:a5920db1c751f611dcaad58ed68b7962b7ebf3d6
master_replid2:0b0d37d21403eb75663a806fa9f46994f7e64bcc
master_repl_offset:4944
second_repl_offset:4859
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:4944

Now The Slave will behave as Master Server. Now This is done from the CLI, if you restart the service again, it will again behave as Slave. we will need to modify the configuration file to make it a permanent Slave as a Master Server.

We will test more conditions:

  1. Revert back the configuration and if you need to make it as Slave, run the below commands.
127.0.0.1:6379> slaveof 192.168.185.143 6379    ## master ip and port
OK

We need to exit the slave CLI and restart the service.

Check Out: How To Install and Enable RepoForge Repository On Linux

2. We will again check the status of Slave Server using the same way. This should behave as a Slave Server.

[root@redis-slave ~]# systemctl restart redis
[root@redis-slave ~]# redis-cli
127.0.0.1:6379> auth redisslave
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.185.147
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:4886
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:0b0d37d21403eb75663a806fa9f46994f7e64bcc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4886
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4859
repl_backlog_histlen:28
127.0.0.1:6379> get test
"This is the Redis master server we're trying to setup master and slave"

As you can see, we have reverted back the configuration and it’s working fine as earlier.

You’re done configure redis master

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment