redis 之 集群

#:下载源码包,并编译安装

[root@localhost src]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz
[root@localhost redis]# tar xf redis-4.0.14.tar.gz
[root@localhost redis]# cd redis-4.0.14/
[root@localhost redis]# make PREFIX=/apps/redis install

#:创建redis用户

[root@localhost redis]# groupadd -g 55 redis && useradd -g redis -u 55 redis

#:创建所需目录

[root@localhost redis]# mkdir /apps/redis/{data,logs,run,etc}

#:准备配置文件

[root@localhost src]# cd /usr/local/src/redis-4.0.14/
[root@localhost redis-4.0.14]# cp redis.conf /apps/redis/etc/

#:配置环境变量

[root@localhost redis-4.0.14]# cd /apps/redis/
[root@localhost redis]# ln -sv bin/redis-* /usr/bin

#:配置启动脚本

[root@localhost redis]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
#ExecStop=/usr/libexec/redis-shutdown
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

#:修改配置文件

[root@localhost ~]# vim /apps/redis/etc/redis.conf
bind 0.0.0.0
pidfile /apps/redis/run/redis_6379.pid
logfile "/apps/redis/logs/redis_6379.log"
stop-writes-on-bgsave-error no
dir "/apps/redis/data"
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 30000


#:修改目录的属主属组

[root@localhost apps]# chown -R redis.redis ./*


#:将文件打包,并传给所有节点(在所有节点必须创建apps目录,和redis用户)

[root@localhost apps]# tar zcvf redis.tar.gz ./*
[root@localhost apps]# scp redis.tar.gz 192.168.7.102:/apps/
[root@localhost apps]# scp redis.tar.gz 192.168.7.103:/apps/
[root@localhost apps]# scp redis.tar.gz 192.168.7.104:/apps/
[root@localhost apps]# scp redis.tar.gz 192.168.7.105:/apps/
[root@localhost apps]# scp redis.tar.gz 192.168.7.106:/apps/

#:将启动脚本拷贝到各节点

[root@localhost lib]# scp /usr/lib/systemd/system/redis.service 192.168.7.102:/usr/lib/systemd/system/
[root@localhost lib]# scp /usr/lib/systemd/system/redis.service 192.168.7.103:/usr/lib/systemd/system/
[root@localhost lib]# scp /usr/lib/systemd/system/redis.service 192.168.7.104:/usr/lib/systemd/system/
[root@localhost lib]# scp /usr/lib/systemd/system/redis.service 192.168.7.105:/usr/lib/systemd/system/
[root@localhost lib]# scp /usr/lib/systemd/system/redis.service 192.168.7.106:/usr/lib/systemd/system/

#:在各几点解压并安装启动

 

 

创建集群

需要使用到集群管理工具 redis-trib.rb这个工具是 redis 官方推出的管理 redis 集群的工具,集成在redis 的源码 src 目录下,是基于 redis 提供的集群命令封装成简单、便捷、实用的操作工具,redis- trib.rb redis 作者用 ruby 开发完成的,centos 系统 yum 安装的 ruby 存在版本较低

#:解决ruby版本较低问题

[root@localhost ~]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
[root@localhost ~]# tar xf ruby-2.5.5.tar.gz
[root@localhost ruby-2.5.5]# cd ruby-2.5.5/
[root@localhost ruby-2.5.5]# ./configure 
[root@localhost ruby-2.5.5]# make -j 2
[root@localhost ruby-2.5.5]#  make install
[root@localhost ruby-2.5.5]#   gem install redis

[root@localhost ruby-2.5.5]# ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

#:验证 redis-trib.rb 命令是否可执行

[root@localhost ruby-2.5.5]# cp /usr/local/src/redis-4.0.14/src/redis-trib.rb /usr/bin
[root@localhost ruby-2.5.5]# redis-trib.rb

#:修改密码为redis的登录密码

[root@localhost lib]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.2/lib/redis/client.rb
 :password => 123456,

#:创建集群

[root@localhost redis]# redis-trib.rb create --replicas 1 192.168.7.101:6379 192.168.7.102:6379 192.168.7.103:6379 192.168.7.104:6379 192.168.7.105:6379 192.168.7.106:6379

 

集群维护之动态添加节点

在准备两台redis服务器,因为一主一从,按照以前的步骤先将两台主机配置成redis服务器

#:添加节点到集群


[root@localhost lib]# redis-trib.rb add-node 192.168.7.107:6379 192.168.7.101:6379

#:分配槽位(添加主机之后需要对添加至集群种的新主机重新分片否则其没有分片)(有几个主节点就用16384除于几个主节点)


[root@localhost lib]# redis-trib.rb reshard 192.168.7.107:6379

#:验证一下

[root@localhost lib]# redis-trib.rb check 192.168.7.101:6379

#:为新的master添加slave节点(先将从节点也加入到集群)


[root@localhost lib]# redis-trib.rb add-node 192.168.7.108:6379 192.168.7.101:6379


#;先连接进slave节点(然后设置成从服务区)

[root@localhost lib]# redis-cli -h 192.168.7.108
192.168.7.108:6379> AUTH 123456
OK
192.168.7.108:6379> CLUSTER NODES
cdebab30a87d4fbca43eb6a87bd27ac032db68b8 192.168.7.105:6379@16379 slave 62c85875d14f9468f2019d90e800bac7175a3b87 0 1568086716108 8 connected
c6d3bc831a365822922798dd957adc063d4500b3 192.168.7.108:6379@16379 myself,master - 0 1568086714000 0 connected
691104e3a3ea714f64778d660c043137c8499815 192.168.7.102:6379@16379 master - 0 1568086717119 2 connected 6827-10922
f8ae70864fcdec35913f7217274f5a8d03a80899 192.168.7.104:6379@16379 slave 8474a3ca08a8f6e60901bb81b5b580935e2ead2e 0 1568086716000 3 connected
9ff8fdf8711a8e1b1e443e200a54879f30a4d6f6 192.168.7.106:6379@16379 slave 691104e3a3ea714f64778d660c043137c8499815 0 1568086715000 2 connected
8474a3ca08a8f6e60901bb81b5b580935e2ead2e 192.168.7.103:6379@16379 master - 0 1568086713000 3 connected 12288-16383
62c85875d14f9468f2019d90e800bac7175a3b87 192.168.7.101:6379@16379 master - 0 1568086714000 8 connected 1365-5460
647bfe7cc607de88f95c26ddb3044823631b3d52 192.168.7.107:6379@16379 master - 0 1568086714087 9 connected 0-1364 5461-6826 10923-12287
192.168.7.108:6379> CLUSTER REPLICATE  647bfe7cc607de88f95c26ddb3044823631b3d52   #:执行此命令

 

 

集群维护之动态删除节点

添加节点的时候是先添加 node 节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正

好相反,是先将被删除的 Redis node 上的槽位迁移到集群中的其他 Redis node 节点上,然后再将其删除。

#:还是进行reshard 

[root@localhost lib]# redis-trib.rb reshard 192.168.7.101:6379
How many slots do you want to move (from 1 to 16384)? 4096  #要下线服务器槽位的总数

What is the receiving node ID? 647bfe7cc607de88f95c26ddb3044823631b3d52 #把槽位分配给谁

Source node #1:8474a3ca08a8f6e60901bb81b5b580935e2ead2e#:从哪个服务器分配槽位(要下线的服务器)

Source node #2:done #如果没有了,则写done


#:将服务器从集群中删除

[root@localhost lib]# redis-trib.rb del-node 192.168.7.101:6379 8474a3ca08a8f6e60901bb81b5b580935e2ead2e

#;然后将从节点也删除

[root@localhost lib]# redis-trib.rb del-node 192.168.7.101:6379 9ff8fdf8711a8e1b1e443e200a54879f30a4d6f6

 

posted @ 2019-09-10 12:58  Linus小跟班  阅读(253)  评论(0编辑  收藏  举报