搭建redis集群
- 多机器版本
- 最少6台机器,分别在其中六台机器也部署一套redis
#/bin/bash
#创建redis主目录,数据库存放目录,日志目录,pid目录
mkdir /data/redis
mkdir /data/redis/data/6301
mkdir /data/redis/logs
mkdir /data/redis/pid
#安装依赖
yum install -y gcc tcl
#下载redis安装包
wget https://download.redis.io/releases/redis-6.0.8.tar.gz
#解压安装包
tar -xvf redis-6.0.8.tar.gz
#移动文件到redis安装目录
mv redis-6.0.8 /data/redis/
#开始编译
make && make install
#测试编译
make test
#如果看到以下字样:表示无错误: \o/ All tests passed without errors!
grep ^[a-Z] redis_6379.conf
bind 192.168.13.212 # 允许通信ip
protected-mode yes
port 6301 # 其他集群机器分别也要不同6302、6303、6304、6305、6306
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes # redis后台运行
pidfile /var/run/redis_6301.pid # pid文件
loglevel notice
logfile "/data/redis_cluster/logs/redis_6301.log" # 集群日志文件
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./ # 配置数据文件存放路劲
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
maxmemory 64M
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no # 是否开启aof
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
cluster-enabled yes # 开启集群状态
cluster-config-file nodes-6301.conf # 集群的配置
cluster-node-timeout 15000 # 集群请求超时 默认15秒,可自行设置
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
jemalloc-bg-thread yes
#启动redis
/data/redis/redis-6.0.8/src/redis-server /data/redis/redis_6301.conf &
port 6301 # 端口6302、6303、6304、6305、6306
bind 192.168.11.11 # 允许通信ip
daemonize yes # redis后台运行
pidfile /usr/local/redis-cluster/redis1/redis_7001.pid # pidfile文件对应6302、6303、6304、6305、6306
cluster-enabled yes # 开启集群
cluster-config-file nodes_7001.conf # 集群的配置 配置文件首次启动自动生成 6302、6303、6304、6305、6306
cluster-node-timeout 15000 # 请求超时 默认15秒,可自行设置
appendonly yes # 开启aof
logfile "/usr/local/redis-cluster/redise1/logs/redis_7001.log" # 配置日志输入路劲 6302、6303、6304、6305、6306
dir "/usr/local/redis-cluster/redis1/data" # 配置数据文件存放路劲
创建集群
- 找一台机器做master控制集群节点,或者也可以在redis集群中其中一台安装master节点
yum -y install ruby ruby-devel rubygems rpm-build
rvm -v
gem install redis
yum install -y rubygems
redis-cli --cluster create 192.168.13.1:6301 192.168.13.2:6302 192.168.13.3:6303 192.168.13.4:6304 192.168.13.5:6305 192.168.13.6:6306 --cluster-replicas 1