Wednesday, January 7, 2015

Redis Master slave Replication

              

            Hello and welcome.from the past few days my Brother  given me a book and i start reading and started implementing the same on my local environment.The book was about the Radis and how Radis makes a difference, whats are its pros and cons. etc etc and how we can start using it.

 well then lets start with the Topic of interest installing a redis is not a much deal we all know Redhat/Centos and Debians like Ubuntu both supports redis so we can install then using the default installer like 
 
STEP 1 Installation 
 
In Redhat / Centos we need to add EPEL and Remi and then simply update the yum data and then "yum install redis"
 after that 

#service redis start
chkconfig --level 35 redis on
service redis start
 
In Ubuntu just fire "apt-get install redis-server"
  
Note: I am using Centos 6.6 

STEP 2 SETTING UP REPLICATION

Assume we have two IP Address 
192.168.1.101 ----> Masters_ip
192.168.1.102 ----> Slave_ip

Setup on Master

Open the terminal as a root or use sudo -i to gain the access of root

Note:Location of these conf file depends distribution and on type of installation from source or from standard package used 

#vim /etc/redis.conf

By default the redis bind to loopback ip-address we need to change it make it more appropriate.
so change 

bind 127.0.0.1 

to

bind 192.168.1.100

For security prospective need to add security as its start listning on network now. 
so change like from this
#requirepass foobared 

to  

requirepass myn@m31sN@vn33tR@t1v3 #some thing like this more    
                                  #complex 

and then save the file and restart the redis server with command

# service redis restart

On Slave Machine 

open the redis conf file using vim /etc/redis.conf

change 

bind 127.0.0.1 

to

bind 192.168.1.101

also set a password by default on new version of redis the redis in read only mode and it will be on network now so change the authentication setting for it provide it with some complex password 
so change the 


#requirepass foobared 

to  

requirepass myn@m31sN@vn33tR@t1v3 #some thing like this more    
                                  #complex 

Setting up Replication Actually

The below steps will outline setting up replication from the Slave. Replication only needs to be defined on Slave systems, the Master server does not require any special configuration.
Specifying the Master

In the slave configuration file specify the master server to replicate from. Redis has the ability to replicate from a slave, to set this up you would simply specify the first slaves details in place of the master.

  
change the below like from

# slaveof <masterip> <masterport>

to

slaveof 192.168.1.101 6379 # or some thing like that as per                                 #your need 

If you set a password earlier for the master server you will need to specify that password via the masterauth setting.

change

# masterauth <master-password>

to

masterauth 
myn@m31sN@vn33tR@t1v3

Start the Service 


Step 3 Testing of your Replication.

To test the replication we will first login to the master server and set a key-value.
Set the key-value on the Redis Master

To login to the Redis server we will use the redis-cli client. The -h flag specifies the host to connect to, by default the redis-cli will connect to the localhost IP.


#redis-cli -h 192.168.1.100


Once logged in use the AUTH command with the password specified earlier.
 

#redis 192.168.1.100:6379> AUTH <password>
 OK

After authenticating we can now set a key-value pair using the SET command or thing you like you can put it in.I have taken key value pair to test it.

 #redis
192.168.1.100:6379> SET replicated:Navneet Rathi
 OK 


 Get the key-value on the Redis Slave

Now that data has been saved to the master we will login to the Redis Slave server using the same redis-cli command and use the GET command to retrieve the replicated data.


# redis-cli -h 192.168.1.101
redis
192.168.1.101:6379> AUTH <password>
 OK

Once logged in use the GET command to retrieve the value from the replicated:test key

 redis
192.168.1.101:6379> GET replicated:Navneet
 "Rathi"



and we are done.
We can also set the moniting for it we will check that out in nex blog till then enjoy... 



 

No comments:

Post a Comment