redis cluster模式搭建
搭建过程中遇到很多问题,特此记录,以备后用
下载
从官网下载,地址:http://download.redis.io/releases/redis-4.0.1.tar.gz,可自主选择版本,本文所用版本为4.0.1
选择安装路径,本文是在/home/redis/redis-cluster/
路径下安装
[root@localhost ~]# cd /home/redis/redis-cluster/
[root@localhost redis-cluster]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root@localhost redis-cluster]# tar -zxvf redis-4.0.1.tar.gz
[root@localhost redis-cluster]# cd redis-4.0.1
[root@localhost redis-4.0.1]# make && make install
到此下载安装已经完成
配置
配置redis.conf文件
# 监听地址
bind 127.0.0.1
# 端口
port 27001
tcp-backlog 511
timeout 0
tcp-keepalive 300
# 守护进程
daemonize yes
supervised no
pidfile /var/run/redis_27001.pid
loglevel notice
logfile "./27001/redis.log"
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-27001.rdb
dir ./
# 和master之间通讯的密码
masterauth RedisPASS
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
# 自身安全所需密码
requirepass RedisPASS
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
# 只允许为文件,不允许为路径,否则会报错
appendfilename "appendonly-27001.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 no
lua-time-limit 5000
# 下面三个为cluster模式所必须的配置
# 开启集群模式
cluster-enabled yes
# 集群的配置文件
cluster-config-file 27001/nodes-27001.conf
# 集群之间心跳超时时间
cluster-node-timeout 10000
# 非必须,用来设置每个master有多少个slave,默认为1
cluster-migration-barrier 2
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
上面是配置文件的内容,如果想要优化,请结合具体情况
准备节点
因为条件所限,只有一台机器,所以就采用一台机器多个端口的方式实现,所以配置文件中所有的文件及路径全部带上端口以示区别
# 创建文件夹
[root@localhost redis-4.0.1] mkdir 27001 27002 27003 27004 27005 27006
# 拷贝redis.conf到各个文件夹下
[root@localhost redis-4.0.1] cp redis.conf 27001/redis.conf
[root@localhost redis-4.0.1] cp redis.conf 27002/redis.conf
[root@localhost redis-4.0.1] cp redis.conf 27003/redis.conf
[root@localhost redis-4.0.1] cp redis.conf 27004/redis.conf
[root@localhost redis-4.0.1] cp redis.conf 27005/redis.conf
[root@localhost redis-4.0.1] cp redis.conf 27006/redis.conf