redis常见部署架构
- 一、redis常见部署架构
- 1、常见部署架构
- 2、多实例部署
- 2.1 规划安装目录、配置文件
- 2.2.2 编辑实例配置文件
- 2.2.3 启动实例
- 2.2.4 测试数据读写
- 3、redis主从复制
- 3.1 规划
- 3.2 从服务器配置
- 3.3 验证主从状态
- 3.4 主从角色切换
- 4、分片集群
- 4.1 原理
- 4.2 分片集群的部署
- 4.2.1 两台服务器安装redis
- 4.2.2 配置redis实例
- 4.2.3 创建分片集群
- 4.2.5 连接集群测试数据读写
- 5、哨兵集群
- 6、Redis哨兵模式的配置
- 准备环境
- 主从节点配置
- 哨兵节点配置
- 启用哨兵
- 启动sentienl,系统会在配置文件中自动写入哨兵的id、链接信息,主从节点信息和其他哨兵信息。如下sentinel-26380.conf启动后的完整信息:
- 关掉端口号为6381的redis主服务器程序
- 端口号为6382的服务器成为了主服务器
一、redis常见部署架构
1、常见部署架构
多实例部署
主从复制
分片集群
哨兵集群
2、多实例部署
安装目录: /opt/6380 端口: 6380
安装目录: /opt/6381 端口: 6381
2.1 规划安装目录、配置文件
[root@localhost ~]# mkdir -p /opt/638{0,1}/{conf,data}
[root@localhost ~]#
[root@localhost ~]# cp /usr/local/redis50/conf/redis.conf /opt/6380/conf/
[root@localhost ~]#
2.2.2 编辑实例配置文件
[root@localhost ~]# vim /opt/6380/conf/redis.conf
bind 192.168.140.10
port 6380
daemonize yes
appendonly yes
pidfile /var/run/redis_6380.pid
loglevel warning
logfile "/var/log/redis_6380.log"
dbfilename dump_6380.rdb
dir /opt/6380/data
appendfilename "appendonly_6380.aof"
2.2.3 启动实例
[root@localhost ~]# redis-server /opt/6380/conf/redis.conf
[root@localhost ~]# redis-server /opt/6381/conf/redis.conf [root@localhost ~]# netstat -tunlp | grep redis
tcp 0 0 192.168.140.10:6380 0.0.0.0:* LISTEN 1331/redis-server 1
tcp 0 0 192.168.140.10:6381 0.0.0.0:* LISTEN 1336/redis-server 1
2.2.4 测试数据读写
[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6380
192.168.140.10:6380> set name martin
OK
192.168.140.10:6380> get name
"martin"
192.168.140.10:6380> exit[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
192.168.140.10:6381> set name demon
OK
192.168.140.10:6381> get name
"demon"
192.168.140.10:6381> exit
3、redis主从复制
在主从复制基础上,整合读写分离提升性能
从服务器默认为只读
基于异步的方式进行数据同步
支持一主多从
3.1 规划
192.168.140.10 6380 主
192.168.140.10 6381 从
3.2 从服务器配置
[root@localhost ~]# vim /opt/6381/conf/redis.conf
replicaof 192.168.140.10 6380
[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381 shutdown
[root@localhost ~]#
[root@localhost ~]# redis-server /opt/6381/conf/redis.conf
3.3 验证主从状态
[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6380
192.168.140.10:6380> set a 100
OK
192.168.140.10:6380> exit
[root@localhost ~]#
[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
192.168.140.10:6381> get a
"100"
192.168.140.10:6381>
192.168.140.10:6381> set b 20
(error) READONLY You can't write against a read only replica.
192.168.140.10:6381> exit
主服务器状态:
192.168.140.10:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.140.10,port=6381,state=online,offset=360,lag=0
master_replid:58d767e64932f28545096a5a20b217e9787fd0df
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:360
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:360
192.168.140.10:6380>
192.168.140.10:6380> exit
从服务器状态:
[root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
192.168.140.10:6381> info replication
# Replication
role:slave
master_host:192.168.140.10
master_port:6380
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:402
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:58d767e64932f28545096a5a20b217e9787fd0df
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:402
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:402
192.168.140.10:6381>
3.4 主从角色切换
将从服务器提升为主
192.168.140.10:6381> SLAVEOF no one
OK
192.168.140.10:6381>
192.168.140.10:6381> info replication
# Replication
role:master
connected_slaves:0
master_replid:87e2111823deba148396441304837a7bdd1d8399
master_replid2:58d767e64932f28545096a5a20b217e9787fd0df
master_repl_offset:584
second_repl_offset:585
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:584
192.168.140.10:6381>
192.168.140.10:6381> set b 20
OK
192.168.140.10:6381> set c 30
OK
192.168.140.10:6381> set d 30
OK
192.168.140.10:6381> 将配置文件中连接主服务器的信息注释、删除
4、分片集群
redis 3.x版本开始支持的
作用:所有缓存数据分散存储到不同的redis上,提升读写性能, 提升数据可靠性
4.1 原理
Redis集群使用哈希槽slot进行数据分片
Redis集群有16384个哈希槽, 每个key通过CRC16校验后对16384取模来决定放置哪个槽, 集群的每个节点负责一部分hash槽
举个例子,比如当前集群有3个节点,
定位数据的算法的优劣势:
优势:简单
劣势:随着集群扩容、缩容,会造成缓存数据丢失
4.2 分片集群的部署
192.168.140.10 3个集群模式的实例 7001 7002 7003
192.168.140.11 3个集群模式的实例 7004 7005 7006
4.2.1 两台服务器安装redis
4.2.2 配置redis实例
实例配置文件参考
bind 192.168.140.10
port 6380
daemonize yes
appendonly yes
pidfile /var/run/redis_6380.pid
loglevel warning
logfile "/var/log/redis_6380.log"
dbfilename dump_6380.rdb
dir /opt/6380/data
appendfilename "appendonly_6380.aof"cluster-enabled yes
cluster-config-file nodes-7001.conf
[root@localhost ~]# sed -ri 's|7001|7002|g' /opt/7002/conf/redis.conf
[root@localhost ~]# sed -ri 's|7001|7003|g' /opt/7003/conf/redis.conf
[root@localhost ~]# netstat -tunlp | grep redis
tcp 0 0 192.168.140.10:17001 0.0.0.0:* LISTEN 1683/redis-server 1
tcp 0 0 192.168.140.10:17002 0.0.0.0:* LISTEN 1688/redis-server 1
tcp 0 0 192.168.140.10:17003 0.0.0.0:* LISTEN 1693/redis-server 1
tcp 0 0 192.168.140.10:7001 0.0.0.0:* LISTEN 1683/redis-server 1
tcp 0 0 192.168.140.10:7002 0.0.0.0:* LISTEN 1688/redis-server 1
tcp 0 0 192.168.140.10:7003 0.0.0.0:* LISTEN 1693/redis-server 1 [root@localhost ~]# ps -elf | grep redis
5 S root 1683 1 0 80 0 - 39139 ep_pol 11:25 ? 00:00:00 redis-server 192.168.140.10:7001 [cluster]
5 S root 1688 1 0 80 0 - 39139 ep_pol 11:25 ? 00:00:00 redis-server 192.168.140.10:7002 [cluster]
5 S root 1693 1 0 80 0 - 39139 ep_pol 11:25 ? 00:00:00 redis-server 192.168.140.10:7003 [cluster]
0 S root 1701 1228 0 80 0 - 28203 pipe_w 11:27 pts/0 00:00:00 grep --color=auto redis
另外一台服务器配置大致相同 [root@localhost ~]# netstat -tunlp | grep redis
tcp 0 0 192.168.140.11:7004 0.0.0.0:* LISTEN 1471/redis-server 1
tcp 0 0 192.168.140.11:7005 0.0.0.0:* LISTEN 1476/redis-server 1
tcp 0 0 192.168.140.11:7006 0.0.0.0:* LISTEN 1481/redis-server 1
tcp 0 0 192.168.140.11:17004 0.0.0.0:* LISTEN 1471/redis-server 1
tcp 0 0 192.168.140.11:17005 0.0.0.0:* LISTEN 1476/redis-server 1
tcp 0 0 192.168.140.11:17006 0.0.0.0:* LISTEN 1481/redis-server 1
[root@localhost ~]#
[root@localhost ~]# ps -elf | grep redis
5 S root 1471 1 0 80 0 - 39139 ep_pol 11:33 ? 00:00:00 redis-server 192.168.140.11:7004 [cluster]
5 S root 1476 1 0 80 0 - 39139 ep_pol 11:33 ? 00:00:00 redis-server 192.168.140.11:7005 [cluster]
5 S root 1481 1 0 80 0 - 39139 ep_pol 11:33 ? 00:00:00 redis-server 192.168.140.11:7006 [cluster]
4.2.3 创建分片集群
[root@localhost ~]# redis-cli --cluster create \
> 192.168.140.10:7001 \
> 192.168.140.10:7002 \
> 192.168.140.10:7003 \
> 192.168.140.11:7004 \
> 192.168.140.11:7005 \
> 192.168.140.11:7006 \
> --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.140.11:7006 to 192.168.140.10:7001
Adding replica 192.168.140.10:7003 to 192.168.140.11:7004
Adding replica 192.168.140.11:7005 to 192.168.140.10:7002
M: ed73c1a4724e5961c9291148312f63b83e841e36 192.168.140.10:7001slots:[0-5460] (5461 slots) master
M: d51484815a84d7485bd191b3018b29987f3831b7 192.168.140.10:7002slots:[10923-16383] (5461 slots) master
S: df6f16e2c84aeda6efb60cf5ab98a7ea7928a37e 192.168.140.10:7003replicates 404b8acd3289b80973cafcf6738e079ed0866526
M: 404b8acd3289b80973cafcf6738e079ed0866526 192.168.140.11:7004slots:[5461-10922] (5462 slots) master
S: afc90762e411df0212880fcb4d151fbf84d205f1 192.168.140.11:7005replicates d51484815a84d7485bd191b3018b29987f3831b7
S: 7e3d9c1be77afa0c04c6bfc49056e793d56d575e 192.168.140.11:7006replicates ed73c1a4724e5961c9291148312f63b83e841e36
Can I set the above configuration? (type 'yes' to accept): yes
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]# redis-cli --cluster info 192.168.140.10:7001
192.168.140.10:7001 (ed73c1a4...) -> 0 keys | 5461 slots | 1 slaves.
192.168.140.10:7002 (d5148481...) -> 3 keys | 5461 slots | 1 slaves.
192.168.140.11:7004 (404b8acd...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
4.2.5 连接集群测试数据读写
[root@localhost ~]# redis-cli -h 192.168.140.11 -p 7004 -c
192.168.140.11:7004>
192.168.140.11:7004> set name martin
OK
192.168.140.11:7004> set url www.jd.com
-> Redirected to slot [12521] located at 192.168.140.10:7002
OK
5、哨兵集群
作用: 用于一主多从的环境下,目的是提升主服务器的可用性
6、Redis哨兵模式的配置
准备环境
主从环境,一主多从
至少三台哨兵节点(奇数个,避免脑裂)
主从节点配置
一个主redis实例,配置文件路径:/opt/6380/conf/redis.conf
两个从redis实例,配置文件路径:
/opt/6381/conf/redis.conf
/opt/6382/conf/redis.conf
redis实例配置文件修改见上述主从配置
启动主从节点并测试是否能够进行正常主从配置,测试成功后进行redis哨兵配置
哨兵节点配置
在三个redis目录下创建redis-sentinel目录,用于存放哨兵的配置文件
[root@localhost 6382]# mkdir -p /opt/6380/redis-sentinel
[root@localhost 6382]# mkdir -p /opt/6381/redis-sentinel
[root@localhost 6382]# mkdir -p /opt/6382/redis-sentinel
在data目录下创建三个存放哨兵文件的数据目录
[root@localhost ~]# mkdir -p /opt/6380/data/26380
[root@localhost ~]# mkdir -p /opt/6381/data/26381
[root@localhost ~]# mkdir -p /opt/6382/data/26382
将安装目录下的sentinel.conf文件分别复制到三个redis-sentinel目录下,重命名
[root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6380/redis-sentinel/sentinel-26380.conf
[root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6381/redis-sentinel/sentinel-26381.conf
[root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6382/redis-sentinel/sentinel-26382.conf
编辑sentinel.conf配置文件,主要修改端口、后台启动进程、pidfile、logfile、dir、主从节点地址等,具体可参考如下修改
sentinel-26380.conf
port 26380
daemonize yes
pidfile /var/run/redis-sentinel-26380.pid
logfile "/var/log/26380.log"
dir /opt/6380/data/26380
sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
sentinel-26381.conf
port 26381
daemonize yes
pidfile /var/run/redis-sentinel-26381.pid
logfile "/var/log/26381.log"
dir /opt/6381/data/26381
sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
sentinel-26382.conf
port 26382
daemonize yes
pidfile /var/run/redis-sentinel-26382.pid
logfile "/var/log/26382.log"
dir /opt/6382/data/26382
sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
启用哨兵
[root@localhost ~]# redis-sentinel /opt/6380/redis-sentinel/sentinel-26380.conf
[root@localhost ~]# redis-sentinel /opt/6381/redis-sentinel/sentinel-26381.conf
[root@localhost ~]# redis-sentinel /opt/6382/redis-sentinel/sentinel-26382.conf
启动sentienl,系统会在配置文件中自动写入哨兵的id、链接信息,主从节点信息和其他哨兵信息。如下sentinel-26380.conf启动后的完整信息:
[root@localhost ~]# cat /opt/6380/redis-sentinel/sentinel-26380.conf | grep -v '#' | grep -v '^$'
port 26380
daemonize yes
pidfile "/var/run/redis-sentinel-26380.pid"
logfile "/var/log/26380.log"
dir "/opt/6380/data/26380"
sentinel myid 72f9410b2bc741edaeed261286c0a6bf1242efb8 //哨兵的myid
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 191.168.140.10 6381 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
protected-mode no
sentinel current-epoch 0
关掉端口号为6381的redis主服务器程序
root 2412 0.0 1.4 167308 14368 ? Ssl 16:54 0:00 redis-server 192.168.140.10:6381
[root@localhost 6381]# kill -9 2412
端口号为6382的服务器成为了主服务器
[root@localhost 6381]# redis-cli -h 192.168.140.10 -p 6382
192.168.140.10:6382> info replication
# Replication
role:master
connected_slaves:0
master_replid:93c49286a4b921d25391e39db1f13a0122df7836
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0