Setup a Redis Cluster
Introduction
We are going to configure a redis cluster with three nodes and three slaves running on Ubuntu 14.04. Like many clusters, you always want to have at least odd numbers of nodes, in our case, three masters. The slaves are more like backup to failover incase a master server crashes. It is critical to configure a slave for each master as the masters in the cluster do not share the same data, but the cluster uses a kind of sharding where every key is conceptually part of what they call a hash slot.A Redis cluster must always have 16384 hash slots, and each master is responsible for a subset of these hash slots. So if a master with a certain subset of hash slots is down, it means that queries searching for data served by those hash slots cannot be served. And this is why it is critical to have slaves, so then can failover and take over the responsibility of orphaned hash slots.
Architecture
Primary Nodes
Node 1: redis-1-1Node 2: redis-2-1
Node 3: redis-3-1
Slaves
Node 4: redis-1-2Node 5: redis-2-2
Node 6: redis-3-2
Install Redis on Ubuntu from PPA:
sudo apt-get updatesudo apt-get install -y redis-server
Note: Check if the redis version is the latest version. I like to use the PPA that our team has created because we update as soon as a new release is announced.
sudo add-apt-repository ppa:gaod/redis-server
sudo apt-get update
sudo apt-get install -y redis-server
To check the version after installation, just do:
dpkg -l |grep redis
Once the program has been installed, Redis comes with a built in script that sets up Redis to run as a background daemon. This script is called install_server.sh. If you do not find the script incase it was not installed along, you can find one on line and just download it.
Install redis gem
sudo sugem install gem
Edit Redis Config file
Edit the following variables in the conf file /etc/redis/redis.conf# redis.conf
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
appendonly yes
Restart Redis
Restart the servers for the changes to take effect.sudo service redis-server restart
Creating the cluster
Now that we have all redis instances running, we need to create our cluster by writing some meaningful configuration to the nodes. We can easily accomplish this by running the redis-trib.rb script. The script basically executes special commands in the instances in order to create new clusters, check or reshard an existing cluster, and so forth. The redis-trib utility is in the src directory of the Redis source code distribution. To create your cluster, we are going to run the following command:./redis-trib.rb create --replicas 1 redis-1-1:6379 redis-2-1:6379 redis-3-1:6379 redis-1-2:6379
redis-2-2:6379 redis-3-2:6379
Note: you have to use IP address instead of hostnames for some reason.
The command used here is create, since we want to create a new cluster. The option --replicas 1 means that we want a slave for every master created. The other arguments are the list of addresses of the instances I want to use to create the new cluster. If only want to create a cluster of three and no slaves for example, we would exclude the part "--replicas 1" and just include three ip:port obviously. But our design includes three redis nodes and three slaves.
Redis-trib will propose you a configuration. Accept typing yes. The cluster will be configured and joined, that means, instances will be bootstrapped into talking with each other. Finally if everything went ok you'll see a message like this:
OK] All 16384 slots covered
This means that there is at least a master instance serving each of the 16384 slots available. You can now play around with the cluster.
No comments:
Post a Comment