Distributed Construction of redis cluster Single Machine Pseudo-3 Master-3 Slave-3 Sentinel Cluster

Posted by dtyson2000 on Mon, 12 Aug 2019 12:20:54 +0200

Recently, the company introduced a micro-service framework, the previous redis 60G storage has been unable to meet the current 260G business needs, after some consideration to build the cluster. In order to facilitate my demonstration with a server, production environment is not recommended to do so (useless), just to record the process, as for the fine configuration needs. Self-study in production

Demonstration environment

[root@localhost ~]# cat /etc/redhat-release   
CentOS Linux release 7.4.1708 (Core)  

redis service deployment process

  • I downloaded the redis-4.0.14.tar.gz package.
  • Decompression installation
    tar -zxvf redis-4.0.14.tar.gz
    make install
  • Start up service
    ./redis-server
  • Verify, start client testing
    ./redis-cli  
    127.0.0.1:6379> set ab b  
    OK  
    127.0.0.1:6379> get ab  
    "b"  
    127.0.0.1:6379> keys *  
    "ab"  
    "foo"  
  • Close redis service
    ./redis-cli shutdown
  • Set redis background boot, in / usr/local/bin directory
    cp /usr/local/redis/redis-4.0.8/redis.conf ./
    Modify the redis.conf configuration file, find the line daemonize no, and save no to yes
    Then start redis with the configuration file,. / redis-server redis.conf
  • After modifying redis.conf configuration in / usr/local/bin directory (mainly setting background boot, bind, password, protection mode, etc.)
    mkdir /etc/redis
    cp /usr/local/bin/redis.conf /etc/redis/6379.conf
  • Copy the startup script from redis to the / etc/init.d directory and name it redisd (usually denoted as a background self-startup service at the end of d)
    cp /usr/local/redis/redis-4.0.8/utils/redis_init_script /etc/init.d/redisd
  • Set to boot self-starting chkconfig redisd on
    If the error redisd does not support chkconfig, add the following fragment in / etc/init.d/redisd to set the boot-up self-start again
    #!/bin/sh  
    #chkconfig:   2345 90 10  
    #description:  Redis is a persistent key-value database
  • Once chkconfig redisd on succeeds, you can start redis with a startup script

    Construction of Pseudo-Distributed Cluster

    Building a minimal cluster requires six machines (at least three master nodes are determined by the node voting mechanism, and at least one slave node is required for each master node to be highly available, so a total of six nodes are required. All six nodes are located on a single computer, and they are separated by port numbers. Realize pseudo-distributed.)

  • Create the folder redisCluster under / usr/local / directory and create six folders under redisCluster directory
    mkdir /usr/local/redisCluster
    mkdir /usr/local/redisCluster/redis0{1,2,3,4,5,6}
  • Copy all files in the / usr/local/bin directory that can normally start redis on a stand-alone single instance into the six folders created above
    cp /usr/local/bin/* /usr/local/redisCluster/redis01/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis02/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis03/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis04/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis05/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis06/
  • Then change redis.conf in each instance (modify one of them here, then copy the other five and modify the next port number)
    vi /usr/local/redisCluster/redis01/redis.conf  
    First place: port 6379 -> value changed to 7001  
    Number 2: daemonize no --> value changed to yes  
    Pidfile/var/run/redis_6379.pid ---> changed to. / redis_7001.pid  
    Fourth place: log file "---> value changed to". / logs/redis_7001.log"  
    Fifth place: bind 127.0.0.1 -> value change cost machine IP (as I'm here 192.168.25.129)  
    Number 6: Protect-mode yes ---> value changed to no  
    Number 7: # cluster-enabled yes --> Remove comments  
    Number 8: # cluster-config-file nodes-6379.conf --> Remove comments and change numbers to corresponding port numbers, such as 7001 here  
    Number 9:  cluster-node-timeout 15000   
    Number 10: Appndonly no --> value changed to yes
  • Rename the redis.conf file in the / usr/local/redisCluster/redis01/directory saved by the above edit number to 7001.conf, and then copy five copies under the redis02, redis03, redis04, redis05, redis06 folders respectively (you can delete the redis.conf file in the original folder), and then conf the text under each folder. Rename it to the corresponding port number. conf, then edit the contents, and save all the changes that were previously changed to 7001 to the corresponding port number.
  • Copy the ruby script to redisCluster directory
    cp /usr/local/redis/redis-4.0.8/src/redis-trib.rb /usr/local/redisCluster/
  • Create redis cluster start and stop scripts and ruby create cluster scripts and authorize
    touch /usr/local/redisCluster/startCluster.sh  
    touch /usr/local/redisCluster/shutdownCluster.sh  
    touch /usr/local/redisCluster/createCluster.sh  
    chmod 777 /usr/local/redisCluster/*.sh

    startcluster.sh reads as follows:

    /usr/local/rediscluster/redis01/redis-server /usr/local/rediscluster/redis01/7001.conf  
    /usr/local/rediscluster/redis02/redis-server /usr/local/rediscluster/redis02/7002.conf  
    /usr/local/rediscluster/redis03/redis-server /usr/local/rediscluster/redis03/7003.conf  
    /usr/local/rediscluster/redis04/redis-server /usr/local/rediscluster/redis04/7004.conf  
    /usr/local/rediscluster/redis05/redis-server /usr/local/rediscluster/redis05/7005.conf  
    /usr/local/rediscluster/redis06/redis-server /usr/local/rediscluster/redis06/7006.conf 

    shutdowncluster.sh reads as follows:

    /usr/local/rediscluster/redis01/redis-cli -c -h 10.11.1.103 -p 7001 shutdown  
    /usr/local/rediscluster/redis02/redis-cli -c -h 10.11.1.103 -p 7002 shutdown  
    /usr/local/rediscluster/redis03/redis-cli -c -h 10.11.1.103 -p 7003 shutdown  
    /usr/local/rediscluster/redis04/redis-cli -c -h 10.11.1.103 -p 7004 shutdown  
    /usr/local/rediscluster/redis05/redis-cli -c -h 10.11.1.103 -p 7005 shutdown  
    /usr/local/rediscluster/redis06/redis-cli -c -h 10.11.1.103 -p 7006 shutdown 

    createcluster.sh reads as follows:
    ruby redis-trib.rb create --replicas 1 10.11.1.103:7001 10.11.1.103:7002 10.11.1.103:7003 10.11.1.103:7004 10.11.1.103:7005 10.11.1.103:7006

  • Start six instances with the script you just created, and create clusters with the ruby script
    cd /usr/local/redisCluster/  
    ./startCluster.sh  
    ./createcluster.sh(input yes)
  • Verify, view the process
    [root@localhost rediscluster]# ps -ef |  grep redis|grep -v grep  
    root     23716     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster]  
    root     23721     1  0 Aug08 ?        00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster]  
    root     23723     1  0 Aug08 ?        00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster]  
    root     23731     1  0 Aug08 ?        00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster]  
    root     23736     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster]  
    root     23741     1  0 Aug08 ?        00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster]  
  • The cluster was built successfully. Login to any node for client authentication (- c means login in a cluster manner), or use RedisDesktop Manager, the redis client tool, to visually manage the redis database.
    cd /usr/local/redisCluster/redis01/  
    ./redis-cli -h 10.11.1.103 -p 7001 -c  
    10.11.1.103:7001> set aaaa b  
    OK  
    10.11.1.103:7002> get aaaa  
    -> Redirected to slot [1953] located at 10.11.1.103:7001  
    "b"  
    10.11.1.103:7001> cluster info  
    cluster_state:ok  
    cluster_slots_assigned:16384  
    cluster_slots_ok:16384  
    cluster_slots_pfail:0  
    cluster_slots_fail:0  
    cluster_known_nodes:6  
    cluster_size:3  
    cluster_current_epoch:6  
    cluster_my_epoch:1  
    cluster_stats_messages_ping_sent:367555  
    cluster_stats_messages_pong_sent:342015  
    cluster_stats_messages_publish_sent:903835  
    cluster_stats_messages_sent:1613405  
    cluster_stats_messages_ping_received:342015  
    cluster_stats_messages_pong_received:367550  
    cluster_stats_messages_publish_received:1084495  
    cluster_stats_messages_received:1794060  
    10.11.1.103:7001>  
  • Sentinel deployment in redis01 02 03, where only the rest of redis01 can be deployed according to this self-configuration, it is important to note that the monitored name is not repeatable (has been marked)
    sentinel.conf is configured as follows:
    protected-mode no  
    port 27001   
    sentinel deny-scripts-reconfig yes  
    sentinel monitor **mymaster1** 10.11.1.103 7001 2  
    sentinel down-after-milliseconds **mymaster1** 10000  
    logfile "/var/log/sentinel1.log"  
    dir "/usr/local/rediscluster/redis01"  
    sentinel failover-timeout **mymaster1** 15000  
    sentinel config-epoch **mymaster1** 0  
    sentinel leader-epoch **mymaster1** 0  
    sentinel known-slave **mymaster1 10.11.1.103 7004**  
    sentinel current-epoch 0  
  • Create Sentinel Start and Close scripts under the rediscluster directory
    startsentinel.sh is as follows
    /usr/local/rediscluster/redis01/redis-sentinel /usr/local/rediscluster/redis01/sentinel.conf &  
    /usr/local/rediscluster/redis02/redis-sentinel /usr/local/rediscluster/redis02/sentinel.conf &  
    /usr/local/rediscluster/redis03/redis-sentinel /usr/local/rediscluster/redis03/sentinel.conf &  

    The stopsentinel.sh script reads as follows:

    kill -9 `ps -ef |  grep sentinel| grep -v grep|awk '{print $2}'`  
  • Look at the process again
    [root@localhost rediscluster]# ps -ef |  grep redis|grep -v grep  
    root     23716     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster]  
    root     23721     1  0 Aug08 ?        00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster]  
    root     23723     1  0 Aug08 ?        00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster]  
    root     23731     1  0 Aug08 ?        00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster]  
    root     23736     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster]  
    root     23741     1  0 Aug08 ?        00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster]  
    root     23924     1  0 Aug08 ?        00:09:55 /usr/local/rediscluster/redis01/redis-sentinel *:27001 [sentinel]  
    root     23925     1  0 Aug08 ?        00:09:38 /usr/local/rediscluster/redis02/redis-sentinel *:27002 [sentinel]  
    root     23926     1  0 Aug08 ?        00:10:22 /usr/local/rediscluster/redis03/redis-sentinel *:27003 [sentinel] 
  • This completes the building. Here is the software that needs to be installed on the Internet.
    yum -y install gcc                    ## The make install command requires this environment  
    yum -y intall ruby                    ## Install ruby (at this point the installation may cause a lower version)  
    curl -L get.rvm.io | bash -s stable   ## Installation of rvm  
    source /usr/local/rvm/scripts/rvm     ## Make the installed rvm take effect immediately  
    rvm install 2.4.1                 ## Install ruby advanced version through rvm  
    gem install redis                     ## Install redis interface through ruby toolkit gem  
  • It should be noted that after closing the redis cluster, an error will be reported when the cluster is started again by repeating the steps. At this time, the cluster can be started again step by step by deleting the files generated in each instance of redis.

Topics: Linux Redis Ruby Database yum