首页 > 数据库 >39_配置redis集群

39_配置redis集群

时间:2024-03-13 10:55:54浏览次数:38  
标签:39 -- redis 192.168 cluster 集群 6381 2.241

配置 redis 集群

  1. 三主三从集群设置
Master1(6381) Master2(6382) Master3(6383)
Slave1(6384)  Slave2(6385)  Slave3(6386)
  1. 拉取镜像启动容器
docker pull redis:6.0.8
docker run -d --name redis-node-1 --net host --privileged=true -v  /data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386

docker ps
    CONTAINER ID   IMAGE          NAMES
    ded0061df2ce   redis:6.0.8    redis-node-6
    402b0073a401   redis:6.0.8    redis-node-5
    78f3028f13be   redis:6.0.8    redis-node-4
    5d96881f585d   redis:6.0.8    redis-node-3
    d087d97a7507   redis:6.0.8    redis-node-2
    beb8f99d6645   redis:6.0.8    redis-node-1
  1. 主从配置
# --cluster-replicas 1 为每个master分配一个slave

docker exec -it redis-node-1 /bin/bash
redis-cli --cluster create 192.168.2.241:6381 192.168.2.241:6382 192.168.2.241:6383 192.168.2.241:6384 192.168.2.241:6385 192.168.2.241:6386 --cluster-replicas 1
  1. 查看集群
# 以 6381 为切入点进入
root@test:/data# redis-cli -p 6381(默认6379)
    127.0.0.1:6381> 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:314
        cluster_stats_messages_pong_sent:309
        cluster_stats_messages_sent:623
        cluster_stats_messages_ping_received:304
        cluster_stats_messages_pong_received:314
        cluster_stats_messages_meet_received:5
        cluster_stats_messages_received:623

    127.0.0.1:6381> CLUSTER NODES
        661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678706342394 3 connected 10923-16383
        c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,master - 0 1678706341000 1 connected 0-5460
        15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678706343396 3 connected
        140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678706342000 2 connected 5461-10922
        9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 slave c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 0 1678706341392 1 connected
        27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678706341000 2 connected
    127.0.0.1:6381>
  1. 数据读写存储
docker exec -it redis-node-1 /bin/bash
    redis-cli -p 6381 -c(优化路由, 以集群而不是单机模式)
        127.0.0.1:6381> KEYS *
        127.0.0.1:6381> FLUSHALL

        127.0.0.1:6381> set k1 v1
            -> Redirected to slot [12706] located at 192.168.2.241:6383
            OK

        192.168.2.241:6383> set k2 v2
            -> Redirected to slot [449] located at 192.168.2.241:6381
            OK

        192.168.2.241:6381> set k3 v3
            OK

        192.168.2.241:6381> set k4 v4
            -> Redirected to slot [8455] located at 192.168.2.241:6382
            OK

    # 查看集群信息
    redis-cli --cluster check 192.168.2.241:6381
        192.168.2.241:6381 (c0c7c1a9...) -> 2 keys | 5461 slots | 1 slaves.
        192.168.2.241:6383 (661ef7b3...) -> 1 keys | 5461 slots | 1 slaves.
        192.168.2.241:6382 (140d2119...) -> 1 keys | 5462 slots | 1 slaves.
        [OK] 4 keys in 3 masters.
        0.00 keys per slot on average.
        >>> Performing Cluster Check (using node 192.168.2.241:6381)
        M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
        slots:[0-5460] (5461 slots) master
        1 additional replica(s)
        M: 661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383
        slots:[10923-16383] (5461 slots) master
        1 additional replica(s)
        S: 15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386
        slots: (0 slots) slave
        replicates 661ef7b3f4730b71f2d76d87e6baad2229e01399
        M: 140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382
        slots:[5461-10922] (5462 slots) master
        1 additional replica(s)
        S: 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384
        slots: (0 slots) slave
        replicates c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f
        S: 27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385
        slots: (0 slots) slave
        replicates 140d21198470f2e066ac376f560c0641c98327ae
        [OK] All nodes agree about slots configuration.
        >>> Check for open slots...
        >>> Check slots coverage...
        [OK] All 16384 slots covered.

  1. 主从切换查看
# 停止一个主节点
 docker stop redis-node-1
 docker exec -it redis-node-2 /bin/bash
    redis-cli -p 6382 -c
        127.0.0.1:6382> CLUSTER NODES
            661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708057000 3 connected 10923-16383
            140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 myself,master - 0 1678708053000 2 connected 5461-10922
            9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708057686 7 connected 0-5460
            27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708056684 2 connected
            c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 master,fail - 1678707913388 1678707909000 1 disconnected
            15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708054681 3 connected

        192.168.2.241:6383> get k1
            "v1"

        192.168.2.241:6383> get k2
            -> Redirected to slot [449] located at 192.168.2.241:6384
            "v2"

        192.168.2.241:6384> get k3
            "v3"

        192.168.2.241:6384> get k4
            -> Redirected to slot [8455] located at 192.168.2.241:6382
            "v4"

# 启动停止的主节点
 docker start redis-node-1
 docker exec -it redis-node-1 /bin/bash
    redis-cli -p 6381 -c
        127.0.0.1:6381> CLUSTER NODES # 还是slave, 要想6381还是master, 可以停掉6384再启动
            27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708466000 2 connected
            661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708467908 3 connected 10923-16383
            c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,slave 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 0 1678708467000 7 connected
            140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678708464000 2 connected 5461-10922
            9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708465905 7 connected 0-5460
            15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708466906 3 connected


  1. 扩容到 4 主 4 从
docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
docker exec -it redis-node-7 /bin/bash
    # 添加一个master节点
    redis-cli --cluster add-node 192.168.2.241:6387 192.168.2.241:6381(相当于通知集群的老大6381)
    # 查看
    redis-cli --cluster check 192.168.2.241:6381
    # 重新分配槽位
    redis-cli --cluster reshard 192.168.2.241:6381
        ...How many slots do you want to move (from 1 to 16384)? 4096 (16384/Master的个数)
        ...What is the receiving node ID? f23674b12839887c47761c40017fd1e44a2a3c45 (6387的id)
        ...Source node #1: all
    # 查看
    redis-cli --cluster check 192.168.2.241:6381

    # 为什么6387是3个新的区间, 以前的还是连续?
    # 重新分配成本太高, 所以前3家各自匀出来一部分, 从6381/6382/6383三个旧节点分别匀出1364个坑位给新节点6387
    # M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
    # slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master

    # 添加从节点
    redis-cli --cluster add-node 192.168.2.241:6388 192.168.2.241:6387 --cluster-slave --cluster-master-id f23674b12839887c47761c40017fd1e44a2a3c45
    # 查看
    redis-cli --cluster check 192.168.2.241:6381
  1. 缩容 6387 6388 下线
# 移除节点
redis-cli --cluster del-node 192.168.2.241:6388 6811637e126da89ec173afe67d2b2f14b4a27b1e(6388的id)

# 将6387的槽号清空, 重新分配, 本例将清出来的槽号都给6381
redis-cli --cluster reshard 192.168.2.241:6381
    ...How many slots do you want to move (from 1 to 16384)? 4096
    ...What is the receiving node ID? c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f(6381的id)
    ...Source node #1: f23674b12839887c47761c40017fd1e44a2a3c45(6387的id, 告知删除哪个)
    ...Source node #2: done

# 查看
redis-cli --cluster check 192.168.2.241:6381
# M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
#    slots:[0-6826],[10923-12287] (8192 slots) master
#    1 additional replica(s)
...
# M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
#    slots: (0 slots) master

# 删除6387节点
redis-cli --cluster del-node 192.168.2.241:6387 f23674b12839887c47761c40017fd1e44a2a3c45

# 查看
redis-cli --cluster check 192.168.2.241:6381

标签:39,--,redis,192.168,cluster,集群,6381,2.241
From: https://www.cnblogs.com/ckh2023/p/18070147

相关文章

  • KeyError: 'Cache only has 0 layers, attempted to access layer with index 0'
    Traceback(mostrecentcalllast):File"/disk2/xiaoming/Github/ChatHxk/chinese_alpaca_2_7b_16k_hf/hxk_demo.py",line345,ingentaskret=self.mfunc(callback=_callback,**self.kwargs)File"/disk2/xiaoming/Github/ChatHxk/chinese_alpa......
  • CH395实现FTP_Client应用(上)
    测试环境:CH30v307RCT6+CH395Q,PC端工具:XLight(FTP服务器,语言为英文)1.主要代码说明(1)main.c(2)CH395FTP.c工程CH395FTP.c99行CH395_FTPInterCmd函数执行了四个主要的流程,包括登录、搜索目录、下载文件、上传文件。在该函数中可以修改想要访问的文件夹和文本文档名称,也可修改想......
  • CH395实现FTP_Client应用(下)
    CH395实现FTP_Client例程、抓包和打印log可以在下面的博客中下载CH395实现FTP_Client应用(上)-lqlq123-博客园(cnblogs.com) 本篇是对例程流程抓包和打印日志的解析1.串口打印日志(从上电到流程结束):1SourIP:192,168,3,30,//本地IP2......
  • 数据库基础--Redis知识体系(掌握Redis,看完这篇文章就够了!)
    1.Redis数据库Redis是一个开源的高性能键值存储数据库,类似字典。通常用作缓存、消息队列和数据存储等用途。mysql,mongodb都是以文件形式存储在磁盘上的,redis数据存在内存中,操作内存的速度远远高于磁盘,并且redis数据最终也可以存储在磁盘上。Redis支持多种数据结构,包括字符串......
  • 分布式与集群--一起学习吧之架构
    分布式和集群都是为了提高系统的性能和可靠性而采用的技术手段。它们在实际应用中各有优势,可以互相结合使用,以实现更好的系统性能和健壮性。一、区别组织结构:分布式:将一个复杂的业务系统拆分成多个子业务系统,每个子业务系统部署在不同的服务器上。这意味着,不同的子业务运行......
  • elastic-dump迁移es集群
    elasticdump安装   npminstall-gcnpm--registry=https://registry.npm.taobao.org   npminstallelasticdump-gelasticdump基本使用                  elasticdump--input=https://elastic:'111@tn111333'@10.246.181......
  • mac redis启动,redis哨兵模式,redis集群的相关命令
    Homebrew安装的软件会默认在/usr/local/Cellar/路径下redis的配置文件redis.conf存放在/usr/local/etc路径下cd/usr/local/Cellar/redis/7.0.10.存在cd/usr/local/opt/redis/bin/redis-server.目录存在cd/usr/local/etc/redis.conf存在。配置文件复制文件cp/us......
  • Ubuntu 配置 redis主从复制细节
    注意1、首先配置  slaveof<主服务器的IP><主服务器redis的端口>2、然后配置  masterauth主库密码3、记得开放端口或者直接关闭防火墙sudosystemctlstopufw.service  关闭防火墙sudosystemctldisableufw.service  禁用防火墙服sudoufwstatus  确认防......
  • Redis精讲
    redis持久化RDB方式RedisDatabaseBackupfile(redis数据备份文件),也被叫做redis数据快照.简单来说就是把内存中的所有数据记录到磁盘中.快照文件称为RDB文件,默认是保存在当前运行目录.[root@centos-zyw~]#dockerexec-itredisredis-cli127.0.0.1:6379>......
  • hadoop报错:is running 446528000B beyond the 'VIRTUAL' memory limit
    发现问题今天在配置hadoop文件时,报错:isrunning446528000Bbeyondthe'VIRTUAL'memorylimit.Currentusage:73.9MBof1GBphysicalmemoryused;2.5GBof2.1GBvirtualmemoryused.Killingcontainer.意思是虚拟内存,超出了限制。然后将虚拟机内存改为3G关闭集群......