Redis HA articles + master-slave construction

Description: This article is a guidebook for Redis master-slave construction
Tags: Redis master-slave, Redis high availability, Redis read-write separation, Redis 4.0.2
Note: The unnecessary redundant parts are deleted in the article, so that beginners can learn at a glance Will
Reminder: If you find something wrong with this article or there is a better way to write it, please leave a message or private message me for modification and optimization


★ Preface
※ The document structure adopts: stand-alone 1 master 2 slave architecture (equivalent to 3 redis)
※ System information: Centos 6.3 x86_64 4GB memory
※ In order to facilitate beginners to learn and understand redis, this article simplifies all steps
※ The full name of Redis is "remote dictionary server "Chinese remote dictionary service
※ Redis belongs to the key-value type in the NoSql database
※ The sentinel mechanism of Redis is supported by redis2.8, and the cluster mode is supported by redis3.0.
※ Redis's sentinel system is used to manage multiple redis servers. The system mainly performs three tasks: monitoring, reminders, and automatic failover.
※ The master of Redis is in read-write mode, and the slave is in read-only mode;
※ For more parameter introduction of Redis, please refer to other related documents of mine

★ Related articles
Redis operation and maintenance articles + installation single instance
Redis HA articles + master-slave construction
Redis HA articles + sentinel construction
Redis HA articles + cluster construction

★ Configure master-slave
※ Copy the configuration file
cp /soft/redis-4.0.2/redis.conf /soft/redis-4.0.2/redis_6380.conf
cp /soft/redis-4.0.2/redis.conf /soft/redis -4.0.2/redis_6381.conf
cp /soft/redis-4.0.2/redis.conf /soft/redis-4.0.2/redis_6382.conf

※ Modify the configuration file (master)
# Related file location: /soft/redis-4.0.2/redis_6380.conf
# Modify the master-slave port: port
sed -i 's/^port 6379/port 6380/g' /soft/redis -4.0.2/redis_6380.conf
cat /soft/redis-4.0.2/redis_6380.conf |grep "^port 63"
# Modify the master-slave process: pidfile
sed -i 's/redis_6379.pid/redis_6380.pid/g ' /soft/redis-4.0.2/redis_6380.conf
cat /soft/redis-4.0.2/redis_6380.conf |grep "redis_6380.pid"

※ Modify the configuration file (from A)
# Related file location: /soft/redis-4.0.2/redis_6381.conf
# Modify the master-slave port: port
sed -i 's/^port 6379/port 6381/g' /soft/ redis-4.0.2/redis_6381.conf
cat /soft/redis-4.0.2/redis_6381.conf |grep "^port 63"
# Modify the master-slave process: pidfile
sed -i 's/redis_6379.pid/redis_6381.pid/ g' /soft/redis-4.0.2/redis_6381.conf
cat /soft/redis-4.0.2/redis_6381.conf |grep "redis_6381.pid"
# Modify slaveof to point to master
echo "slaveof 127.0.0.1 6380" > > /soft/redis-4.0.2/redis_6381.conf
cat /soft/redis-4.0.2/redis_6381.conf |grep "^slaveof"

※ Modify the configuration file (from B)
# Related file location: /soft/redis-4.0.2/redis_6382.conf
# Modify the master-slave port: port
sed -i 's/^port 6379/port 6382/g' /soft/ redis-4.0.2/redis_6382.conf
cat /soft/redis-4.0.2/redis_6382.conf |grep "^port 63"
# Modify the master-slave process: pidfile
sed -i 's/redis_6379.pid/redis_6382.pid/ g' /soft/redis-4.0.2/redis_6382.conf
cat /soft/redis-4.0.2/redis_6382.conf |grep "redis_6382.pid"
# Modify slaveof to point to master
echo "slaveof 127.0.0.1 6380" > > /soft/redis-4.0.2/redis_6382.conf
cat /soft/redis-4.0.2/redis_6382.conf |grep "^slaveof"


★ Start master-slave
# Need to open 3 sessions, respectively execute the suspension
/soft/redis-4.0.2/src/redis-server /soft/redis-4.0.2/redis_6380.conf
/soft/redis-4.0.2/ src/redis-server /soft/redis-4.0.2/redis_6381.conf
/soft/redis-4.0.2/src/redis-server /soft/redis-4.0.2/redis_6382.conf


★ View status
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6381
echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6382


★ Test data transmission
echo "set zzt_01 'hello zzt'" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2/src/redis- cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6381
echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6382
echo "del zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "get zzt_01" | /soft/redis-4.0.2/src/redis-cli -p 6380


★ Test the automatic restart of the slave node
In this mode, the slave node will automatically join the master-slave after restarting, and will automatically synchronize the data of the master database


★ Test the automatic restart of the master node.
The slave nodes will wait for the restart of the master node, and will automatically join after the master node starts up.
This mode needs to be manually switched between master and slave, so it is generally recommended to use the sentinel mode of automatic management.


★ Manual master-slave switch
# Current status slave
    : 6380 slave
    : 6381
    master: 6382
# Target status
    master:
    6380 slave: 6381
    downtime: 6382
# Stop the master library, set up a new master, and redirect the slave library
echo "shutdown" | /soft/ redis-4.0.2/src/redis-cli -p 6382
echo "slaveof no one" | /soft/redis-4.0.2/src/redis-cli -p 6380
echo "slaveof 127.0.0.1 6380" | /soft/ redis-4.0.2/src/redis-cli -p 6381


★ case

[root@main src]# echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6380
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=28,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=28,lag=1
master_replid:348f1968b30ca05ef20ae66920fd9c92679af2bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

[root@main src]# echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6381
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:348f1968b30ca05ef20ae66920fd9c92679af2bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

[root@main src]# echo "info replication" | /soft/redis-4.0.2/src/redis-cli -p 6382
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:348f1968b30ca05ef20ae66920fd9c92679af2bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
[root@main src]# 


※ If you think the article is well written, don't forget to give the author a thumbs up at the end of the article~

over

Guess you like

Origin blog.csdn.net/zzt_2009/article/details/114395196