Redis主从复制和哨兵安装

Redis主从复制和哨兵安装

𝓓𝓸𝓷 Lv6

Centos7.9安装Redis

一、下载

下载地址:http://redis.io/download,下载最新稳定版本。

二、配置YUM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mkdir /opt/yum
cp -r /run/media/admin/CentOS\ 7\ x86_64/* /opt/yum
yum clean all
yum makecache


---配置本地YUM:

cat > /etc/yum.repos.d/CentOS-Base.repo <<EOF

[base]

name=CentOS-\$releasever - Base
baseurl=file:///opt/yum
gpgcheck=0
enabled=1

EOF
三、安装操作系统依赖包
1
yum -y install gcc tcl gcc-c++ libstdc++-devel zlib-devel
四、配置操作系统环境
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
(1)创建redis用户及目录
groupadd -g 2000 redis
useradd -u 2000 -g 2000 redis

(2)设置redis ulimit
cat >> /etc/security/limits.conf <<EOF

redis hard nofile 10240
redis soft nofile 10240
redis hard nproc 8192
redis soft nproc 8192

EOF

(3)设置内核参数
vim /etc/sysctl.conf

vm.overcommit_memory = 1
vm.swappiness = 0
net.core.somaxconn=65535
net.ipv4.tcp_max_syn_backlog=65535
net.core.somaxconn = 2048

sysctl -p

(4)关闭Transparent Huge Page(THP)
sed -i 's/quiet/quiet transparent_hugepage=never/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot

(5)关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service

(6)关闭Selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config

setenforce 0
getenforce

(7)关闭avahi-daemon
ps -ef|grep avahi-daemon

systemctl stop avahi-daemon.socket
systemctl stop avahi-daemon.service

systemctl disable avahi-daemon.socket
systemctl disable avahi-daemon.service

(8)禁用chronyd
systemctl status chronyd

systemctl stop chronyd
systemctl disable chronyd
mv /etc/chrony.conf /etc/chrony.conf.org

(9)关闭ZEROCONF
配置NOZEROCONF:
cat >> /etc/sysconfig/network <<EOF

NOZEROCONF=yes

EOF
五、主从同步安装
1.主从环境
HostName IP Role
master 192.168.1.130 master
slave01 192.168.1.131 slave
slave02 192.168.1.132 slave
2.安装

主从三台节点都需要安装

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
方法一:
# cd /opt/soft
# tar xvf redis-6.2.14.tar.gz
# cd redis-6.2.14/
# make
# cd src/
# make PREFIX=/usr/local/redis install

---批量
cd /opt/soft && tar xvf redis-*.tar.gz && cd redis-* && make && cd src && make PREFIX=/usr/local/redis install && echo $?

---注意:PREFIX必须大写





方法二:
# tar xvf redis-6.0.10.tar.gz -C /usr/local/
# cd /usr/local/
# ln -s redis-6.0.10 redis
# cd redis
# make
# cd redis/src
# make PREFIX=/usr/local/redis install
# make test

PREFIX: 指定程序存放的路径,如果不使用PREFIX,Linux默认将可执行文件存放在/usr/local/bin目录,
库文件会存放在/usr/local/lib目录。配置文件会存放在/usr/local/etc目录。其他的资源文件会存放在usr/local/share目录。
里指定号目录也方便后续的卸载,后续直接rm -rf /usr/local/redis 即可删除redis。


方法三:

# wget https://download.redis.io/releases/redis-6.2.4.tar.gz
# tar zxvf redis-6.2.4.tar.gz -C /usr/local/
# ln -s /usr/local/redis-6.2.4/ /usr/local/redis
# cd /usr/local/redis
# make
# cd src/
# make install
# make test
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# cp /opt/soft/redis-6.2.14/redis.conf /etc/redis/


---master主节点创建配置文件:
# cat >> /etc/redis/redis.conf <<EOF

port 1314
bind 192.168.1.130
daemonize yes
pidfile /var/run/redis/redis.pid
loglevel notice
logfile "/usr/local/redis/redis.log"
dbfilename dump.rdb
dir /usr/local/redis
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
requirepass 123456


timeout 300
masterauth 123456
min-slaves-to-write 1
min-slaves-max-lag 3
slave-read-only yes

EOF



---参数详解:
min-slaves-to-write 1 ---主从数据一致性保证,至少有一个从节点写副本成功
min-slaves-max-lag 3 ---主从数据一致性保证,超时延迟不能超过3秒

注意: 当主节点只有一个从节点,并且需要停止从节点对从节点维护时,这里需要先在主节点上设置config set min-slaves-to-write 0,再对从节点进行维护,否则主节点会进入只读状态

---建议RDB和AOF同时开通
---开通AOF
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

---开通RDB
dbfilename dump.rdb
dir /var/redis/1314



---slave01从节点创建配置文件:
# cat >> /etc/redis/redis.conf <<EOF

port 1314
bind 192.168.1.131
daemonize yes
pidfile /var/run/redis/redis.pid
loglevel notice
logfile "/usr/local/redis/redis.log"
dbfilename dump.rdb
dir /usr/local/redis
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
requirepass 123456


timeout 300
masterauth 123456
min-slaves-to-write 1
min-slaves-max-lag 3
slaveof 192.168.1.130 1314
slave-read-only yes

EOF




---slave02从节点创建配置文件:
# cat >> /etc/redis/redis.conf <<EOF

port 1314
bind 192.168.1.132
daemonize yes
pidfile /var/run/redis/redis.pid
loglevel notice
logfile "/usr/local/redis/redis.log"
dbfilename dump.rdb
dir /usr/local/redis
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
requirepass 123456


timeout 300
masterauth 123456
min-slaves-to-write 1
min-slaves-max-lag 3
slaveof 192.168.1.130 1314
slave-read-only yes

EOF
4.授权
1
2
chown -R redis.redis /etc/redis
chown -R redis.redis /usr/local/redis
5.配置PATH环境变量
1
2
3
4
5
6
7
8
9
10
11
12
# su - redis
$ vim .bash_profile
export PATH=/usr/local/redis/bin:$PATH

或者
cat >> .bash_profile << EOF

export PATH=/usr/local/redis/bin:\$PATH

EOF

$ source .bash_profile
6.启动和停止redis
1
2
3
4
5
6
7
8
9
10
11
# su - redis
$ redis-server /etc/redis/redis.conf

---查看redis进程
$ ps -ef|grep redis-server
redis 7211 1 0 15:59 ? 00:00:00 redis-server 192.168.1.130:1314
redis 7237 7066 0 16:00 pts/0 00:00:00 grep --color=auto redis-server

---停止redis
$ redis-cli -h 192.168.1.130 -p 1314 -a 123456 shutdown

7.登录验证主从同步
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
---master主库:
[redis@master ~]$ redis-cli -h 192.168.1.130 -p 1314
192.168.1.130:1314> auth 123456
OK
192.168.1.130:1314> keys *
(empty array)
192.168.1.130:1314> set bar 1
OK



192.168.1.130:1314> client list
id=3 addr=192.168.1.130:37718 laddr=192.168.1.130:1314 fd=8 name= age=54 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=40928 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 events=r cmd=client user=default redir=-1
192.168.1.130:1314>


---slave01从库:
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 keys *
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "bar"

---slave02从库:
[redis@slave02 ~]$ redis-cli -p 1314 -h 192.168.1.132 -a 123456 keys *
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "bar"
8.查看同步状态
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
68
69
70
71
72
73
74
75
76
77
[redis@master ~]$  redis-cli -p 1314 -h 192.168.1.130 -a 123456 info replication 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.131,port=1314,state=online,offset=794,lag=1
slave1:ip=192.168.1.132,port=1314,state=online,offset=794,lag=1
master_failover_state:no-failover
master_replid:59b5b22fbb6688cc6b3bda5b6958c92c0b289b53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:794
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:794



[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.130
master_port:1314
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_read_repl_offset:822
slave_repl_offset:822
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
min_slaves_good_slaves:0
master_failover_state:no-failover
master_replid:59b5b22fbb6688cc6b3bda5b6958c92c0b289b53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:822
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:822



[redis@slave02 ~]$ redis-cli -p 1314 -h 192.168.1.132 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.130
master_port:1314
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:850
slave_repl_offset:850
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
min_slaves_good_slaves:0
master_failover_state:no-failover
master_replid:59b5b22fbb6688cc6b3bda5b6958c92c0b289b53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:850
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:822





9.配置redis开机启动
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
# cat >> /etc/systemd/system/redis.service  <<EOF

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/redis/redis.pid
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
User=redis
Group=redis

[Install]
WantedBy=multi-user.target

EOF





加载服务:
# systemctl daemon-reload
# systemctl enable redis
# systemctl start redis
# systemctl status redis
---停止服务
# systemctl stop redis



或者
vim /etc/rc.local
su - redis -c /usr/local/redis/bin/redis-server /etc/redis/redis.conf

chmod 777 /etc/rc.local
10.主从故障切换
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---主节点故障,需要将其中的从配置成主
(1).主库模拟关机
[root@master ~]# shutdown -h now

(2).修改其中一切从库为主库,例如:slave01切换成主库
1.查看从库同步状态
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.130
master_port:1314
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:2530
slave_repl_offset:2530
master_link_down_since_seconds:195
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
min_slaves_good_slaves:0
master_failover_state:no-failover
master_replid:59b5b22fbb6688cc6b3bda5b6958c92c0b289b53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2530
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2530

2.停止从库与主库的同步(slaveof no one)
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 slaveof no one
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:0
min_slaves_good_slaves:0
master_failover_state:no-failover
master_replid:4282c3f873d68f3644019bc06ffb65f8ebf53c0c
master_replid2:59b5b22fbb6688cc6b3bda5b6958c92c0b289b53
master_repl_offset:2530
second_repl_offset:2531
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2530



3.修改新主的配置文件
[redis@slave01 ~]$ vim /etc/redis/redis.conf
#slaveof 192.168.1.130 1314

4.重启新主
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

[redis@slave01 ~]$ redis-server /etc/redis/redis.conf


5.修改从库slave02指向
[redis@slave02 ~]$ redis-cli -p 1314 -h 192.168.1.132 -a 123456 slaveof 192.168.1.131 1314
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK


新主查询同步状态:
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=192.168.1.132,port=1314,state=online,offset=56,lag=0
master_failover_state:no-failover
master_replid:9aca20f93d39d5f35af8699ec1edb4ea73381a51
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70


如果旧的主节点成功后,将旧的主节点slave01添加进来成为从:
7.修改旧主节点参数
[redis@master ~]$ vim /etc/redis/redis.conf
slaveof 192.168.1.131 1314

8.重启旧的主节点
[redis@master ~]$ redis-cli -p 1314 -h 192.168.1.130 -a 123456 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

[redis@master ~]$ redis-server /etc/redis/redis.conf


9.查看同步状态
[redis@slave01 ~]$ redis-cli -p 1314 -h 192.168.1.131 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.1.132,port=1314,state=online,offset=1036,lag=0
slave1:ip=192.168.1.130,port=1314,state=online,offset=1036,lag=0
master_failover_state:no-failover
master_replid:9aca20f93d39d5f35af8699ec1edb4ea73381a51
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1036
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1036


六、哨兵(Sentinel)模式安装

什么叫”脑裂”: 由于网络堵塞,主节点响应缓慢等原因,在哨兵监控的情况下,有时哨兵会误判主节点失效,于是选举新的从节点为主节点,并对其他从节点进行数据同步复制(包括误判节点)操作,导致主从复制管理混乱,从而丢失数据,该现象称”脑裂”问题

1.Sentinel环境
HostName IP Port
Sentinel01 192.168.1.138 26379
Sentinel02 192.168.1.139 26379
Sentinel03 192.168.1.140 26379

img

2.安装
1
2
3
4
5
6
7
8
9
10
11
12
13
方法一:
# cd /opt/soft
# tar xvf redis-6.2.14.tar.gz
# cd redis-6.2.14/
# make
# cd src/
# make PREFIX=/usr/local/redis install

---批量
cd /opt/soft && tar xvf redis-*.tar.gz && cd redis-* && make && cd src && make PREFIX=/usr/local/redis install && echo $?

---注意:PREFIX必须大写

3.创建sentinel配置文件
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
68
69
70
71
72
73
74
75
76
77
78
79
80
[root@sentinel03 src]# mkdir /etc/redis && cp /opt/soft/redis-6.2.4/sentinel.conf /etc/redis
[root@sentinel03 src]# ll /etc/redis
total 16
-rw-r--r-- 1 root root 13768 Apr 7 10:49 sentinel.conf

或者安装时候和配置文件一起创建:
cd /opt/soft && tar xvf redis-*.tar.gz && cd redis-* && make && cd src && make PREFIX=/usr/local/redis install && echo $? && mkdir /etc/redis && cp ../sentinel.conf /etc/redis && echo $?


---sentel01

cat >> /etc/redis/sentinel.conf <<EOF

port 26379
bind 192.168.1.138
daemonize yes
protected-mode no
dir /usr/local/redis
logfile /usr/local/redis/sentinel.log
sentinel monitor master 192.168.1.135 1314 2
sentinel down-after-milliseconds master 10000
sentinel parallel-syncs master 1
sentinel failover-timeout master 15000
sentinel auth-pass master 123456

EOF


---sentel02

cat >> /etc/redis/sentinel.conf <<EOF

port 26379
bind 192.168.1.139
daemonize yes
protected-mode no
dir /usr/local/redis
logfile /usr/local/redis/sentinel.log
sentinel monitor master 192.168.1.135 1314 2
sentinel down-after-milliseconds master 10000
sentinel parallel-syncs master 1
sentinel failover-timeout master 15000
sentinel auth-pass master 123456

EOF


---sentel03

cat >> /etc/redis/sentinel.conf <<EOF

port 26379
bind 192.168.1.140
daemonize yes
protected-mode no
dir /usr/local/redis
logfile /usr/local/redis/sentinel.log
sentinel monitor master 192.168.1.135 1314 2
sentinel down-after-milliseconds master 10000
sentinel parallel-syncs master 1
sentinel failover-timeout master 15000
sentinel auth-pass master 123456

EOF





命令详解:
port 26379 sentinel默认端口是26379
bind 后面IP地址是当前sentinel安装所在服务器IP地址
sentinel monitor master 192.168.1.135 1314 2 后面的2是指主机是否宕机至少需要经过2个节点验证,master是个别名
sentinel down-after-milliseconds master 5000 10000毫秒是指10秒
sentinel failover-timeout master 15000 15秒后主库还没有恢复,则实行故障切换

sentinel parallel-syncs与故障转移之后从节点的复制有关:它规定了每次向新的主节点发起复制操作的从节点个数。例如,假设主节点切换完成之后,有3个从节点要向新的主节点发起复制;如果parallel-syncs=1,则从节点会一个一个开始复制;如果parallel-syncs=3,则3个从节点会一起开始复制。

parallel-syncs取值越大,从节点完成复制的时间越快,但是对主节点的网络负载、硬盘负载造成的压力也越大;应根据实际情况设置。例如,如果主节点的负载较低,而从节点对服务可用的要求较高,可以适量增加parallel-syncs取值。parallel-syncs的默认值是1。

4.授权
1
2
chown -R redis.redis /etc/redis
chown -R redis.redis /usr/local/redis/
5.配置PATH环境变量
1
2
3
4
5
6
7
8
9
10
11
12
13
# su - redis
$ vim .bash_profile
export PATH=/usr/local/redis/bin:$PATH

或者
cat >> .bash_profile << EOF

export PATH=/usr/local/redis/bin:\$PATH

EOF

$ source .bash_profile

6.启动/关闭sentinel
1
2
3
4
5
6
7
8
9
10
11
12
13
# su - redis
$ redis-sentinel /etc/redis/sentinel.conf


$ ps -ef|grep sentinel
redis 7309 1 0 11:26 ? 00:00:00 redis-sentinel 192.168.1.138:26379 [sentinel]
redis 7330 7240 0 11:27 pts/0 00:00:00 grep --color=auto sentinel


---关闭sentinel
redis-cli -p 26379 -h 192.168.1.138 shutdown

kill -9 pid
7.监控
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---查看当前master节点情况
$ redis-cli -h 192.168.1.138 -p 26379 sentinel masters

---查看master地址,有几个slave,有几个监控
$ redis-cli -h 192.168.1.138 -p 26379 info

# Sentinel
sentinel_masters:2
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=sdown,address=127.0.0.1:6379,slaves=0,sentinels=1
master1:name=master,status=ok,address=192.168.1.135:1314,slaves=2,sentinels=3

---查看sentinel日志
$ tail -500f /usr/local/redis/sentinel.log
8.Sentinel启停步骤
1
2
3
4
5
6
---启动步骤
先启动redis master主库,再启动Sentinel,redis从库可以后启动

---停止步骤
先停止sentinel,再停止redis slave, 最后停止redis master

9.故障转移测试
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(1) 停止redis master
# shutdown -h now

(2) 查看sentinel日志
[redis@sentinel01 ~]$ tail -500f /usr/local/redis/sentinel.log
7309:X 07 Apr 2024 11:26:24.465 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7309:X 07 Apr 2024 11:26:24.465 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=7309, just started
7309:X 07 Apr 2024 11:26:24.465 # Configuration loaded
7309:X 07 Apr 2024 11:26:24.466 * monotonic clock: POSIX clock_gettime
7309:X 07 Apr 2024 11:26:24.475 * Running mode=sentinel, port=26379.
7309:X 07 Apr 2024 11:26:24.492 # Sentinel ID is 942afea1497531690ea10b6b73da821329f6e475
7309:X 07 Apr 2024 11:26:24.492 # +monitor master mymaster 127.0.0.1 6379 quorum 2
7309:X 07 Apr 2024 11:26:24.493 # +monitor master master 192.168.1.135 1314 quorum 2
7309:X 07 Apr 2024 11:26:24.525 * +slave slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:26:24.526 * +slave slave 192.168.1.137:1314 192.168.1.137 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:26:26.479 * +sentinel sentinel cca82494b29ba27dfa17ea5e45b9aba29db79de6 192.168.1.140 26379 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:26:26.523 * +sentinel sentinel db5123fd74bcd084a39e40188c4eef741181fa59 192.168.1.139 26379 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:26:54.491 # +sdown master mymaster 127.0.0.1 6379
7309:X 07 Apr 2024 11:58:43.077 # +sdown master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.137 # +odown master master 192.168.1.135 1314 #quorum 2/2
7309:X 07 Apr 2024 11:58:43.137 # +new-epoch 1
7309:X 07 Apr 2024 11:58:43.137 # +try-failover master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.143 # +vote-for-leader 942afea1497531690ea10b6b73da821329f6e475 1
7309:X 07 Apr 2024 11:58:43.148 # db5123fd74bcd084a39e40188c4eef741181fa59 voted for 942afea1497531690ea10b6b73da821329f6e475 1
7309:X 07 Apr 2024 11:58:43.149 # cca82494b29ba27dfa17ea5e45b9aba29db79de6 voted for 942afea1497531690ea10b6b73da821329f6e475 1
7309:X 07 Apr 2024 11:58:43.214 # +elected-leader master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.214 # +failover-state-select-slave master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.273 # +selected-slave slave 192.168.1.137:1314 192.168.1.137 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.274 * +failover-state-send-slaveof-noone slave 192.168.1.137:1314 192.168.1.137 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.350 * +failover-state-wait-promotion slave 192.168.1.137:1314 192.168.1.137 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.859 # +promoted-slave slave 192.168.1.137:1314 192.168.1.137 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.859 # +failover-state-reconf-slaves master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:43.859 * +slave-reconf-sent slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:44.268 # -odown master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:44.868 * +slave-reconf-inprog slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:44.868 * +slave-reconf-done slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:44.957 # +failover-end master master 192.168.1.135 1314
7309:X 07 Apr 2024 11:58:44.957 # +switch-master master 192.168.1.135 1314 192.168.1.137 1314
7309:X 07 Apr 2024 11:58:44.958 * +slave slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.137 1314
7309:X 07 Apr 2024 11:58:44.958 * +slave slave 192.168.1.135:1314 192.168.1.135 1314 @ master 192.168.1.137 1314
7309:X 07 Apr 2024 11:58:55.021 # +sdown slave 192.168.1.135:1314 192.168.1.135 1314 @ master 192.168.1.137 1314



从下面日志可以看出主节点从192.168.1.135切换为192.168.1.137,另外从节点192.168.1.136和故障主节点192.168.1.135自动指向新主
7309:X 07 Apr 2024 11:58:44.957 # +switch-master master 192.168.1.135 1314 192.168.1.137 1314
7309:X 07 Apr 2024 11:58:44.958 * +slave slave 192.168.1.136:1314 192.168.1.136 1314 @ master 192.168.1.137 1314
7309:X 07 Apr 2024 11:58:44.958 * +slave slave 192.168.1.135:1314 192.168.1.135 1314 @ master 192.168.1.137 1314



(3)查看新的主从状态
$ redis-cli -p 1314 -h 192.168.1.137 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=192.168.1.136,port=1314,state=online,offset=482569,lag=1
master_failover_state:no-failover
master_replid:96d2a6abd7cac7a4d5e6fc3ab0499e73de08aed2
master_replid2:5ac13b7e033257918adf008129bc038932e92b5a
master_repl_offset:482708
second_repl_offset:403063
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:482694



$ redis-cli -p 1314 -h 192.168.1.136 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.137
master_port:1314
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:487754
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
min_slaves_good_slaves:0
master_failover_state:no-failover
master_replid:96d2a6abd7cac7a4d5e6fc3ab0499e73de08aed2
master_replid2:5ac13b7e033257918adf008129bc038932e92b5a
master_repl_offset:487754
second_repl_offset:403063
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:487754



192.168.1.137:1314> keys *
1) "bar"
2) "test"
3) "qq"
192.168.1.137:1314>
192.168.1.137:1314>
192.168.1.137:1314> set sentinel 1
OK
192.168.1.137:1314> keys *
1) "sentinel"
2) "bar"
3) "test"
4) "qq"


$ redis-cli -p 1314 -h 192.168.1.136 -a 123456 keys *
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "bar"
2) "test"
3) "qq"
4) "sentinel"


此时重新将旧的故障主库重新开机,旧的故障主库会成为从库
[redis@sentinel02 ~]$ redis-cli -p 26379 -h 192.168.1.139 info

# Sentinel
sentinel_masters:2
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=sdown,address=127.0.0.1:6379,slaves=0,sentinels=1
master1:name=master,status=ok,address=192.168.1.137:1314,slaves=2,sentinels=3

[redis@slave02 ~]$ redis-cli -p 1314 -h 192.168.1.137 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.1.136,port=1314,state=online,offset=569950,lag=1
slave1:ip=192.168.1.135,port=1314,state=online,offset=569950,lag=0
master_failover_state:no-failover
master_replid:96d2a6abd7cac7a4d5e6fc3ab0499e73de08aed2
master_replid2:5ac13b7e033257918adf008129bc038932e92b5a
master_repl_offset:570089
second_repl_offset:403063
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:570075

七、配置redis/sentinel开机启动
1.redis开机自启动
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
68
69
70
71
master:
[root@server ~]# vi /etc/systemd/system/redis.service

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/bin/redis-server /etc/redis/1314.conf
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/usr/local/bin/redis-cli -p 1314 -h 192.168.1.135 -a 123456 shutdown
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target



slave01:
[root@server ~]# vi /etc/systemd/system/redis.service

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/bin/redis-server /etc/redis/1314.conf
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/usr/local/bin/redis-cli -p 1314 -h 192.168.1.136 -a 123456 shutdown
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target



slave02:
[root@server ~]# vi /etc/systemd/system/redis.service

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/bin/redis-server /etc/redis/1314.conf
ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/usr/local/bin/redis-cli -p 1314 -h 192.168.1.137 -a 123456 shutdown
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target



加载服务:
[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl enable redis
[root@server ~]# systemctl start redis
[root@server ~]# systemctl status redis
---停止服务
[root@server ~]# systemctl stop redis

2.sentinel开机自启动
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
cat >> /lib/systemd/system/sentinel.service  <<EOF

[Unit]
Description=Redis Sentinel Database Service
Wants=network.target
After=network.target

[Service]
Type=forking
PermissionsStartOnly=true
ExecStart=/usr/local/redis/bin/redis-sentinel /etc/redis/sentinel.conf
ExecReload=/bin/kill -HUP \$MAINPID
Restart=always
User=redis
Group=redis
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target


EOF




加载服务:
systemctl daemon-reload
systemctl enable sentinel
systemctl start sentinel
systemctl status sentinel
---停止服务
systemctl stop sentinel


八、redis登录
1
2
3
[root@master src]# redis-cli -h 192.168.1.135 -p 1314
192.168.1.135:1314> auth 123456
OK
九、主从日常操作
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
(1)开启主从
---两台从节点上执行
[root@slave01 1314]# redis-cli -p 1314 -h 192.168.1.136 -a 123456 slaveof 192.168.1.135 1314

[root@slave02 ~]# redis-cli -p 1314 -h 192.168.1.137 -a 123456 slaveof 192.168.1.135 1314

(2)查询主从状态
[root@master src]# redis-cli -p 1314 -h 192.168.1.135 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.136,port=1314,state=online,offset=435,lag=1
slave1:ip=192.168.1.137,port=1314,state=online,offset=435,lag=1
master_failover_state:no-failover
master_replid:549ec5adb310b50f5fd84305d4b4cc0e973b050f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:435
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:435




[root@slave01 ~]# redis-cli -p 1314 -h 192.168.1.136 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.1.135
master_port:1314
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:589
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:549ec5adb310b50f5fd84305d4b4cc0e973b050f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:589
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:589

(3)解除主从关系
---从库上执行解除主从关系
[root@slave01 ~]# redis-cli -p 1314 -h 192.168.1.136 -a 123456 slaveof on one
  • Title: Redis主从复制和哨兵安装
  • Author: 𝓓𝓸𝓷
  • Created at : 2024-07-12 20:50:45
  • Updated at : 2024-07-20 05:15:41
  • Link: https://www.zhangdong.me/redis-replica-set-sentinel.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论