美文网首页
Docker安装Redis集群(3主3从原生安装)

Docker安装Redis集群(3主3从原生安装)

作者: 宁静的猫 | 来源:发表于2019-11-25 00:30 被阅读0次

    1、配置开启Redis

    port 6391
    daemonize no
    dir "/data"
    logfile "redis.log"
    dbfilename "dump.rdb"
    cluster-enabled yes
    cluster-config-file nodes-6391.conf
    cluster-require-full-coverage no
    

    2、开启6个节点

    #redis集群总线端口为redis客户端端口加上10000
    docker run -d -p 6391:6391 -p 16391:16391 -v /root/redis/c1/redis.conf:/etc/redis/redis.conf -v /root/redis/c1/data:/data:rw --name redis-6391 4760dc956b2d redis-server /etc/redis/redis.conf
    
    docker run -d -p 6392:6392 -p 16392:16392 -v /root/redis/c2/redis.conf:/etc/redis/redis.conf -v /root/redis/c2/data:/data:rw --name redis-6392 4760dc956b2d redis-server /etc/redis/redis.conf
    
    docker run -d -p 6393:6393 -p 16393:16393 -v /root/redis/c3/redis.conf:/etc/redis/redis.conf -v /root/redis/c3/data:/data:rw --name redis-6393 4760dc956b2d redis-server /etc/redis/redis.conf
    
    docker run -d -p 6394:6394 -p 16394:16394 -v /root/redis/c4/redis.conf:/etc/redis/redis.conf -v /root/redis/c4/data:/data:rw --name redis-6394 4760dc956b2d redis-server /etc/redis/redis.conf
    
    docker run -d -p 6395:6395 -p 16395:16395 -v /root/redis/c5/redis.conf:/etc/redis/redis.conf -v /root/redis/c5/data:/data:rw --name redis-6395 4760dc956b2d redis-server /etc/redis/redis.conf
    
    docker run -d -p 6396:6396 -p 16396:16396 -v /root/redis/c6/redis.conf:/etc/redis/redis.conf -v /root/redis/c6/data:/data:rw --name redis-6396 4760dc956b2d redis-server /etc/redis/redis.conf
    

    此时连接节点,set hello world还是会报错,因为集群并没有启动


    节点.png

    3、meet操作

    选择一个节点,对其余节点执行meet操作

    cluster meet <ip> 6392
    cluster meet <ip> 6393
    cluster meet <ip> 6394
    cluster meet <ip> 6395
    cluster meet <ip> 6396
    

    4、分配槽

    a、编写脚本:
    addslots.sh

    start=$1
    end=$2
    port=$3
    for slot in `seq ${start} ${end}`
    do
        echo "slot:${slot}"
        redis-cli -p ${port} cluster addslots ${slot}
    done
    

    让每一个docker容器运行脚本:

    #6391
    docker exec -d redis-6391 /bin/sh /data/addslots.sh 0 5461 6391
    #6392
    docker exec -d redis-6392 /bin/sh /data/addslots.sh 5462 10922 6392
    #6393
    docker exec -d redis-6393 /bin/sh /data/addslots.sh 10923 16383 6393
    

    5、设置组从

    #6394复制6391
    redis-cli -p 6394 cluster replicate <6391的nodeid>
    redis-cli -p 6395 cluster replicate <6392的nodeid>
    redis-cli -p 6396 cluster replicate <6393的nodeid>
    

    设置主从完成,一个3主3从的Redis集群就搭建完成了。

    相关文章

      网友评论

          本文标题:Docker安装Redis集群(3主3从原生安装)

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