redis的一个服务器模拟三个集群

主从复制

注意一个点:redis的启动在哪里?

/usr/local/bin

修改配置文件

-rw-r--r--. 1 root root 85573 4月  16 22:08 redis79.config
-rw-r--r--. 1 root root 85573 4月  16 22:10 redis80.config
-rw-r--r--. 1 root root 85573 4月  16 22:13 redis81.config

主修改

1.端口号

2.进程.pid文件

3.log日志文件

4.dump.rdb的持久化文件

一式三份 分别由一个redis-server来进行三次以不同的配置文件启动

[root@localhost bin]# redis-server wConfig/redis79.config 
[root@localhost bin]# redis-server wConfig/redis80.config 
[root@localhost bin]# redis-server wConfig/redis81.config
[root@localhost bin]# ps -ef|grep redis
root     22781     1  0 22:15 ?        00:00:00 redis-server 127.0.0.1:6380
root     22793     1  0 22:15 ?        00:00:00 redis-server 127.0.0.1:6381
root     22802 21175  0 22:15 pts/3    00:00:00 grep --color=auto redis
root     24336     1  0 15:22 ?        00:00:41 redis-server 127.0.0.1:6379
[root@localhost bin]#

启动三个客户端进行链接

[root@localhost bin]# redis-cli -p  6381
127.0.0.1:6381> ping
PONG
127.0.0.1:6381> 
[root@localhost bin]# 



[root@localhost bin]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG

[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

查看主从信息

127.0.0.1:6379> info replication
# Replication
role:master                   主机
connected_slaves:0
master_replid:01781e13fade93b90cf0836359b302d6b828f703
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
127.0.0.1:6379>
127.0.0.1:6380> info replication
# Replication
role:master                   主机
connected_slaves:0
master_replid:beb43bfdc22245f5ac686d41230c9300bfe71a34
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
127.0.0.1:6380>
127.0.0.1:6381> info replication
# Replication
role:master                      主机
connected_slaves:0
master_replid:8aec29ae8cf910e6049d7652b367d2d6a4c28d7e
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
127.0.0.1:6381>

默认三台都是主机

一般情况下只用配置从机 默认自己都是主机

认老大

设置一个主机两个从机

79 主 80 81 从

只需要配置从机就可

通过命令行的方式配置

slaveof  主机IP 主机端口号   表示要认哪一台的主机位主任 
127.0.0.1:6380> slaveof 127.0.0.1 6379
OK

127.0.0.1:6380> info replication
# Replication
role:slave          从机
master_host:127.0.0.1     主机IP
master_port:6379         主机端口
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:73954cfd79ecedd7a4461fb65720ffd35827a1aa
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:6380>

主机查看

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1   一个从机

从机IP  端口
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=1
master_replid:73954cfd79ecedd7a4461fb65720ffd35827a1aa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=84,lag=0
master_replid:73954cfd79ecedd7a4461fb65720ffd35827a1aa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84
127.0.0.1:6379>
127.0.0.1:6381> slaveof 127.0.0.1 6379
OK

127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:168
slave_priority:100
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=182,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=182,lag=0
master_replid:73954cfd79ecedd7a4461fb65720ffd35827a1aa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:182

从节点无法进行写入 只能读取

127.0.0.1:6381> set key2 v2
(error) READONLY You can't write against a read only replica.
127.0.0.1:6381>

主节点负责写入

但是我么们需要注意的是:

如果通过命令行配置的主从复制关系

我们的从机断开链接的时候 无法获得 群体数据

再次相连 则可获得数据 实现同步

我们从机第一次连接到主机的时候 主机的所有数据都可获得-----全量复制自动执行

我们Master后来的数据会自动的传送给从机 --实现增量复制

如果主机断开了,我可以使用

slaveof  no one

让自己成为主机

重点以上的一切都是使用命令行进行配置的(没有写入配置文件)

哨兵模式

自动选取老大(监控后台主机,投票选举主机)

配置哨兵信息

[root@localhost bin]# cd wConfig/
[root@localhost wConfig]# ll
总用量 336
-rw-r--r--. 1 root root 85573 4月  16 22:08 redis79.config
-rw-r--r--. 1 root root 85573 4月  16 22:10 redis80.config
-rw-r--r--. 1 root root 85573 4月  16 22:13 redis81.config
-rw-r--r--. 1 root root 85561 4月  10 13:28 redis.conf


[root@localhost wConfig]# vi sentinel.conf
[root@localhost wConfig]# cat sentinel.conf 
sentinel monitor myredis 127.0.0.1 6379 1
哨兵      监控             主机      端口  宕机后自选主机
[root@localhost wConfig]#

启动哨兵

[root@localhost bin]# redis-sentinel wConfig/sentinel.conf 
26127:X 17 Apr 2021 10:51:51.660 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26127:X 17 Apr 2021 10:51:51.660 # Redis version=6.0.12, bits=64, commit=00000000, modified=0, pid=26127, just started
26127:X 17 Apr 2021 10:51:51.660 # Configuration loaded
26127:X 17 Apr 2021 10:51:51.661 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.12 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 26127
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26127:X 17 Apr 2021 10:51:51.662 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26127:X 17 Apr 2021 10:51:51.664 # Sentinel ID is 670cfbbf35d7c9c1123ceb58c9db809bc9590d6d


当前主机
26127:X 17 Apr 2021 10:51:51.664 # +monitor master myredis 127.0.0.1 6379 quorum 1


当前从机
26127:X 17 Apr 2021 10:51:51.665 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 6379
26127:X 17 Apr 2021 10:51:51.668 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379

我们关闭主机的时候

127.0.0.1:6379> shutdown
not connected> exit
[root@localhost bin]#

我们的监视器哨兵来了

failover发现了主机的宕机

26127:X 17 Apr 2021 10:56:40.855 # +failover-end master myredis 127.0.0.1 6379
26127:X 17 Apr 2021 10:56:40.855 # +switch-master myredis 127.0.0.1 6379 127.0.0.1 6380
26127:X 17 Apr 2021 10:56:40.855 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6380
26127:X 17 Apr 2021 10:56:40.855 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380
26127:X 17 Apr 2021 10:57:10.868 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380

此时我们的从机信息

6380自动上位变成了主机

自动的变成了主机
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=27441,lag=0
master_replid:cf5fcaaed059a903610f87e9a583926b8920936b
master_replid2:73954cfd79ecedd7a4461fb65720ffd35827a1aa
master_repl_offset:27441
second_repl_offset:19067
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:27441
127.0.0.1:6380>

另一台从机 主动跟随

127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_

并且之前设置的数据都不会丢失

127.0.0.1:6380> get key1
"v1"
127.0.0.1:6380>

哨兵日志

26127:X 17 Apr 2021 10:57:10.868 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ myredis 127.0.0.1 6380

6379的主机宕机  我们的 6380上位变为主机

哨兵模式自动实现主从模式的升级,从手动到自动