专栏首页散尽浮华Redis Cluster高可用集群在线迁移操作记录

Redis Cluster高可用集群在线迁移操作记录

之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移。由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上。考虑到是线上生产环境,决定在线迁移,迁移过程,不中断服务。操作过程如下:

一、机器环境

迁移前机器环境
-------------------------------------------------------------------------------
主机名                   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高可用集群环境部署(这里采用"三主三从"模式)

三台节点机器安装操作如下一致
[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节点上写入数据
[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 207-test1
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 208-test2
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 209-test3
OK

登录三个slave节点上写入数据,发现会自动跳到master节点上进行写入
[root@redis-node01 redis-cluster]# /data/redis-4.0.6/src/redis-cli -h 172.16.60.208 -c -p 7003
172.16.60.208:7003> set test5 208-test5
-> Redirected to slot [4644] 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.207 -c -p 7001
172.16.60.207:7001> set test4 207-test4
-> Redirected to slot [517] 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 7005
172.16.60.209:7005> set test6 209-test6
-> Redirected to slot [8775] located at 172.16.60.208:7002
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
"207-test1"
172.16.60.207:7000> get test2
-> Redirected to slot [8899] located at 172.16.60.208:7002
"208-test2"
172.16.60.208:7002> get test3
-> Redirected to slot [13026] located at 172.16.60.209:7004
"209-test3"
172.16.60.209:7004> get test4
-> Redirected to slot [517] located at 172.16.60.207:7000
"207-test4"
172.16.60.207:7000> get test5
"208-test5"
172.16.60.207:7000> get test6
-> Redirected to slot [8775] located at 172.16.60.208:7002
"209-test6"
172.16.60.208:7002> get test5
-> Redirected to slot [4644] located at 172.16.60.207:7000
"208-test5"
172.16.60.207:7000> get test4
"207-test4"
172.16.60.207:7000> get test3
-> Redirected to slot [13026] located at 172.16.60.209:7004
"209-test3"
172.16.60.209:7004> get test4
-> Redirected to slot [517] located at 172.16.60.207:7000
"207-test4"
172.16.60.207:7000> get test2
-> Redirected to slot [8899] located at 172.16.60.208:7002
"208-test2"
172.16.60.208:7002> get test1
-> Redirected to slot [4768] located at 172.16.60.207:7000
"207-test1"
172.16.60.207:7000> 

三、在线迁移

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • centos下部署redis服务环境的操作记录

    Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日...

    洗尽了浮华
  • centos6下redis cluster集群部署过程

    一般来说,redis主从和mysql主从目的差不多,但redis主从配置很简单,主要在从节点配置文件指定主节点ip和端口,比如:slaveof 192.168....

    洗尽了浮华
  • Redis+Keepalived高可用环境部署记录

    洗尽了浮华
  • Redis之主从复制2

    如此6380是一个从机,而6380还有一个slave是6381.至此实现了我们上面的结构图

    用户4919348
  • 腾讯云服务器Linux系统--安装redis

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    营琪
  • windows 下对Redis的安装和部署以及连接客户端

    Redis的安装和部署 一、Redis的下载地址 Redis官方并没有提供Redis的windows安装包,但在github上, 有相关的下载地址,如下: ...

    学到老
  • ubuntu安装redis以及phpredis扩展

    安装步骤: 1.执行:git clone https://github.com/nicolasff/phpredis.git 2.执行:cd phpredi...

    苦咖啡
  • centos下redis安全相关

    由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!!

    超蛋lhy
  • 使用 docker-compose 在 Docker 中启动带密码的 Redis

    在服务器上使用 docker-compose 启动一个 Docker 中的 Redis 时,配置文件没有生效,Redis 没有加密码,导致 redis 中被写入...

    木制robot
  • Redis 起步

    http://www.cnblogs.com/shanyou/archive/2012/01/28/2330451.html

    bear_fish

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动