前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis的实际操作部署模式(一)

redis的实际操作部署模式(一)

作者头像
灰子学技术
发布2020-11-09 11:20:05
3690
发布2020-11-09 11:20:05
举报
文章被收录于专栏:灰子学技术灰子学技术

写在前面的话:

对于redis来说,它有四种部署模式,分别是单机模式、主从模式、哨兵模式和集群模式,他们的使用场景有些区别,当然也是越来越复杂,可靠性越来越高。

本文从实际操作的角度,来介绍和讲解下,这几种模式的特点,鉴于篇幅的问题,文章分成两篇,一篇用来介绍:单机模式、主从模式和哨兵模式;另外一篇用来介绍:集群模式。

一、单机部署:

下面是单机部署的操作步骤。

// redis-server 没有启动的情况下,直接连接 redis-cli的结果
$ redis-cli -h 127.0.0.1 -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

1.通过redis-server /usr/local/etc/redis.conf 启动默认的6379redis服务

// redis-server 服务启动
$ redis-server /usr/local/etc/redis.conf
5286:C 03 Nov 2020 18:35:17.905 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5286:C 03 Nov 2020 18:35:17.905 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=5286, just started
5286:C 03 Nov 2020 18:35:17.905 # Configuration loaded
5286:M 03 Nov 2020 18:35:17.907 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5286
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

5286:M 03 Nov 2020 18:35:17.909 # Server initialized
5286:M 03 Nov 2020 18:35:17.909 * DB loaded from disk: 0.001 seconds
5286:M 03 Nov 2020 18:35:17.909 * Ready to accept connections

2.通过redis-cli去验证redis-server是否连接成功,下面对一些命令做了简要绍明

// redis-cli 启动成功
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379>
127.0.0.1:6379> ping // 检测连接是不是好的
PONG
127.0.0.1:6379> ping "hello the world" // 也可以发送指定消息
"hello the world"
127.0.0.1:6379>
127.0.0.1:6379> ping // 断开redis-server之后,ping的话就提示下面的错误
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
not connected> ping // 此刻redis-server又被启动了起来
PONG
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379>  ttl key1
(integer) -1
127.0.0.1:6379> set key1 value1 EX 1000
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> ttl key1
(integer) 990
127.0.0.1:6379> ttl key1
(integer) 984
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> get key1
备注: 其他命令实际操作,可以参考:http://redisdoc.com/

特点:

优点:
架构简单,部署方便;性价比较高,不需要额外的机器做备份;
缺点:
存储数据不可靠,在服务器重启,挂掉的时候,很容易丢数据,并且一旦服务器重启就会影响业务。

二、主从部署:

操作步骤如下所示:

1.把配置文件redis.conf复制一份为redis6380.conf,然后修改里面的port为6380,并启动主、从服务器。

// 把配置文件redis.conf复制一份为redis6380.conf,然后修改里面的port为6380;
// 从服务器启动
$ redis-server /usr/local/etc/redis6380.conf --slaveof 127.0.0.1 6379
5404:C 03 Nov 2020 19:01:54.736 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5404:C 03 Nov 2020 19:01:54.736 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=5404, just started
5404:C 03 Nov 2020 19:01:54.736 # Configuration loaded
5404:S 03 Nov 2020 19:01:54.737 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 5404
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

5404:S 03 Nov 2020 19:01:54.738 # Server initialized
5404:S 03 Nov 2020 19:01:54.738 * DB loaded from disk: 0.000 seconds
5404:S 03 Nov 2020 19:01:54.738 * Ready to accept connections
5404:S 03 Nov 2020 19:01:54.739 * Connecting to MASTER 127.0.0.1:6379
5404:S 03 Nov 2020 19:01:54.739 * MASTER <-> REPLICA sync started
5404:S 03 Nov 2020 19:01:54.744 * Non blocking connect for SYNC fired the event.
5404:S 03 Nov 2020 19:01:54.745 * Master replied to PING, replication can continue...
5404:S 03 Nov 2020 19:01:54.745 * Partial resynchronization not possible (no cached master)
5404:S 03 Nov 2020 19:01:54.747 * Full resync from master: 388efa1adb1a8e684f98f70a3ce4ccf45be0fce2:0
5404:S 03 Nov 2020 19:01:54.763 * MASTER <-> REPLICA sync: receiving 274 bytes from master
5404:S 03 Nov 2020 19:01:54.764 * MASTER <-> REPLICA sync: Flushing old data
5404:S 03 Nov 2020 19:01:54.764 * MASTER <-> REPLICA sync: Loading DB in memory
5404:S 03 Nov 2020 19:01:54.764 * MASTER <-> REPLICA sync: Finished with success

2.通过redis-cli来检测主从服务器的角色和详情

// 检测主从是不是设置成功
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1 // 下面便是从服务器的信息
slave0:ip=127.0.0.1,port=6380,state=online,offset=14,lag=1
master_replid:388efa1adb1a8e684f98f70a3ce4ccf45be0fce2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
127.0.0.1:6379>
// 检测从服务器信息:
$ redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:slave // 角色是从服务器
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:448
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:388efa1adb1a8e684f98f70a3ce4ccf45be0fce2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:448
127.0.0.1:6380>

3. 主服务器可以读写,从服务器只支持读操作

// 主服务器的读写操作
127.0.0.1:6379> set hello world EX 1000
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> ttl hello
(integer) 974
127.0.0.1:6379>
// 从服务器的读操作可以,写操作不行
127.0.0.1:6380> set hello world EX 5
(error) READONLY You can't write against a read only replica.
127.0.0.1:6380> get hello
"world"
127.0.0.1:6380> ttl hello
(integer) 980
127.0.0.1:6380>

4. 主服务器断开之后,从服务器需要手动操作才能变成主服务器。

127.0.0.1:6379> info replication // 断开主服务器
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
127.0.0.1:6380> info replication // 从服务器角色并没有变化
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1102
master_link_down_since_seconds:19
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:388efa1adb1a8e684f98f70a3ce4ccf45be0fce2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1102
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1102
127.0.0.1:6380>
127.0.0.1:6380> slaveof no one // 执行命令让从服务器成为主服务器
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_replid:0b2092a9ac8707a4c8b93cddb3e8e4c5dd91793e
master_replid2:388efa1adb1a8e684f98f70a3ce4ccf45be0fce2
master_repl_offset:1102
second_repl_offset:1103
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1102
127.0.0.1:6380>

特点:

优点:
1.master能自动将数据同步到slave,可以进行读写分离,分担master的读压力;
2.master、slave之间的同步是以非阻塞的方式进行的,同步期间,客户端仍然可以提交查询或更新请求。
缺点: 
1.不具备自动容错与恢复功能,master或slave的宕机都可能导致客户端请求失败,需要等待机器重启或手动切换客户端IP才能恢复。 
2.master宕机,如果宕机前数据没有同步完,则切换IP后会存在数据不一致的问题。 
3.难以支持在线扩容,Redis的容量受限于单机配置。 

三、哨兵模式

操作步骤如下所示:

1.按照主从模式的方式启动主、从服务器,这里参考“主从模式”章节

2.启动哨兵配置,redis-server /usr/local/etc/redis-sentinel.conf --sentinel来启动。

// 启动哨兵
$ redis-server /usr/local/etc/redis-sentinel.conf --sentinel
5554:X 03 Nov 2020 19:27:41.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5554:X 03 Nov 2020 19:27:41.756 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=5554, just started
5554:X 03 Nov 2020 19:27:41.756 # Configuration loaded
5554:X 03 Nov 2020 19:27:41.758 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 5554
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

5554:X 03 Nov 2020 19:27:41.762 # Sentinel ID is 3eb0b1fcec3fb6513e1f062d94773788e8ea8da0
5554:X 03 Nov 2020 19:27:41.762 # +monitor master mymaster 127.0.0.1 6379 quorum 1
5554:X 03 Nov 2020 19:27:41.764 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379

// 检查启动的redis相关的服务
$ ps aux |grep redis
zhanghui          5554   0.1  0.0  4309788   2332 s010  S+    7:27下午   0:00.32 redis-server *:26379 [sentinel]
zhanghui          5576   0.0  0.0  4268040    784 s007  S+    7:30下午   0:00.00 grep redis
zhanghui          5550   0.0  0.0  4310812   2424 s009  S+    7:26下午   0:00.26 redis-server 127.0.0.1:6380
zhanghui          5549   0.0  0.0  4310768   2332 s008  S+    7:26下午   0:00.27 redis-server 127.0.0.1:6379

3.查看6379、6380这两个主、从服务器的的角色

// 主服务器master挂掉之后,从服务器替换成master服务器
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_replid:a2e8afcf20f3ced0f1c516b30650b665d9f0d648
master_replid2:63ed5bfc919f8067a2d7565ddd60a2dbeea3693e
master_repl_offset:24304
second_repl_offset:23351
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:24304
127.0.0.1:6380>

4.主服务器挂机之后,从服务器会主动变成主服务器,如下所示;而挂掉的那个服务器再次启动之后,会变成从服务器。

// 挂掉的服务器重启之后,自动变成了slave服务器
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:33987
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a2e8afcf20f3ced0f1c516b30650b665d9f0d648
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:33987
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29891
repl_backlog_histlen:4097
127.0.0.1:6379>

特点

优点:
哨兵模式是基于主从模式设计出来的,所以它具有主从服务模式的优点;除此之外,哨兵模式下,master挂掉可以自动进行切换,系统可用性更高。
缺点:
1.继承了主从模式难以在线扩容的缺点,Redis的容量受限于单机配置。
2.需要额外的资源来启动sentinel进程,实现相对复杂一点,同时slave节点作为备份节点不提供服务。

(备注:集群模式参考后面的文章。)

参考资料:

redis的几种模式:https://www.cnblogs.com/jing99/p/12651186.html
client的几种连接方式:https://blog.csdn.net/comprel/article/details/96716708
哨兵模式:https://www.cnblogs.com/yangming1996/p/12677771.html
redis官方文档: https://godoc.org/gopkg.in/redis.v5
redis安装:https://juejin.im/post/6844904002270789645
redis-cli命令:https://segmentfault.com/a/1190000022713251
redis命令:http://redisdoc.com/string/mset.html
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-11-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 灰子学技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档