一、Redis集群介绍
Redis 集群是一个提供在多个Redis节点间共享数据的程序集。Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误。Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令。
- Redis 集群的优势:
- 自动分割数据到不同的节点上;
- 整个集群的部分节点失败或者不可达的情况下能够继续处理命令。
二、Redis Cluster集群部署
- 环境
主机名 | IP | 端口 |
---|---|---|
node1 | 192.168.2.51 | 7000,7001 |
node2 | 192.168.2.52 | 7000,7001 |
node3 | 192.168.2.53 | 7000,7001 |
# TCP监听队列大小
echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf
sysctl -p
# OMM相关:vm.overcommit_memory
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl -p
# 开启内核特性
echo never > /sys/kernel/mm/transparent_hugepage/enabled
- 安装redis
- 分别在每台服务器上安装两个节点;
- 下载
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
- 解压编译安装
yum install gcc c++ -y tar -xf redis-5.0.5.tar.gz cd redis-5.0.5 make make MALLOC=libc make install PREFIX=/usr/local/redis-cluster mkdir -p /usr/local/redis-cluster/{7000,7001}
- 配置修改
cat redis.conf |grep -v ^# |grep -v ^$
cp redis.conf /usr/local/redis-cluster/7000/
cd /usr/local/redis-cluster/7000/
mv redis.conf 7000_redis.conf
vim 7000_redis.conf
bind 0.0.0.0
protected-mode yes
port 7000
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/usr/local/redis-cluster/7000/redis_7000.pid"
loglevel notice
logfile "/usr/local/redis-cluster/7000/redis_7000.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.rdb"
dir "/usr/local/redis-cluster/7000"
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
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 "/usr/local/redis-cluster/7000/nodes.conf"
cluster-node-timeout 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
- 复制配置文件
# 复制配置文件到另外两台服务器相同目录下
scp 7000/7000_redis.conf root@192.168.2.52:/usr/local/redis-cluster/7000/
scp 7001/7001_redis.conf root@192.168.2.52:/usr/local/redis-cluster/7001/
scp 7000/7000_redis.conf root@192.168.2.53:/usr/local/redis-cluster/7000/
scp 7001/7001_redis.conf root@192.168.2.53:/usr/local/redis-cluster/7001/
- 配置启动脚本
cat start_redis.sh
#!/bin/bash
REDIS_HOME=/usr/local/redis-cluster
$REDIS_HOME/bin/redis-server $REDIS_HOME/7000/7000_redis.conf
$REDIS_HOME/bin/redis-server $REDIS_HOME/7001/7001_redis.conf
chmod +x start_redis.sh
./start_redis.sh
- 集群配置
./redis-cli --cluster create 192.168.2.51:7000 192.168.2.51:7001 192.168.2.52:7000 192.168.2.52:7001 192.168.2.53:7000 192.168.2.53:7001 --cluster-replicas 1
- 配置密码验证
# 分别在每个节点执行
redis-cli -h 192.168.2.51 -p 7000 -c
config set masterauth 123456
config set requirepass 123456
auth 123456
config rewrite
tail -2 7000/7000_redis.conf
masterauth "123456"
requirepass "123456"
- redis-cli相关命令
# 查看集群状态
redis-cli -h 192.168.2.51 -p 7000 -c
192.168.2.51:7000> auth 123456
OK
192.168.2.51:7000> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:8
cluster_my_epoch:7
cluster_stats_messages_ping_sent:971263
cluster_stats_messages_pong_sent:975267
cluster_stats_messages_sent:1946530
cluster_stats_messages_ping_received:975267
cluster_stats_messages_pong_received:971262
cluster_stats_messages_received:1946529
# 查看节点信息
CLUSTER NODES
a3c152931ac1b68f61ad9a61b6e1771a12d46d08 192.168.2.51:7000@17000 myself,slave f869ba21aaf34dee11ec62fe61b58b224fbbfbd3 0 1564559165000 1 connected
5ea17476f641c656e3ee48ae4985181de195db88 192.168.2.53:7000@17000 slave 521da8dd67d8cd27f3f2546636652ae23b396abb 0 1564559164526 8 connected
e5235f0764ce1be056208d683cc69adf6c3398b1 192.168.2.53:7001@17001 slave 6ca305fb349e3281b532d431bbabb79a9bb9838f 0 1564559165526 6 connected
f869ba21aaf34dee11ec62fe61b58b224fbbfbd3 192.168.2.52:7001@17001 master - 0 1564559164025 7 connected 0-5460
6ca305fb349e3281b532d431bbabb79a9bb9838f 192.168.2.52:7000@17000 master - 0 1564559166027 3 connected 5461-10922
521da8dd67d8cd27f3f2546636652ae23b396abb 192.168.2.51:7001@17001 master - 0 1564559165025 8 connected 10923-16383