Redis database master-slave replication

Redis database master-slave replication

I. Overview of replication from the master;
two, synchronization Detailed;
three cases: the configuration from the master copy;

First, the master-slave replication overview:

Summary: In order to read and write share the pressure, Redis supports master-slave replication, Redis master-slave configuration may be employed in a multi-master or from the cascade structure, Redis master can be divided into the total amount of the total amount of incremental synchronization and synchronization according to whether the replication Yes. The figure below shows the cascade structure.
Redis database master-slave replication

Second, synchronization Detailed:

Sync Type: Full synchronization, incremental synchronization
1. The full amount of synchronization:
  Redis full amount generally occurs in Slave replication initialization phase, when the Slave need all the data on a separate copy of the Master. Specific steps are as follows: 
  1) from a server connected to the primary server, transmits SYNC command (from the server initial synchronization with the primary server does not affect the request of the master server receives a client); 
  2) the primary server receives the SYNC named started BGSAVE command RDB generated file and records all write command buffer after execution; 
  3) master BGSAVE executed, transmitted to all snapshot files from the server, and to continue to record the write command is executed during transmission; 
  4) received from the server ; discard all old data snapshot file, loading the received snapshot 
  starts transmission after 5) has been sent from the master server snapshot buffer write command; 
  6) from the server to complete the loading of the snapshot command to start receiving the request and perform buffer write command from the host server; 
Redis database master-slave replication

After the completion of the above steps to complete the operations of all the initialization data from the server, you may receive a read request from the user from the server at this time.
2. Incremental Synchronization:
  Redis incremental replication is either write operation begins working properly initialized after the Slave primary server synchronized to the process from the server. Incremental replication process is mainly the primary server for each command will execute a write, receive and execute the write command received from the server sent from the server to the same write command.

Synchronization strategy:
master and slave when just connected, full amount of synchronization;
after the end of full synchronization, incremental synchronization. Of course, if necessary, slave at any time to initiate the full amount of synchronization. redis strategy is, in any case, will first try to incremental synchronization, such as unsuccessful, requiring full amount of synchronous slave.
Note:
If multiple Slave disconnected, need to restart the time, because as long as the Slave start, it will send a request and host synchronization sync the whole amount, when the plurality of simultaneously occurring, may lead to Master IO surge cause downtime.
Advice Open persistent feature master primary server to avoid the restart after the master, data can not be recovered;

Third, the case: configure the master-slave replication:

Redis database master-slave replication

Experimental procedure:
Ø Install and configure the master role redis services;
Ø installed and configured slave roles redis service (bis instance);
Ø profile master role;
Ø slave role configuration file;
Ø verification from the master copy;

 Install and configure the master role redis service;

[root@master ~]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
[root@master ~]# tar zxvf redis-4.0.9.tar.gz 
[root@master ~]# cd redis-4.0.9
[root@master  redis-4.0.9]# make 
[root@master  redis-4.0.9]# echo $?
[root@master redis-4.0.9]# cd
[root@master ~]# mkdir -p /usr/local/redis
[root@master ~]# cp /root/redis-4.0.9/src/redis-server /usr/local/redis/        ##服务端程序
[root@master ~]# cp /root/redis-4.0.9/src/redis-cli /usr/local/redis/           ##客户端程序
[root@master ~]# cp /root/redis-4.0.9/redis.conf /usr/local/redis/              ##主配置文件
[root@master ~]# ls /usr/local/redis/
redis-cli  redis.conf  redis-server
[root@master ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.101/g' /usr/local/redis/redis.conf   
[root@master ~]# sed -i '/protected-mode/s/yes/no/g' /usr/local/redis/redis.conf                ##关闭redis的保护模式
[root@master ~]# sed -i '/daemonize/s/no/yes/g' /usr/local/redis/redis.conf                 ##开启redis的后台守护进程模式
[root@master ~]# sed -i '/requirepass/s/foobared/123123/g' /usr/local/redis/redis.conf      ##设置redis的密码为123123
[root@master ~]# sed -i '/requirepass 123123/s/^#//g' /usr/local/redis/redis.conf           ##开启redis的密码
[root@master ~]# ln -s /usr/local/redis/redis-cli /usr/local/bin/redis
[root@master ~]# cat <<END >>/etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 80 90
# description: Start and Stop redis
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
AUTH="123123"
LISTEN_IP=\$(netstat -utpln |grep redis-server |awk '{print \$4}'|awk -F':' '{print \$1}')

case "\$1" in
    start)
        if [ -f \$PIDFILE ]
        then
                echo "\$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                \$EXEC \$CONF
        fi
        if [ "\$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running"
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping ..."
                \$REDIS_CLI -h \$LISTEN_IP -p \$REDISPORT -a \$AUTH SHUTDOWN
                while [ -x \${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        \${0} stop
        \${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac
END
[root@master ~]# chmod 755 /etc/init.d/redis
[root@master ~]# chkconfig --add redis
[root@master ~]# /etc/init.d/redis start
Starting Redis server...
4390:C 04 May 02:16:45.232 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4390:C 04 May 02:16:45.232 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4390, just started
4390:C 04 May 02:16:45.232 # Configuration loaded
Redis is running...
[root@master ~]# netstat -utpln |grep redis
tcp        0      192.168.100.101:6379            0.0.0.0:*               LISTEN      4204/redis-server *
[root@master ~]# redis -h 192.168.100.101 -a 123123 -p 6379
192.168.100.101:6379> exit

安装并配置slave角色的redis服务(双实例);
[root@slave ~]# wget http://download.redis.io/releases/redis-4.0.9.tar.gz
[root@slave ~]# tar zxvf redis-4.0.9.tar.gz 
[root@slave ~]# cd redis-4.0.9
[root@slave redis-4.0.9]# make 
[root@slave  redis-4.0.9]# echo $?
[root@slave redis-4.0.9]# cd
[root@slave ~]# mkdir -p /usr/local/redis
[root@slave ~]# cp /root/redis-4.0.9/src/redis-server /usr/local/redis/     ##服务端程序
[root@slave ~]# cp /root/redis-4.0.9/src/redis-cli /usr/local/redis/            ##客户端程序
[root@slave ~]# cp /root/redis-4.0.9/redis.conf /usr/local/redis/               ##主配置文件
[root@slave ~]# ls /usr/local/redis/
redis-cli  redis.conf  redis-server
[root@slave ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.100.102/g' /usr/local/redis/redis.conf    
[root@slave ~]# sed -i '/protected-mode/s/yes/no/g' /usr/local/redis/redis.conf             ##关闭redis的保护模式
[root@slave ~]# sed -i '/daemonize/s/no/yes/g' /usr/local/redis/redis.conf                  ##开启redis的后台守护进程模式
[root@slave ~]# sed -i '/requirepass/s/foobared/123123/g' /usr/local/redis/redis.conf       ##设置redis的密码为123123
[root@slave ~]# sed -i '/requirepass 123123/s/^#//g' /usr/local/redis/redis.conf            ##开启redis的密码
[root@slave ~]# cp /usr/local/redis/redis.conf /usr/local/redis/redis01.conf
[root@slave ~]# sed -i '92s/6379/6380/g' /usr/local/redis/redis01.conf      ##更改监听端口
[root@slave ~]# sed -i '158s/6379/6380/g' /usr/local/redis/redis01.conf     ##更改PID文件
[root@slave ~]# sed -i '171s/^\(.\).\{9\}/logfile "\/usr\/local\/redis\/redis01.log"/g' /usr/local/redis/redis01.conf               ##更改日志文件位置,9代表旧内容的字节数
[root@slave ~]# ln -s /usr/local/redis/redis-cli /usr/local/bin/redis
[root@slave ~]# cat <<END >>/etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 80 90
# description: Start and Stop redis
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
AUTH="123123"
LISTEN_IP=\$(netstat -utpln |grep redis-server |awk '{print \$4}'|awk -F':' '{print \$1}' |uniq)

case "\$1" in
    start)
        if [ -f \$PIDFILE ]
        then
                echo "\$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                \$EXEC \$CONF
        fi
        if [ "\$?"="0" ]
        then
              echo "Redis is running..."
        fi
        ;;
    stop)
        if [ ! -f \$PIDFILE ]
        then
                echo "\$PIDFILE does not exist, process is not running"
        else
                PID=\$(cat \$PIDFILE)
                echo "Stopping ..."
                \$REDIS_CLI -h \$LISTEN_IP -p \$REDISPORT -a \$AUTH SHUTDOWN
                while [ -x \${PIDFILE} ]
               do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
   restart|force-reload)
        \${0} stop
        \${0} start
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
        exit 1
esac
END
[root@slave ~]# chmod 755 /etc/init.d/redis
[root@slave ~]# chkconfig --add redis
[root@slave ~]# cp /etc/init.d/redis /etc/init.d/redis01
[root@slave ~]# sed -i 's/6379/6380/g' /etc/init.d/redis01
[root@slave ~]# sed -i '/CONF=/s/redis.conf/redis01.conf/g' /etc/init.d/redis01
[root@slave ~]# /etc/init.d/redis start
Starting Redis server...
4390:C 04 May 02:16:45.232 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4390:C 04 May 02:16:45.232 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4390, just started
4390:C 04 May 02:16:45.232 # Configuration loaded
Redis is running...
[root@slave ~]# /etc/init.d/redis01 start
Starting Redis server...
4390:C 04 May 02:16:45.232 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4390:C 04 May 02:16:45.232 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4390, just started
4390:C 04 May 02:16:45.232 # Configuration loaded
Redis is running...
[root@slave ~]# netstat -utpln |grep redis
tcp        0      0 192.168.100.102:6379    0.0.0.0:*               LISTEN      11864/redis-server  
tcp        0      0 192.168.100.102:6380    0.0.0.0:*               LISTEN      11877/redis-server
[root@ slave ~]# redis -h 192.168.100.102 -a 123123 -p 6379
192.168.100.102:6379> exit
[root@ slave ~]# redis -h 192.168.100.102 -a 123123 -p 6380
192.168.100.102:6380> exit

配置master角色的文件;
[root@master ~]# sed -i '450s/^\(.\).\{22\}/min-slaves-to-write 2/g' /usr/local/redis/redis.conf
##设置slave节点的数量,如果slave节点数量少于此值,那么master节点将停止客户端的一切写请求
[root@master ~]# sed -n '451s/^\(.\).\{22\}/min-slaves-max-lag 10/g' /usr/local/redis/redis.conf
##master与slave之间同步数据的超时时间,若超过此时间,master节点将停止客户端的一切写操作
[root@master ~]# /etc/init.d/redis restart
Stopping ...
Redis stopped
Starting Redis server...
1638:C 15 May 16:32:08.301 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1638:C 15 May 16:32:08.301 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1638, just started
1638:C 15 May 16:32:08.301 # Configuration loaded
Redis is running...

配置slave角色的文件;
[root@slave ~]# sed -i '281s/^\(.\).\{32\}/slaveof 192.168.100.101 6379/g' /usr/local/redis/redis.conf
##指定master的ip地址以及端口
[root@slave ~]# sed -i '288s/^\(.\).\{29\}/masterauth 123123/g' /usr/local/redis/redis.conf
##指定master的连接密码
[root@slave ~]# /etc/init.d/redis restart
/var/run/redis_6379.pid does not exist, process is not running
Starting Redis server...
4387:C 18 May 03:24:00.027 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4387:C 18 May 03:24:00.027 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4387, just started
4387:C 18 May 03:24:00.027 # Configuration loaded
Redis is running...
[root@slave ~]# sed -i '281s/^\(.\).\{32\}/slaveof 192.168.100.101 6379/g' /usr/local/redis/redis01.conf
[root@slave ~]# sed -i '288s/^\(.\).\{29\}/masterauth 123123/g' /usr/local/redis/redis01.conf
[root@slave ~]# /etc/init.d/redis01 restart
Stopping ...
Redis stopped
Starting Redis server...
Redis is running...

验证主从复制;
[root@master ~]# redis -h 192.168.100.101 -a 123123 -p 6379         ##在master节点上创建键值对
192.168.100.101:6379> set name xiaoming
OK
192.168.100.101:6379> keys *
1) "name"
192.168.100.101:6379> get name
"xiaoming"
192.168.100.101:6379> info replication                      ##查看复制信息
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.100.102,port=6380,state=online,offset=522,lag=1
slave1:ip=192.168.100.102,port=6379,state=online,offset=522,lag=1
master_replid:46ec2c7e971d337d060d183d3a0c1313c5dd1683
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:522
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:522
192.168.100.101:6379> exit

[root@slave ~]# redis -h 192.168.100.102 -p 6379 -a 123123          ##登录slave节点验证键值同步情况,并测试无法写入
192.168.100.102:6379> keys *
1) "name"
192.168.100.102:6379> get name
"xiaoming"
192.168.100.102:6380> set name1 xiaohong
(error) READONLY You can't write against a read only slave.
192.168.100.102:6379> exit
[root@slave ~]# redis -h 192.168.100.102 -p 6380 -a 123123
192.168.100.102:6380> keys *
1) "name"
192.168.100.102:6380> get name
"xiaoming"
192.168.100.102:6380> exit

[root@master ~]# reboot                         ##重启master节点,验证redis默认的RDB持久化
[root@master ~]# /etc/init.d/redis restart
[root@master ~]# redis -h 192.168.100.101 -a 123123 -p 6379
192.168.100.101:6379> keys *
1) "name"
192.168.100.101:6379> exit

[root@slave ~]# redis -h 192.168.100.102 -p 6380 -a 123123 
192.168.100.102:6380> keys *
1) "name"
192.168.100.102:6380> get name
"xiaoming"
192.168.100.102:6380> exit

Guess you like

Origin blog.51cto.com/13528668/2434432