• redis 6.2.12集群部署


    1. 下载安装包

    1. cd /data/base
    2. wget https://download.redis.io/releases/redis-6.2.12.tar.gz
    3. tar -zxvf redis-6.2.12.tar.gz
    4. # 将redis代码移动到src目录
    5. mv redis-6.2.12 redis-6.2.12-src
    6. # 创建新目录用户存放编译好的redis文件
    7. mkdir redis-6.2.12

    2. 编译安装

    1. # 指定内存库为libc,或者使用jmelloc
    2. make -j4 MALLOC=libc
    3. # 安装时指定目标文件目录
    4. make PREFIX=/data/base/redis-6.2.12 install

    3. 新建配置文件redis.conf并启动redis-server

    1. cd redis-6.2.12
    2. vim redis.conf
    1. # 后台启动redis
    2. daemonize yes
    3. # 修改pid文件保存目录
    4. pidfile /data/base/redis-6.2.12/redis_6379.pid
    5. # 修改日志文件路径和名称
    6. logfile "redis.log"
    7. # 修改密码
    8. requirepass 123456
    9. # 允许远程登录
    10. bind 0.0.0.0 -::1

    4.启动redis-server并查看进程状态

    1. ./redis-server redis.conf
    2. [root@redis1 bin]# ./redis-server redis.conf 
    3. [root@redis1 bin]# ps -ef|grep redis
    4. root      36152      1  0 09:40 ?        00:00:00 ./redis-server 0.0.0.0:6379
    5. root      36303   1566  0 09:40 pts/0    00:00:00 grep --color=auto redis

    5.redis-cli客户端使用

    登录redis

    1. [root@redis1 bin]# ./redis-cli -h localhost -p 6379
    2. localhost:6379> auth 123456
    3. OK

    6.redis 集群搭建(cluster模式)

    6.1集群规划

    ip

    port

    部署路径

    10.16.60.81

    6379

    /data/base/redis-6379

    10.16.60.81

    6380

    /data/base/redis-6380

    10.16.60.82

    6379

    /data/base/redis-6379

    10.16.60.82

    6380

    /data/base/redis-6380

    10.16.60.83

    6379

    /data/base/redis-6379

    10.16.60.83

    6380

    /data/base/redis-6380

    6.2 修改redis配置

    将编译好的redis文件目录修改目录名(为了方便,以端口号命名),并复制1份。这样可以在一台机器上启动2个节点。

    1. mv redis-6.2.12 redis-6379
    2. cp -r redis-6379/ redis-6380

    分别修改这2个节点的配置

    1. # 后台启动redis
    2. daemonize yes
    3. # 修改pid文件保存目录
    4. pidfile /data/base/redis-6379/redis_6379.pid
    5. port 6379
    6. cluster-enabled yes
    7. # 修改日志文件路径和名称
    8. logfile "redis.log"
    9. # 修改密码
    10. requirepass 123456
    11. # 允许远程登录
    12. bind 0.0.0.0 -::1
    1. # 后台启动redis
    2. daemonize yes
    3. # 修改pid文件保存目录
    4. pidfile /data/base/redis-6380/redis_6380.pid
    5. port 6380
    6. cluster-enabled yes
    7. # 修改日志文件路径和名称
    8. logfile "redis.log"
    9. # 修改密码
    10. requirepass 123456
    11. # 允许远程登录
    12. bind 0.0.0.0 -::1
    13. # 从节点要配置主节点的密码,否则主节点拒绝同步
    14. masterauth 123456

    6.3 分发redis目录 启动并查看redis-server信息

    将/data/base/redis-6379和/data/base/redis-6380 这2个目录拷贝到其他2台机器上。

    在每台机器上分别进入到这2个节点的目录,启动这2个节点的redis-server。

    ./redis-server redis.conf

    查看redis进程信息

    1. [root@redis1 bin]# ps -ef|grep redis
    2. root     126449      1  0 11:15 ?        00:00:00 ./redis-server 0.0.0.0:6379 [cluster]
    3. root     127165      1  0 11:16 ?        00:00:00 ./redis-server 0.0.0.0:6380 [cluster]
    4. root     127733   1566  0 11:16 pts/0    00:00:00 grep --color=auto redis

    6.4创建redis集群

    在10.16.60.81机器的/data/base/redis-6379目录下执行

    ./redis-cli -a 123456 --cluster create --cluster-replicas 1 10.16.60.81:6379 10.16.60.81:6380 10.16.60.82:6379 10.16.60.82:6380 10.16.60.83:6379 10.16.60.83:6380

    返回如下:

    1. >>> Performing hash slots allocation on 6 nodes...
    2. Master[0] -> Slots 0 - 5460
    3. Master[1] -> Slots 5461 - 10922
    4. Master[2] -> Slots 10923 - 16383
    5. Adding replica 10.16.60.82:6380 to 10.16.60.81:6379
    6. Adding replica 10.16.60.83:6380 to 10.16.60.82:6379
    7. Adding replica 10.16.60.81:6380 to 10.16.60.83:6379
    8. M: b61869f33aaf9459a6e4fb13580fb6beacc171110.16.60.81:6379
    9.    slots:[0-5460] (5461 slots) master
    10. S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380
    11.    replicates de47b58dbc62e6136aca7df33cd8bce17a91a412
    12. M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379
    13.    slots:[5461-10922] (5462 slots) master
    14. S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380
    15.    replicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
    16. M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379
    17.    slots:[10923-16383] (5461 slots) master
    18. S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380
    19.    replicates 21ebb5e282e6727ffc8f5bd546d404406da467f4
    20. Can I set the above configuration? (type 'yes' to accept):

    输入yes,返回

    1. >>> Nodes configuration updated
    2. >>> Assign a different config epoch to each node
    3. >>> Sending CLUSTER MEET messages to join the cluster
    4. Waiting for the cluster to join
    5. .
    6. >>> Performing Cluster Check (using node 10.16.60.81:6379)
    7. M: b61869f33aaf9459a6e4fb13580fb6beacc171110.16.60.81:6379
    8.    slots:[0-5460] (5461 slots) master
    9.    1 additional replica(s)
    10. S: 350656e9823e2d84d0e763faca90e7e499d04fce 10.16.60.82:6380
    11.    slots: (0 slots) slave
    12.    replicates b61869f33aaf9459a6e4fb13580fb6beacc1711b
    13. M: 21ebb5e282e6727ffc8f5bd546d404406da467f4 10.16.60.82:6379
    14.    slots:[5461-10922] (5462 slots) master
    15.    1 additional replica(s)
    16. M: de47b58dbc62e6136aca7df33cd8bce17a91a412 10.16.60.83:6379
    17.    slots:[10923-16383] (5461 slots) master
    18.    1 additional replica(s)
    19. S: 69ecc2dd284e397968a029bc89b97260951450fe 10.16.60.83:6380
    20.    slots: (0 slots) slave
    21.    replicates 21ebb5e282e6727ffc8f5bd546d404406da467f4
    22. S: 7d394aaf601421a6ea1de3827d67d546eda91bcb 10.16.60.81:6380
    23.    slots: (0 slots) slave
    24.    replicates de47b58dbc62e6136aca7df33cd8bce17a91a412
    25. [OK] All nodes agree about slots configuration.
    26. >>> Check for open slots...
    27. >>> Check slots coverage...
    28. [OK] All 16384 slots covered.

    6.5 redis-cli 连接集群查看集群信息

    1. [root@redis1 bin]# ./redis-cli -h 10.16.60.81 
    2. 10.16.60.81:6379> auth 123456
    3. OK
    4. 10.16.60.81:6379> cluster info
    5. cluster_state:ok
    6. cluster_slots_assigned:16384
    7. cluster_slots_ok:16384
    8. cluster_slots_pfail:0
    9. cluster_slots_fail:0
    10. cluster_known_nodes:6
    11. cluster_size:3
    12. cluster_current_epoch:6
    13. cluster_my_epoch:1
    14. cluster_stats_messages_ping_sent:389
    15. cluster_stats_messages_pong_sent:422
    16. cluster_stats_messages_sent:811
    17. cluster_stats_messages_ping_received:417
    18. cluster_stats_messages_pong_received:389
    19. cluster_stats_messages_meet_received:5
    20. cluster_stats_messages_received:811

    6.6 验证数据

    在10.16.60.81上执行

    1. [root@redis1 bin]# ./redis-cli -c -a 123456
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. 127.0.0.1:6379> keys *
    4. (empty array)
    5. 127.0.0.1:6379> set user user_1
    6. -> Redirected to slot [5474] located at 10.16.60.82:6379
    7. OK

    在10.16.60.82上执行

    1. [root@redis2 bin]# ./redis-cli -c -a 123456
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. 127.0.0.1:6379> keys *
    4. 1"user"
    5. 127.0.0.1:6379> get user
    6. "user_1"

    6.7 springboot配置redis集群信息

    1. spring:
    2.   redis:
    3.     database: 0 # redis集群只能配置0
    4. #host: 10.16.60.81
    5.     password: 123456
    6.     cluster:
    7.       nodes: 10.16.60.81:6379,10.16.60.81:6380,10.16.60.82:6379,10.16.60.82:6380,10.16.60.83:6379,10.16.60.83:6380
    8. #max-redirects: 3
    9.     timeout: 60000 #连接超时时间(毫秒)
    10. # Lettuce配置
    11.     lettuce:
    12. # Lettuce连接池配置
    13.       pool:
    14. # 最大激活连接数
    15.         max-active: 200
    16. # 在连接池耗尽时,在抛出异常之前连接分配应阻塞的最长时间
    17.         max-wait: 20000
    18. # 最小空闲连接数
    19.         min-idle: 10

    7.问题汇总

    7.1 Node 10.16.60.82:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

    原因是该节点中默认生成的配置或历史存储数据不一致导致的,清除对应节点的dump.rdb、nodes.conf文件,重启之后即可。

    7.2 redis 10.16.60.82:6379 Could not connect to Redis No route to host

    原因可能是防火墙没有关闭

    1. systemctl stop firewalld
    2. systemctl disable firewalld

    7.3 ERR SELECT is not allowed in cluster mode

    1. 127.0.0.1:6379> select 0
    2. OK
    3. 127.0.0.1:6379> select 1
    4. (error) ERR SELECT is not allowed in cluster mode

    在redis集群模式下,默认且只能使用0号database库,不允许使用SELECT 操作选择database 。

     

     

     

  • 相关阅读:
    CentOS7.5离线安装jdk1.8
    【C语言入门】ZZULIOJ 1036-1040
    ios面试准备 - 网络篇
    生成可执行jar
    【C++】——string类的模拟实现
    跟着阿昌学习day3-【go基础知识】Content(上下文)探究
    Zookeeper学习二集群搭建
    每次重启完IDEA,application.properties文件里的中文变成?
    目前主流的负载均衡器
    2021-09-29破解小米“铁蛋”,只需9999元,你也可以做一个四足机器人!
  • 原文地址:https://blog.csdn.net/shangjg03/article/details/133354712