未加星标

Configuring redis MASTER SLAVE setup

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
We discussed in our previous tutorial, about redis server & its installation . In this tutorial, we will learn to create a redis cluster or rather a master slave setup for redis server. With master slave setup, both the servers will be identical copies of each other & in the event master fails then we can use slave server as master until we have fixed the master server. So let’s start with the process,
(Recommended Read : Installing MEMCACHED in Ubuntu & RHEL/CentOS )

(Also Read : How to install or upgradephp version to PHP 7 )

Pre-requisites

We will need to servers with redis installed (check out our redis installation tutoria l), one of the server will act as master & other one as slave. For purpose of this tutorial, setup information is as follows,

Master server

IP address 192.168.1.10 Redis port number 6379

Slave server

IP address 192.168.1.11 Redis port number 6379

We will now start with the configuration on the redis server.

Configuration (MASTER)

We only need to make couple of changes in master redis instance, open the redis instance configuration file i.e. /etc/redis/6379.conf,

$ vi /etc/redis/6379.conf

Then look for paramter ‘bind’ & change it to,

bind 0.0.0.0

Next, search for parameters ‘appendonly’ & ‘appendfilename’ , then change them to,

appendonly yes

appendfilename “appendonly.aof”

These are the only changes required in master server. Save file , then exit & restart the redis instance to implement the changes made,

$ service redis_6379 restart OR systemctl restart redis_6379

Configuration (SLAVE)

Now we will make changes to slave configuration file i.e. /etc/redis/6379.conf. To make the changes, open the file with vi editor

$ vi /etc/redis/6379/conf

As we did for master, search & change the ‘bind’ parameter

bind 0.0.0.0

Next search for parameter ‘slaveof’, this parameter is where we mention our master server IP address & redis instance port number,

slaveof 192.168.1.10 6379

Save file, then exit & restart the redis service,

$ service redis_6379 restart OR systemctl restart redis_6379

Verifying master slave setup

Now that we have made the necessary changes on both, master & slave, servers, we will verify if our setup is working as expected or not. To check if the master slave setup is working, connect to the master redis instance using the following command,

$ redis-cli -h 192.168.1.10 -p 6379

& execute the command ‘info’,

192.168.1.10:6379> info

In the result, head down to ‘Replication’ section

# Replication

role:master

connected_slaves:1

Slave0:

ip=192.168.1.11,

port=6379,

state=online,

offset=215,

lag=0

master_repl_offset:215

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:214

here you can see information about all the slaves connected to the master redis instance. This shows that our master slave replication is working. We can further carry out another test to make sure that data replication among master & slave is working fine by creating a test key-value in master & then it should get replicated to slave.

To create a test key-value, login to redis instance & execute the following command,

192.168.1.48:6379> set test “testing”

now to check the key on master, run command ‘get test’

192.168.1.48:6379> get test “testing”

Now, login to slave server & check if the key ‘test’ has been replicated or not,

$ redis-cli -h 192.168.1.152 -p 6379

& run ‘get test’ command,

192.168.1.152:6379> get test “testing”

This shows that key has been replicated to the slave server as well & our master slave data replication is working fine.

Promoting redis SLAVE as MASTER

In the event master server fails, we can then promote the slave server to act as the backup master server, so that redis server can continue to work while we resolve the issues on the master server. To promote redis slave server as master, connect to slave instance

$ redis-cli -h 192.168.1.152 -p 6379

& run the following command,

192.168.1.152:6379> SLAVEOF NO ONE

OK

Our slave server will now act as a master server.

Note:-If we have 2 or more slaves connected to master , then we need to run the command ‘SLAVEOF 192.168.1.152 6379’ to make them point towards the new master server.

Reconnecting original master server

Once the original master server is up, we will demote the slave server (that was promoted to master) & reconnect it as slave to original master server by executing,

192.168.1.152:6379> SLAVEOF 192.168.1.48 6379

Then run the same command on other slave servers as well.

(OPTIONAL) Setting password authentication for the redis instances

In order to set password authentication for a redis instance, open configuration file for the redis instance i.e. ‘/etc/redis/6379.conf’ & uncomment ‘requirepass’ parameter,

$ vi /etc/redis/6379.conf requirepass redis

here, ‘redis’ is the password for the instance. Save file & exit, then restart the service to implement the changes,

$ service redis_6379 restart

Now login to the instance,

$ redis-cli -h 192.168.1.48 -p 6379

We will not be prompted for password, we need to execute command ‘AUTH’ followed by password,

$ 192.168.1.48:6379> AUTH redis

Once authenticated, we can use the red

本文数据库(综合)相关术语:系统安全软件

代码区博客精选文章
分页:12
转载请注明
本文标题:Configuring redis MASTER SLAVE setup
本站链接:https://www.codesec.net/view/621037.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(47)