redis集群搭建(三主三从)

打印 上一主题 下一主题

主题 1571|帖子 1571|积分 4715

redis集群搭建(三主三从)



  

一、环境准备

每台机器有两个节点,一共六个节点(三主三从),主节点和从节点不在同一台机器上,有条件固然可以选择使用6台机器
地点端口172.24.8.26379 6380172.24.8.36379 6380172.24.8.36379 6380 二、环境搭建

首先确保防火墙处于关闭状态否则创建集群时间会报错

这种环境只需要关闭防火墙即可
  1. systemctl stop firewalld && systemctl disable firewalld
复制代码
安装语言环境
  1. yum -y install gcc gcc-c++
复制代码
创建目录
mkdir -p /data/redis/redis_cluster/6379
mkdir -p /data/redis/redis_cluster/6380
拷贝bin/redis.conf 6379和6380的目录下面
并分别修改他们的端口

redis.conf 配置文件
  1. bind 172.24.8.2
  2. #requirepass oa123456
  3. daemonize yes
  4. pidfile /var/run/redis_6379.pid
  5. logfile ""
  6. stop-writes-on-bgsave-error no
  7. dir ./
  8. maxmemory 1GB
  9. maxmemory-policy allkeys-lru
  10. appendonly no
  11. appendfilename "appendonly.aof"
  12. no-appendfsync-on-rewrite no
  13. aof-use-rdb-preamble yes
  14. #新增
  15. cluster-enabled  yes
  16. cluster-config-file nodes_6379.conf
  17. cluster-node-timeout  15000
  18. protected-mode yes
  19. port 6379
  20. tcp-backlog 511
  21. timeout 0
  22. tcp-keepalive 300
  23. supervised no
  24. loglevel notice
  25. databases 16
  26. always-show-logo yes
  27. rdbcompression yes
  28. rdbchecksum yes
  29. replica-serve-stale-data yes
  30. replica-read-only yes
  31. repl-diskless-sync no
  32. repl-diskless-sync-delay 5
  33. repl-disable-tcp-nodelay no
  34. replica-priority 100
  35. lazyfree-lazy-eviction no
  36. lazyfree-lazy-expire no
  37. lazyfree-lazy-server-del no
  38. replica-lazy-flush no
  39. appendfsync everysec
  40. auto-aof-rewrite-percentage 100
  41. auto-aof-rewrite-min-size 64mb
  42. aof-load-truncated yes
  43. lua-time-limit 5000
  44. slowlog-log-slower-than 10000
  45. slowlog-max-len 128
  46. latency-monitor-threshold 0
  47. notify-keyspace-events ""
  48. hash-max-ziplist-entries 512
  49. hash-max-ziplist-value 64
  50. list-max-ziplist-size -2
  51. list-compress-depth 0
  52. set-max-intset-entries 512
  53. zset-max-ziplist-entries 128
  54. zset-max-ziplist-value 64
  55. hll-sparse-max-bytes 3000
  56. stream-node-max-bytes 4096
  57. stream-node-max-entries 100
  58. activerehashing yes
  59. client-output-buffer-limit normal 0 0 0
  60. client-output-buffer-limit replica 256mb 64mb 60
  61. client-output-buffer-limit pubsub 32mb 8mb 60
  62. hz 10
  63. dynamic-hz yes
  64. aof-rewrite-incremental-fsync yes
  65. rdb-save-incremental-fsync yes
复制代码
启动redis
  1. bin/redis-server redis_cluster/6379/redis.conf
  2. bin/redis-server redis_cluster/6380/redis.conf
复制代码
三、创建集群

  1. bin/redis-cli --cluster create 172.24.8.2:6379 172.24.8.2:6380 172.24.8.3:6379 172.24.8.3:6380 172.24.8.4:6379 172.24.8.4:6380 --cluster-replicas 1 -a oa123456
复制代码
出现绿色表现集群创建成功


登录进去并查察集群各节点
  1. bin/redis-cli -c -h 172.24.8.2 -p 6379
  2. cluster nodes
  3. cluster info
复制代码

验证集群

四、设置暗码

  1. 172.24.8.2:6379> config set requirepass oa123456
  2. OK
  3. 172.24.8.2:6379> config rewrite
  4. (error) NOAUTH Authentication required.
  5. 172.24.8.2:6379> auth oa123456
  6. OK
  7. 172.24.8.2:6379> config rewrite
  8. OK
  9. 172.24.8.2:6379>
复制代码
Redis 出现(error)NOAUTH Authentication required
使用暗码登录任意节点
  1. bin/redis-cli -c -h 172.24.8.4 -p 6380 -a oa123456
复制代码

五、加入到开机自启动服务

第一个redis端口6379
  1. vim /etc/systemd/system/redis6379.service
复制代码
内容添加如下
  1. [Unit]
  2. Description=redis6379-server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/data/redis/bin/redis-server /data/redis/redis_cluster/6379/redis.conf
  7. PrivateTmp=true
  8. [Install]
  9. WantedBy=multi-user.target
复制代码
启动与停止与开机自启动
  1. systemctl daemon-reload
  2. systemctl start redis6379.service
  3. systemctl stop redis6379.service
  4. systemctl enable redis6379.service
复制代码
第二个redis端口6380
  1. vim /etc/systemd/system/redis6380.service
复制代码
内容添加如下
  1. [Unit]
  2. Description=redis6380-server
  3. After=network.target
  4. [Service]
  5. Type=forking
  6. ExecStart=/data/redis/bin/redis-server /data/redis/redis_cluster/6380/redis.conf
  7. PrivateTmp=true
  8. [Install]
  9. WantedBy=multi-user.target
复制代码
启动与停止与开机自启动
  1. systemctl daemon-reload
  2. systemctl start redis6380.service
  3. systemctl stop redis6380.service
  4. systemctl enable redis6380.service
复制代码
六、redis故障处理再加入

检查节点状态
  1. ./redis-cli -h 172.24.8.40 -p 6380 -a oa123456
  2. cluster nodes
复制代码
可以看到2个节点失去接洽

强制移除故障节点
  1. 本案例nodeID分别为
  2. 2396c1bf141981ebffa199e3ac196a34d7ed7622
  3. 5699fc668ca3d3fb0d83488f74aaecc17fe8c145
复制代码
  1. cluster forget 2396c1bf141981ebffa199e3ac196a34d7ed7622
  2. cluster forget 5699fc668ca3d3fb0d83488f74aaecc17fe8c145
复制代码

重新加入集群
  1. ./redis-cli -h 172.24.8.35 -a oa123456 -p 6380 cluster meet 172.24.8.40 6380
复制代码
发现酿成五主一从

调整主从节点关系
将主节点酿成从节点
  1. ./redis-cli -h 172.24.8.35 -p 6381 -a oa123456 cluster replicate 2146d339234fb9916e3510325e61047f41b04a18
复制代码
  1. bin/redis-cli -c -h 172.24.8.2 -a oa123456 --cluster add-node 172.24.8.3:6379 172.24.8.3:6380 --cluster-slave --cluster-master-id 7ff17164a1dd665448a49f6526ea
  2. 33d036709b34
复制代码
搭建集群时报错[ERR] Not all 16384 slots are covered by nodes.
其原因:
这个往往是由于主node移除了,但是并没有移除node上面的slot,从而导致了slot总数没有到达16384,实在也就是slots分布不精确。所以在删除节点的时间一定要留意删除的是否是Master主节点。
办理办法:
1、使用如下命令来修复集群
  1. # redis-cli --cluster fix host:port
  2. bin/redis-cli --cluster fix 172.24.8.3:6379 -a oa123456
复制代码
2、修复完成后再用check命令检查下是否精确,其命令如下:
  1. # redis-cli --cluster check host:port
  2. bin/redis-cli --cluster check 172.24.8.3:6379 -a oa123456
  3. redis-cli --cluster check 127.0.0.1:6379
复制代码
3、假如分布不匀称那可以使用下面的方式重新分配slot
  1. # redis-cli --cluster reshar host:port
  2. redis-cli --cluster reshard 127.0.0.1:6379
复制代码
握手
  1. echo 'cluster meet 172.24.8.3 6379' | /data/redis/bin/redis-cli -h 172.24.8.2 -p 6379 -a "oa123456"
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南七星之家

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表