欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Redis学习笔记——(三)Redis集群安装

程序员文章站 2022-07-05 13:24:07
...

redis集群安装
正常安装redis是需要至少6太服务器,这里模拟采用6个实例进行模拟安装。安装步骤
1.集群环境安装,其中redis-3.0.0.gem版本不一定要和redis版本对应,只要保证redis-trib.rb能够运行即可。

redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:

安装ruby
yum install ruby
yum install rubygems

安装ruby和redis的接口程序
拷贝redis-3.0.0.gem至/usr/local下
执行:
gem install /usr/local/redis-3.0.0.gem

2.新建6个redis实例,修改配置文件,模拟安装。

[root@localhost local]# mkdir redis-cluster --创建主目录,存放6个集群实例
[root@localhost local]# ls
bin  games    lib    libexec  redis-cluster  share
etc  include  lib64  redis    sbin           src
[root@localhost local]# cd redis --进入之前安装好的redis
[root@localhost redis]# cp -r bin ../redis-cluster/redis01 --复制为实例01
[root@localhost redis]# cd ../redis-cluster/redis01/
[root@localhost redis01]# ls
dump.rdb         redis-benchmark  redis-cli   redis-sentinel
mkreleasehdr.sh  redis-check-aof  redis.conf  redis-server
[root@localhost redis01]# rm -f dump.rdb --删除这个dump文件
[root@localhost redis01]# vim redis.conf --编辑配置文件。

3.编辑内容:修改端口号为7001,cluster-enabled yes

4.然后将01复制多份,分别是02、03、04、05、06

[root@localhost redis-cluster]# cp -r redis01/ redis02
[root@localhost redis-cluster]# cp -r redis01/ redis03
[root@localhost redis-cluster]# cp -r redis01/ redis04
[root@localhost redis-cluster]# cp -r redis01/ redis05
[root@localhost redis-cluster]# cp -r redis01/ redis06
[root@localhost redis-cluster]# ls
redis01  redis02  redis03  redis04  redis05  redis06
[root@localhost redis-cluster]# ls
redis01  redis02  redis03  redis04  redis05  redis06

5.然后分别编辑每个redis0x 下的配置文件,修改端口号为对应的端口号。

[root@localhost redis-cluster]# vim redis02/redis.conf 
[root@localhost redis-cluster]# vim redis03/redis.conf 
[root@localhost redis-cluster]# vim redis04/redis.conf 
[root@localhost redis-cluster]# vim redis05/redis.conf 
[root@localhost redis-cluster]# vim redis06/redis.conf 
[root@localhost redis-cluster]# cd /softPage/

6.拷贝redis.trib.rb到redis-cluster目录

[root@localhost softPage]# cd redis-3.2.11/src
[root@localhost src]# cp *.rb /usr/local/redis-cluster/
[root@localhost src]# cd /usr/local/redis-cluster/
[root@localhost redis-cluster]# ls
redis01  redis02  redis03  redis04  redis05  redis06  redis-trib.rb

7.启动所有的redis实例,可以把命令编写到.sh文件统一执行。编写startall.sh

[root@localhost redis-cluster]# vim startall.sh

startall.sh内容

cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..

8.添加文件权限,执行,启动


[root@localhost redis-cluster]# chmod +x startall.sh  

9.创建集群,这里后面跟的就是对应实例的IP:端口号,如果生成环境,就换成对应的服务器IP:端口号即可。
中间会提示是否按照这样创建集群,输入yes即可。

[[email protected] redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.0.101:7001 192.168.0.101:7002 192.168.0.101:7003 192.168.0.101:7004  192.168.0.101:7005  192.168.0.101:7006 
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.101:7001
192.168.0.101:7002
192.168.0.101:7003
Adding replica 192.168.0.101:7004 to 192.168.0.101:7001
Adding replica 192.168.0.101:7005 to 192.168.0.101:7002
Adding replica 192.168.0.101:7006 to 192.168.0.101:7003
M: d545e0f0b6742f6f40d473b8564bc0086d6baca8 192.168.0.101:7001
   slots:0-5460 (5461 slots) master
M: fa2b24894700b59df671141b046e1e0df460593f 192.168.0.101:7002
   slots:5461-10922 (5462 slots) master
M: 370c93df267099ccd0b4781ba56972edbc02fe00 192.168.0.101:7003
   slots:10923-16383 (5461 slots) master
S: 952813f2edcf9cc2ad7e02aa4b8c1371d7f47b3c 192.168.0.101:7004
   replicates d545e0f0b6742f6f40d473b8564bc0086d6baca8
S: 0238abb208781121cfd4c864541338eb500d5127 192.168.0.101:7005
   replicates fa2b24894700b59df671141b046e1e0df460593f
S: 648d308fe6d53f08fdccbc10a53ce9db74870223 192.168.0.101:7006
   replicates 370c93df267099ccd0b4781ba56972edbc02fe00
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
。。。。。。。省略
>>> Performing Cluster Check (using node 192.168.0.101:7001)
M: d545e0f0b6742f6f40d473b8564bc0086d6baca8 192.168.0.101:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 952813f2edcf9cc2ad7e02aa4b8c1371d7f47b3c 192.168.0.101:7004
   slots: (0 slots) slave
   replicates d545e0f0b6742f6f40d473b8564bc0086d6baca8
S: 0238abb208781121cfd4c864541338eb500d5127 192.168.0.101:7005
   slots: (0 slots) slave
   replicates fa2b24894700b59df671141b046e1e0df460593f
M: fa2b24894700b59df671141b046e1e0df460593f 192.168.0.101:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 648d308fe6d53f08fdccbc10a53ce9db74870223 192.168.0.101:7006
   slots: (0 slots) slave
   replicates 370c93df267099ccd0b4781ba56972edbc02fe00
M: 370c93df267099ccd0b4781ba56972edbc02fe00 192.168.0.101:7003
   slots:10923-16383 (5461 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.

10.测试集群,在任意实例下都可以,启动redis-cli -h 服务器IP -p 端口号 -c
这里-c 表示在集群中间跳转。

[[email protected] redis01]# ./redis-cli -h 192.168.0.101 -p 7002 -c
192.168.0.101:7002> set a b
-> Redirected to slot [15495] located at 192.168.0.101:7003
OK
192.168.0.101:7003> get a
"b"
192.168.0.101:7003> set c d
-> Redirected to slot [7365] located at 192.168.0.101:7002
OK
192.168.0.101:7003> get c
-> Redirected to slot [7365] located at 192.168.0.101:7002
"d"

11.编写关闭脚本 shutdownall.sh,指定端口进行关闭

[root@localhost redis-cluster]# vim shutdownall.sh
[root@localhost redis-cluster]# chmod +x shutdownall.sh 
[root@localhost redis-cluster]# ./shutdownall.sh 
[root@localhost redis-cluster]# ps aux|grep redis
root      11896  0.0  0.7 136920  7548 ?        Ssl  21:26   0:02 ./redis-server 0.0.0.0:6379
root      13314  0.0  0.0 112660   960 pts/1    R+   22:41   0:00 grep --color=auto redis
[root@localhost redis-cluster]# 

--脚本内容如下
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
redis01/redis-cli -p 7003 shutdown
redis01/redis-cli -p 7004 shutdown
redis01/redis-cli -p 7005 shutdown
redis01/redis-cli -p 7006 shutdown