Redis three kinds of cluster model - master-slave mode

I. Introduction

  There are three Redis cluster model, the first one is the master-slave mode, the second "sentinel" mode, the third mode is a cluster Cluster, cluster model is only a third of the increase coming in a future version of Redis 3.x, we are speaking today about the Redis first cluster modes: master mode from the cluster.

Second, the configuration instructions
         to achieve the main principle of the copy (Master-Slave Replication): The following Master Slave starts and connects to the service node, it sends a SYNC command. Master Service Master node after receiving the synchronization command starts a background archiving process, while collecting all the received command is used to modify the data set, after the background process is finished, Master transferring the entire database file to a Slave, to complete a full synchronization . Slave node from the service after receiving the data file to save the database and loaded into memory. Thereafter, Master master node continues all modification commands have been collected, and sequentially transmits the new modification command to the Slaves, Slave will be performed in this data modification commands, so as to achieve a final data synchronization.
        If the link between the Master and Slave disconnection phenomenon occurs, it can automatically reconnect Slave Master, but after the connection is successful, a full synchronization will be performed automatically

Master-slave replication configurations:
       The first step: modification from the node configuration file: slaveof <masterip> <masterport>
       Step 2: If the password is set, it is necessary to set: masterauth <master-password>
       master slave configuration is very simple to copy, the main operation not need any changes from the configuration file node, the master node. We can use the info view to know the main character role service or from the service.

Version Features: Test version redis-5.0.5

    REPLICATION, from the master configuration mode. Note that previous versions of redis, configured as a slave of, now replaced REPLICATION.

  Master-slave mode, can be a tree, belonging to more than one service from the main service, and can also have the service from the service.

  Master-slave mode with separate read and write; high availability mode, the main service problems arise, can be switched from a service-oriented service via sentinel;

  You can not achieve the main service data persistence, for persistence work from services that reduce the burden on the main service and so on.

  • replicaof <masterip> <masterport>: ip and port configuration of the primary service. Once configured, this machine is a younger brother. The main service, to know who is his younger brother.
  • masterauth <master-password>: if a main service password authentication, where the need to configure the password from the service connecting the main service.
  • replica-read-only: The default is yes, the default configuration from the service as a read-only mode.

Third, the configuration of master and slave modes

1.redis master-slave

  1) Create a master-slave directory

  /usr/software/redis/redis-ms/

    mkdir 7001

    mkdir 7002

    mkdir 7003

  2) Copy from the main directory redis.conf

    cp /usr/software/redis/redis.conf    /usr/software/redis/redis-ms/7001

    cp/usr/software/redis/redis.conf     /usr/software/redis/redis-ms/7002

    cp /usr/software/redis/redis.conf    /usr/software/redis/redis-ms/7003

  3) Modify the main ./ 7001 /redis.conf

    vim ./7001/redis.conf

    Modify the value of the following key #

    bind 0.0.0.0 # ip can be connected to any

    protected-mode no # close protection, to allow non-local connection

    Port 7001 # port number

    daemonize yes # running in the background

    pidfile /var/run/redis_7001.pid# process guardian file is stored information related to the process ID of place

    dir / usr / software / redis / redis-ms / 7001 / data // # db directory location and other related

    logfile "/usr/software/redis/redis-ms/7001/log/redis.log"

    appendonly yes # Enable the log form

    requirepass XX # code

  4) modified from ./7002/redis.conf

    vim ./7002/redis.conf

    Modify the value of the following key #

    bind 0.0.0.0 # ip can be connected to any

    protected-mode no # close protection, to allow non-local connection

    port 7002 # port number

    daemonize yes # running in the background   

    pidfile /var/run/redis_7002.pid# process guardian file is stored information related to the process ID of place

    dir / usr / software / redis / redis-ms / 7002 / data // # db directory location and other related  

    logfile "/usr/software/redis/redis-ms/7002/log/redis.log"

    replicaof <masterip> <masterport> # main information

    masterauth <master-password> # main information

    appendonly yes # Enable the log form

    requirepass XX # code

  5) modified from ./ 7003 /redis.conf

    vim ./7003/redis.conf 

    Modify the value of the following key #

    bind 0.0.0.0 # ip can be connected to any

    protected-mode no # close protection, to allow non-local connection

    port 7003 # port number

    daemonize yes # running in the background

    pidfile /var/run/redis_7003.pid# process guardian file is stored information related to the process ID of place

    dir / usr / software / redis / redis-ms / 7003 / data // # db directory location and other related    

    logfile "/usr/software/redis/redis-ms/7003/log/redis.log"

    replicaof <masterip> <masterport> # main information

    masterauth <master-password> # main information

    appendonly yes # Enable the log form

    requirepass XX # code

 2. Start test

  1) Start

    redis-server /usr/software/redis/redis-ms/7001/redis.conf

    redis-server /usr/software/redis/redis-ms/7002/redis.conf

    redis-server /usr/software/redis/redis-ms/7003/redis.conf

    ps -ef | grep Redis

    

    # View from the main building if successful

    /redis-cli -p 7001 -a password

    info

    Figure:

    

    

  2) test

    redis-cli -p 7001 -a ww

    

    redis-cli -p 7002 -a ww

    

    --raw Chinese display

    主7001设置name值,可以在从7002、7003取到,但是从不能设置值。

四、主从模式的优缺点
          1、Redis的Replication的特点和优点:
                  1】、同一个Master可以同步多个Slaves。
                  2】、Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。因此我们可以将Redis的Replication架构视为图结构。
                  3】、Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求。
                  4】、Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据。
                  5】、为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成。即便如此,系统的伸缩性还是得到了

        很大的提高。
                  6】、Master可以将数据保存操作交给Slaves完成,从而避免了在Master中要有独立的进程来完成此操作。
                  7】、支持主从复制,主机会自动将数据同步到从机,可以进行读写分离。
         2、Redis的Replication的缺点:
                1】、Redis不具备自动容错和恢复功能,主机从机的宕机都会导致前端部分读写请求失败,需要等待机器重启或者手动切换前端的IP才能恢复。
                2】、主机宕机,宕机前有部分数据未能及时同步到从机,切换IP后还会引入数据不一致的问题,降低了系统的可用性。
                3】、Redis的主从复制采用全量复制,复制过程中主机会fork出一个子进程对内存做一份快照,并将子进程的内存快照保存为文件发送给从机,

      这一过程需要确保主机有足够多的空余内存。若快照文件较大,对集群的服务能力会产生较大的影响,而且复制过程是在从机新加入集群或者从机和

      主机网络断开重连时都会进行,也就是网络波动都会造成主机和从机间的一次全量的数据复制,这对实际的系统运营造成了不小的麻烦。
                4】、Redis较难支持在线扩容,在集群容量达到上限时在线扩容会变得很复杂。为避免这一问题,运维人员在系统上线时必须确保有足够的空间,

      这对资源造成了很大的浪费。

五、结束

         其实redis的主从模式很简单,在实际的生产环境中是很少使用的,我也不建议在实际的生产环境中使用主从模式来提供系统的高可用性,之所以不建议使用都是由它的缺点造成的,在数据量非常大的情况,或者对系统的高可用性要求很高的情况下,主从模式也是不稳定的。虽然这个模式很简单,但是这个模式是其他模式的基础,所以必须深刻的理解,对其他模式的学习才会有帮助作用。

 

Guess you like

Origin www.cnblogs.com/pinghengxing/p/11139997.html