redis---cluster集群模式搭建

一、准备集群模式的配置文件

grep -Ev "^$|#"  redis.conf

复制去除注释文本

mkdir -p  cluster-redis
cd  cluster-redis
mkdir -p  node1-7001  node2-7002  node3-7003  node4-7004  node5-7005  node6-7006

在每一个节点下面新建一个redis.conf文件 cd node1-7001 vim redis.conf

redis.conf配置文件的内容

bind 0.0.0.0
protected-mode no
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile home/han.sun/application/cluster-redis/node1-7001/redis_7001.pid
loglevel notice
logfile "/home/han.sun/application/cluster-redis/node1-7001/redis.log"

# 允许加入集群
cluster-enabled yes
# 此文件会被生成在上面dir配置目录下,
# cluster开启必须重命名指定cluster-config-file,
# 不能与别的节点相同,否则会启动失败,最好按主机+端口命名
cluster-config-file node1-7001.conf
# 节点宕机被发现的时间
cluster-node-timeout 15000

databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
# 此redis服务端密码
requirepass han.sun
# 每个节点都有可能是从节点,所以如果设置了requirepass,就要设置对应的masterauth
masterauth han.sun

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

二、启动各自的节点服务

redis-server  ./redis.conf

三、执行集群命令

redis-cli --cluster create 192.168.56.11:7001  192.168.56.11:7002  192.168.56.12:7003  192.168.56.12:7004  192.168.56.13:7005  192.168.56.13:7006  --cluster-replicas 1 -a han.sun

 

四、查看集群节点

redis-cli  -c -h ip地址 -a 密码  -p  7003

这说明cluster模式下会自动进行分配两两主从模式

五、测试set、get是否将key打在了不同的节点上

六、模拟一个主节点宕机

最后附上集群模式下,生产环境级别配置:

  1 bind 0.0.0.0
  2 # 关闭保护模式,让其他主机连接
  3 protected-mode no
  4 port 7005
  5 # tcp 连接等待队列长度
  6 tcp-backlog 511
  7 timeout 0
  8 tcp-keepalive 300
  9 # 开启后台运行模式
 10 daemonize yes
 11 supervised no
 12 # pid进程文件
 13 pidfile /home/u_njyh/redis-5.0.8/cluster-redis/node1-7005/redis_7005.pid
 14 # 日志记录级别
 15 loglevel notice
 16 # 日志文件保存位置
 17 logfile "/home/u_njyh/redis-5.0.8/cluster-redis/node1-7005/redis.log"
 18 
 19 # --------设置redis集群模式开启------------
 20 # 允许加入集群
 21 cluster-enabled yes
 22 # 此文件会被生成在上面dir配置目录下,
 23 # cluster开启必须重命名指定cluster-config-file,
 24 # 不能与别的节点相同,否则会启动失败,最好按主机+端口命名
 25 cluster-config-file node1-7005.conf
 26 # 节点宕机被发现的时间
 27 cluster-node-timeout 15000
 28 
 29 
 30 # --------设置redis工作目录、备份策略、配置访问密码------------
 31 databases 16
 32 always-show-logo yes
 33 # 设置多少微秒内发生几次写入内存keys到哈希槽时,执行copy-on-write
 34 save 900 1
 35 save 300 10
 36 save 60 10000
 37 # 后台备份出错时,是否停止写入keys到哈希槽
 38 stop-writes-on-bgsave-error yes
 39 # 数据文件是否压缩
 40 rdbcompression yes
 41 # 数据文件执行校验
 42 rdbchecksum yes
 43 # 数据文件保存位置
 44 dbfilename dump.rdb
 45 # redis数据文件保存的目录
 46 dir ./
 47 replica-serve-stale-data yes
 48 replica-read-only yes
 49 repl-diskless-sync no
 50 repl-diskless-sync-delay 5
 51 repl-disable-tcp-nodelay no
 52 replica-priority 100
 53 # 此redis服务端密码
 54 requirepass han.sun
 55 # 每个节点都有可能是从节点,所以如果设置了requirepass,就要设置对应的masterauth
 56 masterauth han.sun
 57 
 58 # --------设置client连接数、redis最大内存、置换策略------------
 59 # 设置同时连接客户端的最大数量,默认情况下这个限制设置为10000个客户。cluster模式为4064
 60 # 一旦达到限制,Redis将关闭所有新的连接,发送错误的最大客户数
 61 maxclients 4096
 62 # 将内存使用限制设置为指定的字节数。64位系统会默认为0表示最大内存空间。
 63 # 如果为32位系统最大可设置为3G
 64 # 动态更新config set maxmemory 100mb
 65 maxmemory 1024mb
 66 # 置换策略, Redis 内存使用达到maxmemory时,需要选择设置好的maxmemory-policy进行对旧数据的置换
 67 # 1.volatile-lru(least recently used):最近最少使用算法,从设置了过期时间的键中选择空转时间最长的键值对清除掉;
 68 # 2.volatile-lfu(least frequently used):最近最不经常使用算法,从设置了过期时间的键中选择某段时间之内使用频次最小的键值对清除掉;
 69 # 3.volatile-ttl:从设置了过期时间的键中选择过期时间最早的键值对清除;
 70 # 4.volatile-random:从设置了过期时间的键中,随机选择键进行清除;
 71 # 5.allkeys-lru:最近最少使用算法,从所有的键中选择空转时间最长的键值对清除;
 72 # 6.allkeys-lfu:最近最不经常使用算法,从所有的键中选择某段时间之内使用频次最少的键值对清除;
 73 # 7.allkeys-random:所有的键中,随机选择键进行删除;
 74 # 8.noeviction:不做任何的清理工作,在redis的内存超过限制之后,所有的写入操作都会返回错误;但是读操作都能正常的进行;
 75 maxmemory-policy allkeys-lru
 76 
 77 lazyfree-lazy-eviction no
 78 lazyfree-lazy-expire no
 79 lazyfree-lazy-server-del no
 80 replica-lazy-flush no
 81 # aop持久化开关
 82 appendonly no
 83 appendfilename "appendonly.aof"
 84 appendfsync everysec
 85 no-appendfsync-on-rewrite no
 86 auto-aof-rewrite-percentage 100
 87 auto-aof-rewrite-min-size 64mb
 88 aof-load-truncated yes
 89 aof-use-rdb-preamble yes
 90 lua-time-limit 5000
 91 slowlog-log-slower-than 10000
 92 slowlog-max-len 128
 93 latency-monitor-threshold 0
 94 notify-keyspace-events ""
 95 hash-max-ziplist-entries 512
 96 hash-max-ziplist-value 64
 97 list-max-ziplist-size -2
 98 list-compress-depth 0
 99 set-max-intset-entries 512
100 zset-max-ziplist-entries 128
101 zset-max-ziplist-value 64
102 hll-sparse-max-bytes 3000
103 stream-node-max-bytes 4096
104 stream-node-max-entries 100
105 activerehashing yes
106 client-output-buffer-limit normal 0 0 0
107 client-output-buffer-limit replica 256mb 64mb 60
108 client-output-buffer-limit pubsub 32mb 8mb 60
109 hz 10
110 dynamic-hz yes
111 aof-rewrite-incremental-fsync yes
112 rdb-save-incremental-fsync yes
原文地址:https://www.cnblogs.com/han-sun/p/12837233.html