3. redis常见部署架构

打印 上一主题 下一主题

主题 682|帖子 682|积分 2050

一、redis常见部署架构

1、常见部署架构

多实例部署
主从复制
分片集群
哨兵集群
2、多实例部署

安装目录: /opt/6380 端口: 6380
安装目录: /opt/6381 端口: 6381
2.1 规划安装目录、配置文件

  1. [root@localhost ~]# mkdir -p /opt/638{0,1}/{conf,data}
  2. [root@localhost ~]#
  3. [root@localhost ~]# cp /usr/local/redis50/conf/redis.conf /opt/6380/conf/
  4. [root@localhost ~]#
复制代码
2.2.2 编辑实例配置文件

  1. [root@localhost ~]# vim /opt/6380/conf/redis.conf
  2. bind 192.168.140.10
  3. port 6380
  4. daemonize yes
  5. appendonly yes
  6. pidfile /var/run/redis_6380.pid
  7. loglevel warning
  8. logfile "/var/log/redis_6380.log"
  9. dbfilename dump_6380.rdb
  10. dir /opt/6380/data
  11. appendfilename "appendonly_6380.aof"
复制代码
2.2.3 启动实例

  1. [root@localhost ~]# redis-server /opt/6380/conf/redis.conf
  2. [root@localhost ~]# redis-server /opt/6381/conf/redis.conf
  3. [root@localhost ~]# netstat -tunlp | grep redis
  4. tcp        0      0 192.168.140.10:6380     0.0.0.0:*               LISTEN      1331/redis-server 1
  5. tcp        0      0 192.168.140.10:6381     0.0.0.0:*               LISTEN      1336/redis-server 1
复制代码
2.2.4 测试数据读写

  1. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6380
  2. 192.168.140.10:6380> set name martin
  3. OK
  4. 192.168.140.10:6380> get name
  5. "martin"
  6. 192.168.140.10:6380> exit
  7. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
  8. 192.168.140.10:6381> set name demon
  9. OK
  10. 192.168.140.10:6381> get name
  11. "demon"
  12. 192.168.140.10:6381> exit
复制代码
3、redis主从复制

在主从复制基础上,整合读写分离提升性能
从服务器默以为只读
基于异步的方式举行数据同步
支持一主多从
3.1 规划

192.168.140.10 6380 主
192.168.140.10 6381 从
3.2 从服务器配置

  1. [root@localhost ~]# vim /opt/6381/conf/redis.conf
  2. replicaof 192.168.140.10 6380
复制代码
  1. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381 shutdown
  2. [root@localhost ~]#
  3. [root@localhost ~]# redis-server /opt/6381/conf/redis.conf
复制代码
3.3 验证主从状态

  1. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6380
  2. 192.168.140.10:6380> set a 100
  3. OK
  4. 192.168.140.10:6380> exit
  5. [root@localhost ~]#
  6. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
  7. 192.168.140.10:6381> get a
  8. "100"
  9. 192.168.140.10:6381>
  10. 192.168.140.10:6381> set b 20
  11. (error) READONLY You can't write against a read only replica.
  12. 192.168.140.10:6381> exit
复制代码
主服务器状态:
  1. 192.168.140.10:6380> info replication
  2. # Replication
  3. role:master
  4. connected_slaves:1
  5. slave0:ip=192.168.140.10,port=6381,state=online,offset=360,lag=0
  6. master_replid:58d767e64932f28545096a5a20b217e9787fd0df
  7. master_replid2:0000000000000000000000000000000000000000
  8. master_repl_offset:360
  9. second_repl_offset:-1
  10. repl_backlog_active:1
  11. repl_backlog_size:1048576
  12. repl_backlog_first_byte_offset:1
  13. repl_backlog_histlen:360
  14. 192.168.140.10:6380>
  15. 192.168.140.10:6380> exit
复制代码
从服务器状态:
  1. [root@localhost ~]# redis-cli -h 192.168.140.10 -p 6381
  2. 192.168.140.10:6381> info replication
  3. # Replication
  4. role:slave
  5. master_host:192.168.140.10
  6. master_port:6380
  7. master_link_status:up
  8. master_last_io_seconds_ago:8
  9. master_sync_in_progress:0
  10. slave_repl_offset:402
  11. slave_priority:100
  12. slave_read_only:1
  13. connected_slaves:0
  14. master_replid:58d767e64932f28545096a5a20b217e9787fd0df
  15. master_replid2:0000000000000000000000000000000000000000
  16. master_repl_offset:402
  17. second_repl_offset:-1
  18. repl_backlog_active:1
  19. repl_backlog_size:1048576
  20. repl_backlog_first_byte_offset:1
  21. repl_backlog_histlen:402
  22. 192.168.140.10:6381>
复制代码
3.4 主从角色切换

将从服务器提升为主
  1. 192.168.140.10:6381> SLAVEOF no one
  2. OK
  3. 192.168.140.10:6381>
  4. 192.168.140.10:6381> info replication
  5. # Replication
  6. role:master
  7. connected_slaves:0
  8. master_replid:87e2111823deba148396441304837a7bdd1d8399
  9. master_replid2:58d767e64932f28545096a5a20b217e9787fd0df
  10. master_repl_offset:584
  11. second_repl_offset:585
  12. repl_backlog_active:1
  13. repl_backlog_size:1048576
  14. repl_backlog_first_byte_offset:1
  15. repl_backlog_histlen:584
  16. 192.168.140.10:6381>
  17. 192.168.140.10:6381> set b 20
  18. OK
  19. 192.168.140.10:6381> set c 30
  20. OK
  21. 192.168.140.10:6381> set d 30
  22. OK
  23. 192.168.140.10:6381>
  24. 将配置文件中连接主服务器的信息注释、删除
复制代码
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实例

实例配置文件参考
  1. bind 192.168.140.10
  2. port 6380
  3. daemonize yes
  4. appendonly yes
  5. pidfile /var/run/redis_6380.pid
  6. loglevel warning
  7. logfile "/var/log/redis_6380.log"
  8. dbfilename dump_6380.rdb
  9. dir /opt/6380/data
  10. appendfilename "appendonly_6380.aof"
  11. cluster-enabled yes
  12. cluster-config-file nodes-7001.conf
复制代码
  1. [root@localhost ~]# sed -ri 's|7001|7002|g' /opt/7002/conf/redis.conf
  2. [root@localhost ~]# sed -ri 's|7001|7003|g' /opt/7003/conf/redis.conf
复制代码
  1. [root@localhost ~]# netstat -tunlp | grep redis
  2. tcp        0      0 192.168.140.10:17001    0.0.0.0:*               LISTEN      1683/redis-server 1
  3. tcp        0      0 192.168.140.10:17002    0.0.0.0:*               LISTEN      1688/redis-server 1
  4. tcp        0      0 192.168.140.10:17003    0.0.0.0:*               LISTEN      1693/redis-server 1
  5. tcp        0      0 192.168.140.10:7001     0.0.0.0:*               LISTEN      1683/redis-server 1
  6. tcp        0      0 192.168.140.10:7002     0.0.0.0:*               LISTEN      1688/redis-server 1
  7. tcp        0      0 192.168.140.10:7003     0.0.0.0:*               LISTEN      1693/redis-server 1
  8. [root@localhost ~]# ps -elf | grep redis
  9. 5 S root       1683      1  0  80   0 - 39139 ep_pol 11:25 ?        00:00:00 redis-server 192.168.140.10:7001 [cluster]
  10. 5 S root       1688      1  0  80   0 - 39139 ep_pol 11:25 ?        00:00:00 redis-server 192.168.140.10:7002 [cluster]
  11. 5 S root       1693      1  0  80   0 - 39139 ep_pol 11:25 ?        00:00:00 redis-server 192.168.140.10:7003 [cluster]
  12. 0 S root       1701   1228  0  80   0 - 28203 pipe_w 11:27 pts/0    00:00:00 grep --color=auto redis
复制代码
  1. 另外一台服务器配置大致相同
  2. [root@localhost ~]# netstat -tunlp | grep redis
  3. tcp        0      0 192.168.140.11:7004     0.0.0.0:*               LISTEN      1471/redis-server 1
  4. tcp        0      0 192.168.140.11:7005     0.0.0.0:*               LISTEN      1476/redis-server 1
  5. tcp        0      0 192.168.140.11:7006     0.0.0.0:*               LISTEN      1481/redis-server 1
  6. tcp        0      0 192.168.140.11:17004    0.0.0.0:*               LISTEN      1471/redis-server 1
  7. tcp        0      0 192.168.140.11:17005    0.0.0.0:*               LISTEN      1476/redis-server 1
  8. tcp        0      0 192.168.140.11:17006    0.0.0.0:*               LISTEN      1481/redis-server 1
  9. [root@localhost ~]#
  10. [root@localhost ~]# ps -elf | grep redis
  11. 5 S root       1471      1  0  80   0 - 39139 ep_pol 11:33 ?        00:00:00 redis-server 192.168.140.11:7004 [cluster]
  12. 5 S root       1476      1  0  80   0 - 39139 ep_pol 11:33 ?        00:00:00 redis-server 192.168.140.11:7005 [cluster]
  13. 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 创建分片集群

  1. [root@localhost ~]# redis-cli --cluster create \
  2. > 192.168.140.10:7001 \
  3. > 192.168.140.10:7002 \
  4. > 192.168.140.10:7003 \
  5. > 192.168.140.11:7004 \
  6. > 192.168.140.11:7005 \
  7. > 192.168.140.11:7006 \
  8. > --cluster-replicas 1
  9. >>> Performing hash slots allocation on 6 nodes...
  10. Master[0] -> Slots 0 - 5460
  11. Master[1] -> Slots 5461 - 10922
  12. Master[2] -> Slots 10923 - 16383
  13. Adding replica 192.168.140.11:7006 to 192.168.140.10:7001
  14. Adding replica 192.168.140.10:7003 to 192.168.140.11:7004
  15. Adding replica 192.168.140.11:7005 to 192.168.140.10:7002
  16. M: ed73c1a4724e5961c9291148312f63b83e841e36 192.168.140.10:7001
  17.    slots:[0-5460] (5461 slots) master
  18. M: d51484815a84d7485bd191b3018b29987f3831b7 192.168.140.10:7002
  19.    slots:[10923-16383] (5461 slots) master
  20. S: df6f16e2c84aeda6efb60cf5ab98a7ea7928a37e 192.168.140.10:7003
  21.    replicates 404b8acd3289b80973cafcf6738e079ed0866526
  22. M: 404b8acd3289b80973cafcf6738e079ed0866526 192.168.140.11:7004
  23.    slots:[5461-10922] (5462 slots) master
  24. S: afc90762e411df0212880fcb4d151fbf84d205f1 192.168.140.11:7005
  25.    replicates d51484815a84d7485bd191b3018b29987f3831b7
  26. S: 7e3d9c1be77afa0c04c6bfc49056e793d56d575e 192.168.140.11:7006
  27.    replicates ed73c1a4724e5961c9291148312f63b83e841e36
  28. Can I set the above configuration? (type 'yes' to accept): yes
  29. [OK] All nodes agree about slots configuration.
  30. >>> Check for open slots...
  31. >>> Check slots coverage...
  32. [OK] All 16384 slots covered.
复制代码
  1. [root@localhost ~]# redis-cli --cluster info 192.168.140.10:7001
  2. 192.168.140.10:7001 (ed73c1a4...) -> 0 keys | 5461 slots | 1 slaves.
  3. 192.168.140.10:7002 (d5148481...) -> 3 keys | 5461 slots | 1 slaves.
  4. 192.168.140.11:7004 (404b8acd...) -> 1 keys | 5462 slots | 1 slaves.
  5. [OK] 4 keys in 3 masters.
  6. 0.00 keys per slot on average.
复制代码
4.2.5 毗连集群测试数据读写

  1. [root@localhost ~]# redis-cli -h 192.168.140.11 -p 7004 -c
  2. 192.168.140.11:7004>
  3. 192.168.140.11:7004> set name martin
  4. OK
  5. 192.168.140.11:7004> set url www.jd.com
  6. -> Redirected to slot [12521] located at 192.168.140.10:7002
  7. 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目录,用于存放哨兵的配置文件
  1. [root@localhost 6382]# mkdir -p /opt/6380/redis-sentinel
  2. [root@localhost 6382]# mkdir -p /opt/6381/redis-sentinel
  3. [root@localhost 6382]# mkdir -p /opt/6382/redis-sentinel
复制代码
在data目录下创建三个存放哨兵文件的数据目录
  1. [root@localhost ~]# mkdir -p /opt/6380/data/26380
  2. [root@localhost ~]# mkdir -p /opt/6381/data/26381
  3. [root@localhost ~]# mkdir -p /opt/6382/data/26382
复制代码
将安装目录下的sentinel.conf文件分别复制到三个redis-sentinel目录下,重命名
  1. [root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6380/redis-sentinel/sentinel-26380.conf
  2. [root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6381/redis-sentinel/sentinel-26381.conf
  3. [root@localhost ~]# cp redis-5.0.12/sentinel.conf /opt/6382/redis-sentinel/sentinel-26382.conf
复制代码
编辑sentinel.conf配置文件,主要修改端口、背景启动进程、pidfile、logfile、dir、主从节点地址等,具体可参考如下修改
sentinel-26380.conf
  1. port 26380
  2. daemonize yes
  3. pidfile /var/run/redis-sentinel-26380.pid
  4. logfile "/var/log/26380.log"
  5. dir /opt/6380/data/26380
  6. sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
复制代码
sentinel-26381.conf
  1. port 26381
  2. daemonize yes
  3. pidfile /var/run/redis-sentinel-26381.pid
  4. logfile "/var/log/26381.log"
  5. dir /opt/6381/data/26381
  6. sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
复制代码
sentinel-26382.conf
  1. port 26382
  2. daemonize yes
  3. pidfile /var/run/redis-sentinel-26382.pid
  4. logfile "/var/log/26382.log"
  5. dir /opt/6382/data/26382
  6. sentinel monitor mymaster 191.168.140.10 6381 2 //这里的地址和端口号写主服务器的
复制代码
启用哨兵

  1. [root@localhost ~]# redis-sentinel /opt/6380/redis-sentinel/sentinel-26380.conf
  2. [root@localhost ~]# redis-sentinel /opt/6381/redis-sentinel/sentinel-26381.conf
  3. [root@localhost ~]# redis-sentinel /opt/6382/redis-sentinel/sentinel-26382.conf
复制代码
启动sentienl,系统会在配置文件中主动写入哨兵的id、链接信息,主从节点信息和其他哨兵信息。如下sentinel-26380.conf启动后的完整信息:

  1. [root@localhost ~]# cat /opt/6380/redis-sentinel/sentinel-26380.conf | grep -v '#' | grep -v '^$'
  2. port 26380
  3. daemonize yes
  4. pidfile "/var/run/redis-sentinel-26380.pid"
  5. logfile "/var/log/26380.log"
  6. dir "/opt/6380/data/26380"
  7. sentinel myid 72f9410b2bc741edaeed261286c0a6bf1242efb8 //哨兵的myid
  8. sentinel deny-scripts-reconfig yes
  9. sentinel monitor mymaster 191.168.140.10 6381 2
  10. sentinel config-epoch mymaster 0
  11. sentinel leader-epoch mymaster 0
  12. protected-mode no
  13. sentinel current-epoch 0
复制代码
关掉端口号为6381的redis主服务器程序

  1. root       2412  0.0  1.4 167308 14368 ?        Ssl  16:54   0:00 redis-server 192.168.140.10:6381
  2. [root@localhost 6381]# kill -9 2412
复制代码
端口号为6382的服务器成为了主服务器

  1. [root@localhost 6381]# redis-cli -h 192.168.140.10 -p 6382
  2. 192.168.140.10:6382> info replication
  3. # Replication
  4. role:master
  5. connected_slaves:0
  6. master_replid:93c49286a4b921d25391e39db1f13a0122df7836
  7. master_replid2:0000000000000000000000000000000000000000
  8. master_repl_offset:0
  9. second_repl_offset:-1
  10. repl_backlog_active:0
  11. repl_backlog_size:1048576
  12. repl_backlog_first_byte_offset:0
  13. repl_backlog_histlen:0
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王國慶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表