How to Setup Redis Cluster on Ubuntu 20.04 (Step by Step)

How to Setup Redis Cluster on Ubuntu 20.04. This article will start with an introduction of Redis server and it’s features, followed by the benefits of Redis Cluster and then setup of Redis Cluster on Ubuntu 20.04.

What is Redis Server?

Redis server stands for Remote Dictionary Server and in memory, fast, open source and key value data store. It is used as a database, message broker or a cache. It supports data structures (strings, hashes, lists, sets, bitmaps, hyperloglogs and geospatial indexes). 

There is two ways to set up your environment with Redis.

 

1. Redis Sentinel —when speed is not your primary concern. Best for small implementation. 
2. Redis Cluster which gives high availability plus clustering solution. Great for high availability and fast speed.

Redis Server Features (Pros)

Redis main benefits are:

  • Versatile data structures (strings, lists, hashes, Bitmaps, HyperLogLogs, Geospatial).
  • In-memory datastore.
  • Data Persistence.
  • Transactions
  • Pub/Sub
  • Lua scripting
  • Geospatial
  • Keys with a limited time-to-live
  • LRU eviction of keys
  • Cluster / Automatic failover.
  • Replication (primary replica architecture and  asynchronous). 
  • Supported languages include Java, Python, PHP, C, C++, C#, JavaScript, Node.js, Ruby, R, Go and many others.

What is Redis Cluster?

Redis cluster offers a convenient way to run the Redis server installation where operational data is shared across multiple Redis nodes. Redis cluster is a native sharding implementation that aids data distribution across multiple nodes.  This type of cluster also provides a degree of available space for the data partitions during the time of nodes failure or inability to communicate with other nodes.

Features of Redis Cluster

The following are the key features of the Redis cluster:

  • All data nodes in the Redis cluster are directly connected with a service channel.
  • It consists of TCP protocol base port + 4000 nodes.
  • It is a native Solution.
  • Node to node protocol is binary and they are optimized for bandwidth and speed.
  • Clients talk as usual using ASCII protocols with minor additions.
  • Nodes never proxy with queries.
  • Every ping and pong Packet contains a gossip section.

Benefits of Redis Cluster

  • The Redis cluster provides high performance and linear scalability up to 1000+ nodes. In the Redis cluster nodes, no proxies, asynchronous and no merge operations are used on values.
  • It provides a better way to write safely, as the system tries to retain all the message, that originated from the clients connected with the master nodes.
  • The Redis cluster is able to survive multiple data partitions where the majority of the data nodes are reachable.

In this post about how to Setup Redis Cluster on Ubuntu 20.04, we will show you how to install two node Redis Cluster on Ubuntu 20.04.

For the purpose of this article, we will use the following setup:

Hosts IP Address
Master
69.28.84.50
Slave
69.28.85.189

Setup Redis Cluster on Ubuntu 20.04

Install Redis Server

First, you will need to install Redis server package on both master and slave hosts. You can install it by running the following command on both hosts:

				
					apt-get install redis-server -y
				
			

Once the Redis server is installed, you can verify the status of the Redis service using the following command:

				
					systemctl status redis
				
			

You will get the following output:

				
					● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-01-11 09:00:51 UTC; 13s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1213 (redis-server)
      Tasks: 4 (limit: 2353)
     Memory: 1.8M
     CGroup: /system.slice/redis-server.service
             └─1213 /usr/bin/redis-server 127.0.0.1:6379

Jan 11 09:00:51 master systemd[1]: Starting Advanced key-value store...
				
			

Configure Redis Master

At this point, the Redis server is up and running on both hosts. Now, you will need to configure the Redis master and slave server.

First, go to the Redis Master node and edit the Redis main configuration file using the following command:

				
					nano /etc/redis/redis.conf
				
			

Change the following line to set keepalive time:

				
					tcp-keepalive 60
				
			

Uncomment and change the following line to make the server accessible from the remote hosts:

				
					bind 127.0.0.1 69.28.84.50
				
			

Change the following lines for backing up data:

				
					appendonly yes
appendfilename "appendonly.aof​"
				
			

Following that, change the following line to set the master password:

				
					requirepass masterpassword
maxmemory-policy noeviction
				
			

Save and close the file then restart the Redis service to apply the configuration changes:

				
					systemctl restart redis
				
			

Configure Redis Slave

Next, you will need to configure the Redis slave server. First, go to the Redis Slave node and edit the Redis main configuration file:

				
					nano /etc/redis/redis.conf
				
			

Change the following line to make the server accessible from the remote hosts:

				
					bind 127.0.0.1 69.28.85.189
				
			

Set the slave server password:

				
					requirepass slavepassword
				
			

Input the following line to define the master server:

				
					slaveof 69.28.84.50 6379
				
			

Enter the following line with your Redis master server:

				
					masterauth masterpassword
				
			

Save and close the file then restart the Redis service to apply the changes:

				
					systemctl restart redis
				
			

Verify Redis Master Slave Replication

At this point, Redis master and slave server are configured and listen to port 6379. You can check it using the following command:

				
					ss -antpl | grep redis
				
			

You will get the following output:

				
					LISTEN    0         511            69.28.84.50:6379             0.0.0.0:*        users:(("redis-server",pid=1934,fd=7))                                         
LISTEN    0         511              127.0.0.1:6379             0.0.0.0:*        users:(("redis-server",pid=1934,fd=6)) 
				
			

To check the Redis master server, go to the Redis master node and connect it using the following command:

				
					redis-cli -h 69.28.84.50 -p 6379
				
			

Once you are connected, authenticate the server with a password:

				
					69.28.84.50:6379> auth masterpassword
				
			

Next, check the master server status using the following command:

				
					69.28.84.50:6379> INFO
				
			

You will get the replication status of the master node in the following output:

				
					# Replication
role:master
connected_slaves:1
slave0:ip=69.28.85.189,port=6379,state=online,offset=98,lag=1
master_replid:5fa724462404aaa3e792951cbb7e77e6f9ac4221
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98

# CPU
used_cpu_sys:0.121895
used_cpu_user:0.116221
used_cpu_sys_children:0.000000
used_cpu_user_children:0.003588

# Cluster
cluster_enabled:0

				
			

Next, create a key value using the following command:

				
					69.28.84.50:6379> set master "This is master server"
				
			

Now, check the key value using the following command:

				
					69.28.84.50:6379> get master
				
			

The following output will be achieved:

				
					"This is master server"
				
			

Next step is to go to Redis Slave node and connect to the Redis instance with the following command:

				
					redis-cli -h 69.28.85.189 -p 6379
				
			

Next is to authenticate Redis using the password:

				
					69.28.85.189:6379> auth slavepassword
				
			

Following step is to check the key value, which you have created on the Master node:

				
					69.28.85.189:6379> get master
				
			

The following output will appear:

				
					"This is master server"
				
			

The above output indicates that the Redis replication is working properly.

Now, check the status of the Redis instance using the following command:

				
					69.28.85.189:6379> INFO
				
			

You should see that this node has the role of slave:

				
					# Replication
role:slave
master_host:69.28.84.50
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:328
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5fa724462404aaa3e792951cbb7e77e6f9ac4221
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:328
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:328

# CPU
used_cpu_sys:0.340056
used_cpu_user:0.237189
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0

				
			

Redis Slave to Master

In this part of how to Setup Redis Cluster on Ubuntu 20.04 is to check your Master Slave. If your Master server fails then you can promote the slave server to act as the backup master server. That way Redis server can continue to work while we resolve the issues on the master server.

To promote the Redis slave server as the master, connect to slave instance with the following command:

				
					redis-cli -h 69.28.85.189 -p 6379
				
			

Next is to authenticate the Slave instance with a password:

				
					69.28.85.189:6379> auth slavepassword
				
			

Following that is to turn off slave behavior with the following command:

				
					69.28.85.189:6379> SLAVEOF NO ONE
				
			

Your Slave server will now act as a master server. You can check it with the following command:

				
					69.28.85.189:6379> INFO
				
			

You will get the following output:

				
					# Replication
role:master
connected_slaves:0
master_replid:73978e7df3c6b75f7c453ea5b595f3d9f4c13c1a
master_replid2:5fa724462404aaa3e792951cbb7e77e6f9ac4221
master_repl_offset:412
second_repl_offset:413
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:412

# CPU
used_cpu_sys:0.447103
used_cpu_user:0.315920
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0

				
			

Reconnect to Master

Once the Redis Master server is up you will need to demote the slave server. On the Slave server, run the following command to reconnect it as a slave to the original master server:

				
					69.28.85.189:6379> SLAVEOF 69.28.84.50 6379
				
			

Now, verify the behavior of the Slave server using the following command:

				
					69.28.85.189:6379> INFO
				
			

You should see that your Slave server is now back to work as a slave.

				
					# Replication
role:slave
master_host:69.28.84.50
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:426
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5fa724462404aaa3e792951cbb7e77e6f9ac4221
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:426
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:413
repl_backlog_histlen:14

# CPU
used_cpu_sys:0.503667
used_cpu_user:0.372126
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0

				
			

How to Setup Redis Cluster on Ubuntu 20.04 Conclusion

In the above guide, we explained how to set up two node Redis cluster on Ubuntu 20.04 server. You can now add more slave nodes to scale the Redis cluster. I hope this setup will help you to prevent data loss. Redis is great for fast speed, its ability to store data types like sets and lists and strong contender for persistence.

 

Redis Cluster is a high availability and horizontal scalability solution for Redis that helps large organizations to split the data into multiple nodes to prevent data fail.

Redis cluster is self managed and it’s big benefit is that it removes the single point of failure that proxy servers brought.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x