1. Redis集群部署 (5.0.14)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Redis 集群的优势:
自动分割数据到不同的节点上.
整个集群的部分节点失败或者不可达的情况下能够继续处理命令.
Redis 集群的数据分片
Redis 集群没有使用一致性hash, 而是引入了哈希槽的概念.
Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽.
集群的每个节点负责一部分hash槽,举个例子,比如当前集群有3个节点,那么:
节点 A 包含 0 到 5500号哈希槽.
节点 B 包含5501 到 11000 号哈希槽.
节点 C 包含11001 到 16384号哈希槽.
这种结构很容易添加或者删除节点. 比如如果我想新添加个节点D, 我需要从节点 A, B, C中得部分槽到D上.
如果我想移除节点A,需要将A中得槽移到B和C节点上,然后将没有任何槽的A节点从集群中移除即可.
由于从一个节点将哈希槽移动到另一个节点并不会停止服务,所以无论添加删除或者改变某个节点
的哈希槽的数量都不会造成集群不可用的状态.

1.1 开启防火墙

1
2
3
4
5
6
7
8
9
10
11
## 开启防火墙
systemctl status firewalld
systemctl start firewalld
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --state

firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=17000/tcp --permanent
firewall-cmd --zone=public --add-port=17001/tcp --permanent

1.2 安装redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- 安装路径:/usr/local/redis5-cluster
- 配置文件:/usr/local/redis5-cluster/conf

- 创建数据目录。
# mkdir -pv /usr/local/redis5-cluster/redis-data
- 创建日志目录。
# mkdir -pv /usr/local/redis5-cluster/redis-logs
- 创建配置目录。
# mkdir -pv /usr/local/redis5-cluster/conf


#解压
tar xzf redis-5.0.8.tar.gz
make MALLOC=libc
make install PREFIX=/usr/local/redis5

cd /usr/local/redis5
mkdir /usr/local/redis5/{bin,logs,conf,redis_data}
cp redis-check-aof redis-benchmark redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis5/bin/
cp /home/software/redis-5.0.14/redis.conf /usr/local/redis5/conf/

1.3 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 需要修改的redis的配置文件,配置文件示例
---其内容为:
bind 192.168.1.30
requirepass 123456
protected-mode no
port 7000 #需要修改端口。
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /usr/local/redis5-cluster/bin/redis_7000.pid #需要修改端口
loglevel notice
logfile /usr/local/redis5-cluster/redis-logs/redis-7000.log #需要修改端口
databases 26
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-7000.rdb #需要修改端口
dir /usr/local/redis5-cluster/redis_data/
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-7000.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 redis-7000.conf #需要修改端口
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

1.4 部署过程

1
2
3
4
5
6
7
8
9
# 部署包
/usr/local/redis5-cluster/redis-7000
/usr/local/redis5-cluster/redis-7001

# 启动redis服务
/usr/local/redis5-cluster/redis-7000/bin/redis-server /usr/local/redis5-cluster/redis-7000/conf/redis-7000.conf
/usr/local/redis5-cluster/redis-7001/bin/redis-server /usr/local/redis5-cluster/redis-7001/conf/redis-7001.conf

ps -ef |grep redis |grep redis5 |grep 7001 |awk '{print $2}'|xargs kill -9

1.5 创建集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#参数解释
--replicas 1 表示我们希望为集群中的每个主节点创建一个从节点

# 创建集群
echo yes| /usr/local/redis5-cluster/redis-7000/bin/redis-cli -h 192.168.1.30 -a 123456 -p 7000 --cluster create 192.168.1.30:7000 192.168.1.30:7001 192.168.1.31:7000 192.168.1.31:7001 192.168.1.32:7000 192.168.1.32:7001 --cluster-replicas 1 --cluster-yes

# 集群验证
# /usr/local/redis5-cluster/redis-7000/bin/redis-cli -h 192.168.1.30 -p 7000
192.168.1.30:7000> auth 123456
OK
192.168.1.30:7000> cluster nodes
85eaca64000d30f45a67536c53d2dc66adfab08b 192.168.1.32:7000@17000 master - 0 1688714888000 5 connected 10923-16383
7da8c524474d3ea7db335000c694c812afebd57c 192.168.1.32:7001@17001 slave 7bd394bfe5d692c5e8df93ed6721b8020fd17e5e 0 1688714889289 6 connected
63d1e6e9eaa1cf89d08db90e75af6172743e15ff 192.168.1.31:7001@17001 slave 193a4ffd4e5bb2f541e925db3119f9c81b8fb21c 0 1688714888000 4 connected
193a4ffd4e5bb2f541e925db3119f9c81b8fb21c 192.168.1.30:7000@17000 myself,master - 0 1688714888000 1 connected 0-5460
7bd394bfe5d692c5e8df93ed6721b8020fd17e5e 192.168.1.31:7000@17000 master - 0 1688714890000 3 connected 5461-10922
ce22d2cc1dbbe1874804fcfeb74bf14233276223 192.168.1.30:7001@17001 slave 85eaca64000d30f45a67536c53d2dc66adfab08b 0 1688714890292 5 connected

2. 运维管理命令

2.1 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
- 安全关闭
# /usr/local/redis5-cluster/redis-7000/bin/redis-cli shutdown save

- 不备份关闭
# /usr/local/redis5-cluster/redis-7000/bin/redis-cli shutdown nosave

- 后台运⾏备份不阻塞主进程/save异步保存到硬盘 堵塞主进程
# 127.0.0.1:6379[1]> bgsave
# Background saving started

- aof备份⽅式-异步执⾏⼀个aof⽂件重写操作
# 127.0.0.1:6379[1]> BGREWRITEAOF
# Background append only file rewriting started

- 查看客户端连接情况
# 127.0.0.1:6379> client list
# age: 客户端存活的秒数
# idle: 客户端空闲的秒数

- kill掉⽐较损耗资源的连接
# redis 10.10.1.173:6379> client kill 10.10.0.46:31574

- 设置客户端标识-标记当前链接名
# 127.0.0.1:6379> client setname jesse
# 127.0.0.1:6379> client list
# id=127236 addr=127.0.0.1:39330 fd=6 name=jesse .....

- 清空当前的数据库flushdb/flushall清空所有的数据库
# 127.0.0.1:6379[2]> flushdb

- 查看当前的roles
# 127.0.0.1:6379[2]> ROLE
1) "master"
2) (integer) 0
3) (empty list or set)

- 查看配置信息
# 127.0.0.1:6379[2]> config get timeout
1) "timeout"
2) "300"
127.0.0.1:6379[2]> config get dbfilename
1) "dbfilename"
2) "6379.rdb"

- 在线修改参数
# 127.0.0.1:6379[2]> config set timeout 301

2.2 持久化运维

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# rdb/aof
# 1.配置⽂件修改
save 900 1
save 300 10
save 60 10000
# 2.命令⾏执⾏保存
save 前台执⾏ 堵塞主线程其它读写操作
bgsave 后台执⾏ 不堵塞读写操作
# 3.⽇志⽂件追加持久化
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
执⾏命令:
bgrewriteaof

# 优点:每秒刷新⼀次磁盘 最多丢失1s数据
# 缺点:相同的数据集来说 AOF⽂件的体积通常要⼤于RDB⽂件的体积