美文网首页
Redis-6.0.1 集群

Redis-6.0.1 集群

作者: Odven | 来源:发表于2020-05-11 13:20 被阅读0次

    实验环境单机版启动6个reids实例

    端口号分别为 6381 6382 6383 6384 6385 6386

    1) 下载redis-6.0.1.tar.gz

    wget http://download.redis.io/releases/redis-6.0.1.tar.gz
    

    2) 创建集群目录和解压编译

    # 2.1 创建集群目录
    mkdir /mnt/redis-cluster  -p
    
    #2.2 解压和建立软连接 编译
    tar -xf redis-6.0.1.tar.gz -C /mnt/redis-cluster/
    cd /mnt/redis-cluster/
    ln -s redis-6.0.1/ redis
    cd redis
    make && make install
    
    # 有时候安装了gcc gcc-c++ autoconf直接make还是会报错,可能是gcc的版本太低
    # 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5
    gcc -v
    # 升级gcc到5.3及以上,如下:
    升级到gcc 9.3:
    yum -y install centos-release-scl
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
    # 临时使用高版本的gcc退出shell或重启就会恢复原系统gcc版本
    scl enable devtoolset-9 bash
    
    # 如果想一直生效请 
    echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
    source /etc/profile
    

    3) 部署redis的配置文件并启动

    cd /mnt/redis-cluster/
    
    # 部署redis脚本
    cat > deploy-redis-conf.sh << EOF
    #!/bin/bash
     
    # conf: 放配置文件
    # log: 放日志文件
    # pid: 放pid文件
    # data: 数据目录
    
    for i in {1..6}
    do
    mkdir /mnt/redis-cluster/638\${i}/{conf,log,pid,data} -p
    
    echo """
    bind 0.0.0.0
    port 6379
    daemonize yes
    logfile /mnt/redis-cluster/6379/log/redis_6379.log
    pidfile /mnt/redis-cluster/6379/pid/redis_6379.pid
    loglevel notice
    maxmemory 268435456
    
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dir /mnt/redis-cluster/6379/data
    dbfilename dump-6379.rdb
    
    
    cluster-enabled  yes
    cluster-config-file  nodes.conf
    cluster-node-timeout  5000  
    
    """ > /mnt/redis-cluster/638\${i}/conf/redis.conf
    
    sed -i "s/6379/638\${i}/g"  /mnt/redis-cluster/638\${i}/conf/redis.conf
    
    done
    EOF
    
    # redis集群启动脚本
    cat > start-redis.sh << EOF 
    #!/bin/bash
    
    for i in {1..6}
    do
        redis-server /mnt/redis-cluster/638\${i}/conf/redis.conf
        sleep 0.05
    done
    EOF
    
    # redis集群停止脚本
    cat > stop-redis.sh << EOF
    #!/bin/bash
    
    for i in {1..6}
    do
        redis-cli -p 638\${i} shutdown
        sleep 0.05
    done
    EOF
    
    
    chmod +x deploy-redis-conf.sh start-redis.sh stop-redis.sh
    ./deploy-redis-conf.sh 
    ./start-redis.sh
    
    # 查看redis启动情况
    ps aux | grep redis
    
    root       6877  0.0  0.2 165328  7892 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6381 [cluster]
    root       6884  0.0  0.2 165328  7892 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6382 [cluster]
    root       6891  0.0  0.2 165328  7896 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6383 [cluster]
    root       6898  0.2  0.2 165328  7892 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6384 [cluster]
    root       6905  0.0  0.2 165328  7900 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6385 [cluster]
    root       6912  0.0  0.2 165328  7896 ?        Ssl  00:13   0:00 redis-server 0.0.0.0:6386 [cluster]
    root       6919  0.0  0.0 112716   968 pts/0    R+   00:13   0:00 grep --color=auto redis
    

    4) 启动redis集群

    # 启动集群
    redis-cli --cluster create  127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 127.0.0.1:6386 --cluster-replicas 1
    
    >>> Performing hash slots allocation on 6 nodes...
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    Adding replica 127.0.0.1:6385 to 127.0.0.1:6381
    Adding replica 127.0.0.1:6386 to 127.0.0.1:6382
    Adding replica 127.0.0.1:6384 to 127.0.0.1:6383
    ......
    Can I set the above configuration? (type 'yes' to accept): yes
    >>> Nodes configuration updated
    >>> Assign a different config epoch to each node
    >>> Sending CLUSTER MEET messages to join the cluster
    Waiting for the cluster to join
    ......
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    # 检查集群状态
    redis-cli --cluster check 127.0.0.1:6381
    
    127.0.0.1:6381 (32a23583...) -> 0 keys | 5461 slots | 1 slaves.
    127.0.0.1:6383 (27b01789...) -> 0 keys | 5461 slots | 1 slaves.
    127.0.0.1:6382 (dd45dd58...) -> 0 keys | 5462 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    ......
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    5) 调整集群主从关系

    登录到要改变的从节点然后执行 CLUSTER REPLICATE 想要成为那个主节点的那个节点的id号
    如:你想让6384从节点 成为6383主节点的从
    redis-cli --cluster check 127.0.0.1:6381
    
    aaa.png
    # 登录6384从节点
    redis-cli -c -p 6384
    127.0.0.1:6384> CLUSTER REPLICATE 5ad2ad6130671b74af23fac2bd85ba82bb4c1619
    OK
    # 5ad2ad6130671b74af23fac2bd85ba82bb4c1619 为6383主节点的id
    

    6) 测试集群

    cat > for.sh << EOF
    #!/bin/bash
    
    for i in {1..10000}
    do
        redis-cli -c -p 6381  set key\${i} value\${i}
    done
    EOF
    
    chmod +x for.sh
    ./for.sh
    
    # 查看分配情况
    redis-cli --cluster info 127.0.0.1:6381
    
    127.0.0.1:6381 (35bf9c0e...) -> 3331 keys | 5461 slots | 1 slaves.
    127.0.0.1:6382 (a9945ee2...) -> 3341 keys | 5462 slots | 1 slaves.
    127.0.0.1:6383 (5ad2ad61...) -> 3328 keys | 5461 slots | 1 slaves.
    [OK] 10000 keys in 3 masters.
    0.61 keys per slot on average.
    
    redis-cli --cluster rebalance 127.0.0.1:6381
    
    >>> Performing Cluster Check (using node 127.0.0.1:6381)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    *** No rebalancing needed! All nodes are within the 2.00% threshold.
    

    7) 停止redis集群

    /mnt/redis-cluster/stop-redis.sh
    

    相关文章

      网友评论

          本文标题:Redis-6.0.1 集群

          本文链接:https://www.haomeiwen.com/subject/bnnynhtx.html