Redis Cluster 4.0高可用集群安装在线迁移操作记录
Posted 张冲andy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis Cluster 4.0高可用集群安装在线迁移操作记录相关的知识,希望对你有一定的参考价值。
之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移。由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上。考虑到是线上生产环境,决定在线迁移,迁移过程,不中断服务。操作过程如下:
一、机器环境
1
2
3
4
5
6
7
8
9
10
11
12
13
|
迁移前机器环境 ------------------------------------------------------------------------------- 主机名 ip地址 节点端口 redis-node01 172.16.60.207 7000,7001 redis-node02 172.16.60.208 7002,7003 redis-node03 172.16.60.209 7004,7005 迁移后机器环境 ------------------------------------------------------------------------------- 主机名 ip地址 节点端口 redis-new01 172.16.60.202 7000,7001 redis-new02 172.16.60.204 7002,7003 redis-new03 172.16.60.205 7004,7005 |
二、迁移前redis cluster高可用集群环境部署(这里采用"三主三从"模式)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
三台节点机器安装操作如下一致 [root@redis-node01 ~] # yum install -y gcc g++ make gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip git [root@redis-node01 ~] # /etc/init.d/iptables stop [root@redis-node01 ~] # setenforce 0 [root@redis-node01 ~] # vim /etc/sysconfig/selinux SELINUX=disabled 下载并编译安装redis [root@redis-node01 ~] # mkdir -p /data/software/ [root@redis-node01 software] # wget http://download.redis.io/releases/redis-4.0.6.tar.gz [root@redis-node01 software] # tar -zvxf redis-4.0.6.tar.gz [root@redis-node01 software] # mv redis-4.0.6 /data/ [root@redis-node01 software] # cd /data/redis-4.0.6/ [root@redis-node01 redis-4.0.6] # make ------------------------------------------------------------------------------- 分别创建和配置节点 节点1配置 [root@redis-node01 ~] # mkdir /data/redis-4.0.6/redis-cluster [root@redis-node01 ~] # cd /data/redis-4.0.6/redis-cluster [root@redis-node01 redis-cluster] # mkdir 7000 7001 [root@redis-node01 redis-cluster] # vim 7000/redis.conf port 7000 bind 172.16.60.207 daemonize yes pidfile /var/run/redis_7000 .pid cluster-enabled yes cluster-config- file nodes_7000.conf cluster-node-timeout 10100 appendonly yes [root@redis-node01 redis-cluster] # vim 7001/redis.conf port 7001 bind 172.16.60.207 daemonize yes pidfile /var/run/redis_7001 .pid cluster-enabled yes cluster-config- file nodes_7001.conf cluster-node-timeout 10100 appendonly yes 节点2配置 [root@redis-node02 ~] # mkdir /data/redis-4.0.6/redis-cluster [root@redis-node02 ~] # cd /data/redis-4.0.6/redis-cluster [root@redis-node02 redis-cluster] # mkdir 7002 7003 [root@redis-node02 redis-cluster] # vim 7000/redis.conf port 7002 bind 172.16.60.208 daemonize yes pidfile /var/run/redis_7002 .pid cluster-enabled yes cluster-config- file nodes_7002.conf cluster-node-timeout 10100 appendonly yes [root@redis-node01 redis-cluster] # vim 7003/redis.conf port 7003 bind 172.16.60.208 daemonize yes pidfile /var/run/redis_7003 .pid cluster-enabled yes cluster-config- file nodes_7003.conf cluster-node-timeout 10100 appendonly yes 节点3配置 [root@redis-node01 ~] # mkdir /data/redis-4.0.6/redis-cluster [root@redis-node01 ~] # cd /data/redis-4.0.6/redis-cluster [root@redis-node01 redis-cluster] # mkdir 7004 7005 [root@redis-node01 redis-cluster] # vim 7004/redis.conf port 7004 bind 172.16.60.209 daemonize yes pidfile /var/run/redis_7004 .pid cluster-enabled yes cluster-config- file nodes_7004.conf cluster-node-timeout 10100 appendonly yes [root@redis-node01 redis-cluster] # vim 7005/redis.conf port 7005 bind 172.16.60.209 daemonize yes pidfile /var/run/redis_7005 .pid cluster-enabled yes cluster-config- file nodes_7005.conf cluster-node-timeout 10100 appendonly yes ------------------------------------------------------------------------------- 分别启动redis服务(这里统一在 /data/redis-4 .0.6 /redis-cluster 路径下启动redis服务,即nodes_*.conf等文件也在这个路径下产生) 节点1 [root@redis-node01 redis-cluster] # for((i=0;i<=1;i++)); do /data/redis-4.0.6/src/redis-server /data/redis-4.0.6/redis-cluster/700$i/redis.conf; done [root@redis-node01 redis-cluster] # ps -ef|grep redis root 1103 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.207:7000 [cluster] root 1105 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.207:7001 [cluster] root 1315 32360 0 16:16 pts /1 00:00:00 grep redis 节点2 [root@redis-node02 redis-cluster] # for((i=2;i<=3;i++)); do /data/redis-4.0.6/src/redis-server /data/redis-4.0.6/redis-cluster/700$i/redis.conf; done [root@redis-node02 redis-cluster] # ps -ef|grep redis root 9446 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.208:7002 [cluster] root 9448 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.208:7003 [cluster] root 9644 8540 0 16:17 pts /0 00:00:00 grep redis 节点3 [root@redis-node01 redis-cluster] # for((i=4;i<=5;i++)); do /data/redis-4.0.6/src/redis-server /data/redis-4.0.6/redis-cluster/700$i/redis.conf; done [root@redis-node03 ~] # ps -ef|grep redis root 9486 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.209:7004 [cluster] root 9488 1 0 15:19 ? 00:00:03 /data/redis-4 .0.6 /src/redis-server 172.16.60.209:7005 [cluster] root 9686 9555 0 16:17 pts /0 00:00:00 grep redis ------------------------------------------------------------------------------- 接着在节点1上安装 Ruby(只需要在其中一个节点上安装即可) [root@redis-node01 ~] # yum -y install ruby ruby-devel rubygems rpm-build [root@redis-node01 ~] # gem install redis 温馨提示: 在centos6.x下执行上面的 "gem install redis" 操作可能会报错,坑很多! 默认yum安装的ruby版本是1.8.7,版本太低,需要升级到ruby2.2以上,否则执行上面安装会报错! 首先安装rvm(或者直接下载证书:https: //pan .baidu.com /s/1slTyJ7n 密钥:7uan 下载并解压后直接执行 "curl -L get.rvm.io | bash -s stable" 即可) [root@redis-node01 ~] # curl -L get.rvm.io | bash -s stable //可能会报错,需要安装提示进行下面一步操作 [root@redis-node01 ~] # curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - //然后再接着执行:curl -L get.rvm.io | bash -s stable [root@redis-node01 ~] # find / -name rvm.sh /etc/profile .d /rvm .sh [root@redis-node01 ~] # source /etc/profile.d/rvm.sh [root@redis-node01 ~] # rvm requirements 然后升级ruby到2.3 [root@redis-node01 ~] # rvm install ruby 2.3.1 [root@redis-node01 ~] # ruby -v ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] 列出所有ruby版本 [root@redis-node01 ~] # rvm list 设置默认的版本 [root@redis-node01 ~] # rvm --default use 2.3.1 更新下载源 [root@redis-node01 ~] # gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org https: //gems .ruby-china.org/ added to sources source https: //rubygems .org not present in cache [root@redis-node01 ~] # gem sources *** CURRENT SOURCES *** https: //rubygems .org/ https: //gems .ruby-china.org/ 最后就能顺利安装了 [root@redis-node01 ~] # gem install redis Successfully installed redis-4.0.6 Parsing documentation for redis-4.0.6 Done installing documentation for redis after 1 seconds 1 gem installed ------------------------------------------------------------------------------- 接着创建redis cluster集群(在节点1机器上操作即可) 首先手动指定三个master节点。master节点最好分布在三台机器上 [root@redis-node01 ~] # /data/redis-4.0.6/src/redis-trib.rb create 172.16.60.207:7000 172.16.60.208:7002 172.16.60.209:7004 然后手动指定上面三个master节点各自的slave节点。slave节点也最好分布在三台机器上 [root@redis-node01 ~] # /data/redis-4.0.6/src/redis-trib.rb add-node --slave 172.16.60.208:7003 172.16.60.207:7000 [root@redis-node01 ~] # /data/redis-4.0.6/src/redis-trib.rb add-node --slave 172.16.60.209:7005 172.16.60.208:7002 [root@redis-node01 ~] # /data/redis-4.0.6/src/redis-trib.rb add-node --slave 172.16.60.207:7001 172.16.60.209:7004 然后检查下redis cluster集群状态 [root@redis-node01 redis-cluster] # /data/redis-4.0.6/src/redis-trib.rb check 172.16.60.207:7000 >>> Performing Cluster Check (using node 172.16.60.207:7000) M: 971d05cd7b9bb3634ad024e6aac3dff158c52eee 172.16.60.207:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: e7592314869c29375599d781721ad76675645c4c 172.16.60.209:7005 slots: (0 slots) slave replicates 0060012d749167d3f72833d916e53b3445b66c62 S: 52b8d27838244657d9b01a233578f24d287979fe 172.16.60.208:7003 slots: (0 slots) slave replicates 971d05cd7b9bb3634ad024e6aac3dff158c52eee S: 213bde6296c36b5f31b958c7730ff1629125a204 172.16.60.207:7001 slots: (0 slots) slave replicates e936d5b4c95b6cae57f994e95805aef87ea4a7a5 M: e936d5b4c95b6cae57f994e95805aef87ea4a7a5 172.16.60.209:7004 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 0060012d749167d3f72833d916e53b3445b66c62 172.16.60.208:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots。 covered. 通过上面可以看出,只有master主节点才占用slots,从节点都是0 slots,也就是说keys数值是在master节点上。 三个master主节点分割了16384 slots。分别是0-5460、5461-10922、10923-16383。 如果有一组master-slave都挂掉,16484 slots不完整,则整个集群服务也就挂了,必须待这组master-slave节点,集群才能恢复。 如果新加入master主节点,默认是0 slots,需要reshard为新master节点分布数据槽(会询问向移动多少哈希槽到此节点),后面会提到。 写入几条测试数据 登录三个master节点上写入数据(登录slave节点上写入数据,发现也会自动跳到master节点上进行写入) [root@redis-node01 redis-cluster] # /data/redis-4.0.6/src/redis-cli -h 172.16.60.207 -c -p 7000 172.16.60.207:7000> set test1 test -207 OK 172.16.60.207:7000> set test11 test -207-207 -> Redirected to slot [13313] located at 172.16.60.209:7004 OK [root@redis-node01 redis-cluster] # /data/redis-4.0.6/src/redis-cli -h 172.16.60.208 -c -p 7002 172.16.60.208:7002> set test2 test -208 OK 172.16.60.208:7002> set test22 test -208-208 -> Redirected to slot [4401] located at 172.16.60.207:7000 OK [root@redis-node01 redis-cluster] # /data/redis-4.0.6/src/redis-cli -h 172.16.60.209 -c -p 7004 172.16.60.209:7004> set test3 test -209 OK 172.16.60.209:7004> set test33 test -209-209 OK 读数据 [root@redis-node01 redis-cluster] # /data/redis-4.0.6/src/redis-cli -h 172.16.60.207 -c -p 7000 172.16.60.207:7000> get test1 "test-207" 172.16.60.207:7000> get test11 -> Redirected to slot [13313] located at 172.16.60.209:7004 "test-207-207" 172.16.60.209:7004> get test2 -> Redirected to slot [8899] located at 172.16.60.208:7002 "test-208" 172.16.60.208:7002> get test22 -> Redirected to slot [4401] located at 172.16.60.207:7000 "test-208-208" 172.16.60.207:7000> get test3 -> Redirected to slot [13026] located at 172.16.60.209:7004 "test-209" 172.16.60.209:7004> get test33 "test-209-209" 172.16.60.209:7004> |
三、在线迁移
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207以上是关于Redis Cluster 4.0高可用集群安装在线迁移操作记录的主要内容,如果未能解决你的问题,请参考以下文章
Redis 单节点 ➤ Redis Sentinel 高可用 ➤ Redis Cluster 集群 Redis 单节点 ➤ Redis Sentinel 高可用 ➤ Redis Cluster 集群 |