Redis集群搭建(多机集群)

  链接: https://pan.baidu.com/s/1IczvAUFhNz23xvCjlTiI_A?pwd=ruge

  1.  Redis入门篇
    https://blog.csdn.net/tongxin_tongmeng/article/details/126620333
  2. 集群配置文件(3主3从)
    1.复制/home/redis/redis-7.0.4/redis.conf到/home/redis/workspace/cluster_many
    cp /home/redis/redis-7.0.4/redis.conf /home/redis/workspace/cluster_many/redis_7001.conf
    cp /home/redis/redis-7.0.4/redis.conf /home/redis/workspace/cluster_many/redis_7002.conf
    cp /home/redis/redis-7.0.4/redis.conf /home/redis/workspace/cluster_many/redis_7003.conf
     
    2.编辑/home/redis/workspace/cluster_many/redis_7001.conf
    设置服务端口:port 7001
    解除本机绑定:# bind 127.0.0.1 -::1
    守护方式启动:daemonize yes
    设置pid路径:pidfile /home/redis/workspace/cluster_many/redis_7001.pid
    设置存储路径:dir /home/redis/workspace/cluster_many
    设置连接密码:requirepass 123456
    AOF方式持久化:appendonly yes
    AOF存储文件名:appendfilename "appendonly_7001.aof"
    AOF持久化策略:appendfsync always
    开启集群:cluster-enabled yes
    集群Node名称:luster-config-file nodes_7001.conf
    集群超时时间:cluster-node-timeout 5000
    
    3.编辑/home/redis/workspace/cluster_many/redis_7002.conf
    设置服务端口:port 7002
    解除本机绑定:# bind 127.0.0.1 -::1
    守护方式启动:daemonize yes
    设置pid路径:pidfile /home/redis/workspace/cluster_many/redis_7002.pid
    设置存储路径:dir /home/redis/workspace/cluster_many
    设置连接密码:requirepass 123456
    AOF方式持久化:appendonly yes
    AOF存储文件名:appendfilename "appendonly_7002.aof"
    AOF持久化策略:appendfsync always
    开启集群:cluster-enabled yes
    集群Node名称:luster-config-file nodes_7002.conf
    集群超时时间:cluster-node-timeout 5000
    
    4.编辑/home/redis/workspace/cluster_many/redis_7003.conf
    设置服务端口:port 7003
    解除本机绑定:# bind 127.0.0.1 -::1
    守护方式启动:daemonize yes
    设置pid路径:pidfile /home/redis/workspace/cluster_many/redis_7003.pid
    设置存储路径:dir /home/redis/workspace/cluster_many
    设置连接密码:requirepass 123456
    AOF方式持久化:appendonly yes
    AOF存储文件名:appendfilename "appendonly_7003.aof"
    AOF持久化策略:appendfsync always
    开启集群:cluster-enabled yes
    集群Node名称:luster-config-file nodes_7003.conf
    集群超时时间:cluster-node-timeout 5000

    0a95a0ce498a4475a1a4edbb3dbdbc2f.png

  3. 克隆redis-1-->redis-2

    redis-1:192.168.1.4
    redis-2:192.168.1.244

    5843434c7c8c4d7fa9862850ed5ba2ab.png

    2cae8a1fffde4db6923bf856dc4247d0.png

  4. 创建集群

    注意:端口7001、7002、7003为本机客户端登录端口,17001、17002、17003为redis集群默认的集群总线端口,集群内部不同机器之间通过集群总线端口互相进行客户端登录,如果防火墙不打开集群总线端口则集群无法正常创建,持续阻塞"Waiting for the cluster to join"
    
    1.打开redis-1防火墙端口权限
    firewall-cmd --zone=public --add-port=17001/tcp --permanent
    firewall-cmd --zone=public --add-port=17002/tcp --permanent
    firewall-cmd --zone=public --add-port=17003/tcp --permanent
    firewall-cmd --reload
    
    2.启动redis-1的redis服务
    redis-server /home/redis/workspace/cluster_many/redis_7001.conf
    redis-server /home/redis/workspace/cluster_many/redis_7002.conf
    redis-server /home/redis/workspace/cluster_many/redis_7003.conf
    
    3.打开redis-2防火墙端口权限
    firewall-cmd --zone=public --add-port=17001/tcp --permanent
    firewall-cmd --zone=public --add-port=17002/tcp --permanent
    firewall-cmd --zone=public --add-port=17003/tcp --permanent
    firewall-cmd --reload
    
    4.启动redis-2的redis服务
    redis-server /home/redis/workspace/cluster_many/redis_7001.conf
    redis-server /home/redis/workspace/cluster_many/redis_7002.conf
    redis-server /home/redis/workspace/cluster_many/redis_7003.conf
    
    5.创建集群(redis-1或redis-2执行)
    redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.1.4:7001 192.168.1.4:7002 192.168.1.4:7003 192.168.1.244:7001 192.168.1.244:7002 192.168.1.244:7003
    注意:前3个为主节点,后3个为从节点,创建命令只执行一次,重启集群只需要重启redis服务
    

    a8a6ad93e06f4a4e80cf6088512f2d20.png
    e290fff2a9e6444caa3ab54fa516e263.png
    ddb82a7e6f0d40f39716977dad0ac6af.png

  5. 启动集群

    1.启动redis-1的redis服务
    redis-server /home/redis/workspace/cluster_many/redis_7001.conf
    redis-server /home/redis/workspace/cluster_many/redis_7002.conf
    redis-server /home/redis/workspace/cluster_many/redis_7003.conf
    
    2.启动redis-2的redis服务
    redis-server /home/redis/workspace/cluster_many/redis_7001.conf
    redis-server /home/redis/workspace/cluster_many/redis_7002.conf
    redis-server /home/redis/workspace/cluster_many/redis_7003.conf

    8cecb89c51da4acab5a1041e3d6331c0.png
    b111e96e7a5c4b118f785f8c902cbf9d.png

  6. 关闭集群

    1.方式一:redis-cli命令关闭集群(推荐)
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7001 shutdown
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7002 shutdown
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7003 shutdown
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7001 shutdown
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7002 shutdown
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7003 shutdown
    注意:redis-1或者redis-2可以相互关闭对方的redis服务
    
    2.方式二:根据进程号关闭集群(方便)
    ps -ef | grep -i redis
    kill -9 24611 24617 24643

    27f4e3c212de48e79044d86892157815.png
    0dc81da7ffa04b119b573824ffa6e1fb.png
    ca3952e42d0e4e969ec52f82a7114015.png
    d6093714b2ac4258978ead7ead5de41c.png

  7. 登录集群

    1.登录redis-1的客户端
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7001
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7002
    redis-cli -a 123456 -c -h 192.168.1.4 -p 7003
    
    2.登录redis-2的客户端
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7001
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7002
    redis-cli -a 123456 -c -h 192.168.1.244 -p 7003
    
    2.客户端切换时需要先退出:quit

    e46f28fc5b534aecb83c83a5597167dc.png

    867dbda466e7487d8ef97da6525a31d2.png

  8. 集群信息
    5fe82708b6904f20bbb7a60f0516e90e.png

  9. 数据备份、恢复
    7eee00b89ab54812b492c43e547ccc4b.png
    c0273672044b4a66b45f85c77204a763.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童心同萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值