So in this session we are going to set up Redis cluster on a Demo Environment.
So when I'm saying Demo Environment, we are going to set up Redis cluster on a single server.
However we are going to create multiple copies of Redis. So on the same server basically we are going
to launch six Redis instances. Now the steps to do that is very easy.
And also when you are doing the set up in the Production.
So underlying steps remain the same. So to begin with I have downloaded Redis software and I have
extracted it and now we have Redis-stable folder.
So if I go inside where Redis-stable,
this is a structure which you see. This is the default one.
Okay.
So what I'm going to do here is,
these are the Minimum configuration File, which you need to have in order to start a node in
a cluster mode.
Okay.
So when you are setting up Redis cluster. Basically first you need to start each of the nodes.
Okay.
So each of the nodes you need to start individually and then you run Create Cluster
Command, which is shown here.
So then you run Create Cluster, which is going to set up a cluster. Okay.
So I'm going to copy this configuration and please remember this is only for one node.
So if you are running on 6 different Servers, then you can copy paste exactly the same.
However if you're running all the six instances on the same server. In that case we need to make changes
in the Port.
Okay.
So I will tell you what we need to do.
So here I'm going to create a file, which is going to be called N 1
Okay.
This is the one which is going to be denoting Node one.
And then I'm going to say redis.conf.
Okay.
So notice this new Configuration File, which we're creating. In all of our previous examples we had a
used Redis.conf
Okay.
So just to make it simple. I'm just going to create a new configuration file and inside this I'm going
to paste whatever we had copied.
Okay.
Also I'm going to change the Port from 7000 to 7001.
So our Node 1 is going to have port of 7001
Okay.
This is the Client port.
So when you're connecting to Redis from Redis Client.
This is a port which you need to provide.
So other than that, we don't need to make any changes. So I'm just going to save and exit.
Okay.
And if I do a ls-lrt again. Now you see we have n1_redis.conf
So now I need to repeat this process six times.
Okay.
So I'm going to create a copy of this and going to name it N 2 for Node 2.
And similarly I will do the same for Node 3 and I will do the same for Node 4.
I'm going to do the same for Node 5 and 6 as well.
okay.
Now if we do ls-lrt, we can see we have 6 Configuration File.
However remember since we have copied all the configuration file from N 1. So all in all the configuration
file, Ports are going to be same.
So we must manually make sure that we are changing the port.
So I'm going to go to n2_redis.conf
And here I'm going to change the port to 7002 and for the third file N 3
I'm going to change the port to 7003
I'm editing this in VI Editor, but you can use any Editor. Just make sure the steps which you are following
is the same. Here I'm going to change it to 7004
This is a Fourth Node and for Fifth Node,
I'm going to change the port to 7005
if you are good in Scripting, you can in fact create a Script, which can do all this task, which I am doing.
OK.
So now we have all of this file which is set up with correct things.
Now let me do cat on all of this file.
OK.
So if I do cat n* ,since all our file begins with n. So now we can do cat n*
and.
And here I can see all my six Configuration File. So I can see .OK I have correct Port Setup.
So it is starting with 7001, 2, 3,4,5,6
So everything looks okay.
And this also marks that our step one is completed.
So this Configuration File, we have created 6 copies.
And also we have changed the configuration and when i am saying, changing the Configuration.
Basically we updated the port number.
Now I'm going to create 6 directories and I'm going to move each of this configuration file to its
respective directory.
So the command here basically is going to create six directories. So I want directory something like
7001 then 7002
Okay.
So rather than this I'm just going to use this command.
So this is basically a pattern and this is going to create 6 directories.
And if I do ls-lrt again. Now you can see, we have 6 new directories created.
So it is starting with 7001, 7002, 7003, 7004, 7005, 7006. Now I'm going to copy
each of the configuration file to its directory. so N1, I'm going to copy to 7001 and N 2
going to be copied to 7002
Okay.
In fact rather than copying, I will just move this Configuration File to its directories.
So I'm going to run mv command and going to move N1 to 7001
and we need to repeat the same for all the nodes.
Okay so this is done.
If I do ls-lrt again.
Now I do not see all this configuration file because all the configuration file has been moved inside
the respective folders.
So now we will go to each of the folders one by one and we will start Redis servers and also we
will pass the Configuration File.
Okay.
So rather than going inside, you can also or let's just keep it simple. Let's go inside the folders.
So now I'm inside 7001 and here if I do ls, I can see there's a Configuration File
there.
So now we're going to do Redis -server to start Redis.
And also we are going to provide this Configuration File, also I will use & to start this in background
and hit enter. Okay.
So this is the first one, which has started. Here you can see that, when you provided those directive, which
is related to Redis Cluster. Now it can see that it is a, it is going to be or supposed to be running in
a Cluster Mode. So you also see message saying, Running in Cluster Mode and Port is shown here as
7001.
So if remember our first node is, we had provided the Port number of 7001
So everything looks okay here.
And right now we are into this directory. So I'm going to go back to 7002 and then we are
going to run Redis-server and we need to provide this Second Configuration File. Hit enter
and then I will repeat the same process for all the 6 Nodes.
I have started 3 nodes and now this is the fourth node which i am going to start
and now sixth one is also started.
Okay so here if I do ps-ef grep redis. Now we can see we have 6 nodes, which are running
as a Cluster.
Okay.
So other than this we also need to run Redis cli command,
Okay to create the cluster.
So again here make sure you provide this Cluster Create Command and you provide all the IP addresses, where your
Redis is running. In my case
all of them are running on local host.
So I have provided 127.0.0.1
And also you need to provide the Port of each of the instances, where it is running and in the end we have
--cluster replicas
And we have given 1
So basically this is the number of your replicas.
So here you see the structure or the topology.
We are trying to install is this one. So we are going to have 1 Master and 1 replica, which is Slave
Node for each of the Masters.
Okay.
So I'm going to copy paste this from here.
Okay.
And let's hit enter.
So once you hit enter, it is telling you that, this is a Configuration which is, it is going to deploy.
So it is going to create 3 Masters and 3 Slaves.
Okay.
And the Masters are going to be 7001, 2 and 3.
Okay.
So notice,
whatever you passed in the beginning. These are going to be Masters and whatever you passed later
on, these are going to be Slaves.
If you want to change it, you can change it.
In this case I'm just going to keep it.
Yes.
Hit enter.
And now this is running Cluster Creation Scripts. Okay.
And we see the output.
And I do not see any error. So which means cluster is successfully started.
Okay.
Now one more thing which you can see here is, the Slots.
Okay.
So you see Master is having Slot of 0-5460
So as I told you, in Cluster data is stored into partition.
Okay.
So basically there are total 16000 partitions.
And since we have 3 master servers. 16000 is divided into 3 and each of the Master Series is going
to have that many number of Slots.
So if you divide 16000 by 3
So you are going to get approximately 5461 for each of the node.
Okay.
That is what has happened. So that is the same you can see in the configuration
so now if I hit enter. I'm just going to come back to Shell.
And now we can connect to Redis, using Redis Client.
So I'm going to use Redis-cli and we need to provide Port. Notice if
I do not provide Port here, if I just hit enter.
It is going to give us error
that Connection is refused, because if you see, by default it is trying to connect to 6 3 7 9 port.
So what we need here is, because we know our cluster is running on different ports.
Again if you just want to see all the services you can do ps-ef grep redis and
you can see these are the Ports, where our Redis services are running on.
So I'm going to now use Redis-cli and -p, I'm going to provide 7001
So you can connect to any node.
I'm just going to connect to 7001 and now you see, we are connected and we have not got
any error.
So now I'm going to connect to Redis cli
And then we'll be running some Key Value Pair. Basically we'll do some write and read operations.
So when you're connecting to Redis cluster. You can provide -c parameter.
So which is going to make it Cluster aware.
So here you can type Redis-cli and then you can type this -c parameter. Okay, which
is just going to make this Client aware that you are connecting to a Cluster and then you can provide
-p, followed with the Port, which
Your Redis is running. So here it is running on 7001. So I have provided 7001
So we have basically 6, if I exit from here if I do ps-ef grep redis, you
can see we have 6 instances running.So you can connect to any of that.
So I'm connecting to 7001, but if you want, you can connect to 7002 or any
instance.
OK.
So let's move back to 7001 and here if we perform this write operation
Now you see, when we perform this write operation, it saying the message of Redirected to slot this one. So
if you remember, when we set up Redis Cluster, all of the masters were assigned some number of Slots.
So when we wrote this Key Value Pair, Redis calculated hash of this key which is foo, and then that
Hash was in the slot, which is actually kept on this Master Server.
That's why internally it Redirected this to correct Slot.
OK.
Again this is something which is taken care internally. You don't need to worry about. Okay all of these
things are going to be taken care internally by Redis.
Now here if I set Value of Hello World, again you see this was supposed to be kept in this slot, which is
on this address.
So it has redirected again to this address.
Here also you can see your client.
Initially we had connected to 7001, then based on the slot where it was supposed to perform
this action. It had changed the address.
And again in Third Action, address has been changed.
So now what is going to happen, if you perform Read operation.
OK.
So as we know, when we wrote value of foo, it was stored into or stored to this address.
Okay.
So right now we are connected to 7001 Port and this is the one which is running on
7003
So in this case, when it is trying to get this value from this server. Basically this value is not going
to be available and because the hash of this is here.
So again it is going to do a Redirect action.
So let's see that, Ok as we hit enter. We can see now it has again Redirected it to the correct address.
So internally Cluster is aware of all of these Slots.
And that's why it is able to recognize where the request needs to be sent to. OK
I have exited.
Now let's see what else we can see.
So
and this is very good
Command.
This is a command which you can use to see status of Redis Cluster and what is the role of each of the
node.
So if I run this Command
again you saw I got the error because the port which, which was there is 7000.
But we do not have Redis, any of the Redis running on 7000.
We are having it running from 7001
So we need to change the port and Lets say, I want to connect to this instance.
You can run this command from any of the Redis Client. So I'm going to hit enter and now we're able
to connect and we get the value about Masters and also get the information about Slaves.
The other good command is, this one Cluster nodes.
so what it does is, it is going to give you information of all the nodes. Okay and
so this is how it gives all the information.
And now you can see we have a Master Node, then we have another Master Node and this is the Third Master
Node and we also have 3 Slave Nodes. Okay now let's shut down one of the Master Server and see what
happens.
Okay.
So I will do Redis-cli-P 7001 and then I will do shut down.
Okay.
So what this is going to do is. It is going to shut down
this one. Okay because this is the service, which is running on 7001. So it is going to shut
down this instance and then we should be seeing any other Slaves becoming the Master.
Okay.
So basically whatever, whoever was the Slave of this master is going to become, Next Master.
Okay so let's hit Enter
okay.
Now you can see all the logs
so let's try to understand the logs here.
So if you see from here.
So we actually tried to shutdown one of the Masters and then it got disconnected.
Okay.
And after that
Cluster got over there there is a state changed.
Okay.
And after that it started performing other action, to make other Slave Node turn into a Master.
Okay now I'm going to hit enter and we're going to run the same
Command to check information about all the Nodes. so once you hit enter. This time you see 7001 is
already down.
So we would't be able to connect to 7001
So I'm going to just connect to other available nodes. So which is let's see 7003
And once we do that. Now we can see, previously we had 7001,7002 and
7003 as a master.
And now we have 7005, turn to a Master.
Okay
so this was previously the Slave of 7001 node.
So
again you can play around with Redis Clustering. You can install it in Demo Environment and then you
can also play around with available Clients, which can interact with Redis Cluster.
So I usually use Python, but you can use python or Jython or probably any other available Clients
depending on the need of your project and then you should play around in the Development.
And that's how you're going to learn more.