Linux(CentOS7) 环境搭建 Redis5.0.8 集群

本篇文章,阐述从Centos7 min版 ,从0搭建redis集群

如果Centos7 最小化安装没有ifconfig这个命令:

yum install net-tools

安装完成之后

Linux(CentOS7) 环境搭建 Redis5.0.8 集群

 

安装rz sz 工具 :yum -y install lrzsz

将下载好的 redis 安装包和 ruby 安装包上传到linux 解压

tar zxvf  redis-stable.tar.gz          

tar zxvf  ruby-2.6.6.tar.gz

yum install gcc-c++

make MALLOC=libc

yum install -y tcl

make test

make install PREFIX=/usr/local/redis

 cd redis-stable

 cp redis.conf /usr/local/redis/bin/

vi redis.conf 

daemonize yes

 

前端启动:./redis-server

后端启动: ./redis-server redis.conf  

客户端操作: ./redis-cli 

#############################单机版Redis搭建完成##################################

[root@localhost local]# mkdir redis-cluster

修改配置文件
[root@localhost redis]# cp -r bin/ /usr/local/redis-cluster/
[root@localhost redis]# cd /usr/local/redis-cluster/
[root@localhost redis-cluster]# mv bin/ redis01
[root@localhost redis-cluster]# vi redis01/redis.conf

bind 127.0.0.1 192.168.198.132
port 6001
cluster-enabled yes

复制6份

[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

修改对应端口 6002 6003 6004 6005 6006

[root@localhost redis-cluster]# touch start.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 ..

 

脚本授权:
[root@localhost redis-cluster]# chmod +x start.sh

启动redis实例
[root@localhost redis-cluster]# ./start.sh
9546:C 16 Apr 2020 07:43:44.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9546:C 16 Apr 2020 07:43:44.197 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9546, just started
9546:C 16 Apr 2020 07:43:44.197 # Configuration loaded
9548:C 16 Apr 2020 07:43:44.204 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9548:C 16 Apr 2020 07:43:44.204 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9548, just started
9548:C 16 Apr 2020 07:43:44.204 # Configuration loaded
9550:C 16 Apr 2020 07:43:44.210 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9550:C 16 Apr 2020 07:43:44.210 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9550, just started
9550:C 16 Apr 2020 07:43:44.210 # Configuration loaded
9555:C 16 Apr 2020 07:43:44.221 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9555:C 16 Apr 2020 07:43:44.221 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9555, just started
9555:C 16 Apr 2020 07:43:44.221 # Configuration loaded
9560:C 16 Apr 2020 07:43:44.226 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9560:C 16 Apr 2020 07:43:44.226 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9560, just started
9560:C 16 Apr 2020 07:43:44.226 # Configuration loaded
9565:C 16 Apr 2020 07:43:44.236 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9565:C 16 Apr 2020 07:43:44.236 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9565, just started
9565:C 16 Apr 2020 07:43:44.236 # Configuration loaded

查看redis实例进程
[root@localhost redis-cluster]# ps aux|grep redis
root 9454 0.1 0.2 144092 2216 ? Ssl 07:21 0:02 ./redis-server 127.0.0.1:6379
root 9547 0.1 0.2 144540 2588 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6001 [cluster]
root 9549 0.1 0.2 144540 2588 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6002 [cluster]
root 9554 0.1 0.2 144540 2588 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6003 [cluster]
root 9556 0.1 0.2 144540 2588 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6004 [cluster]
root 9561 0.1 0.2 144540 2584 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6005 [cluster]
root 9566 0.1 0.2 144540 2584 ? Ssl 07:43 0:00 ./redis-server 127.0.0.1:6006 [cluster]
root 9579 0.0 0.0 112712 964 pts/0 R+ 07:44 0:00 grep --color=auto redis

移动redis管理工具到集群中
[root@localhost redis-stable]# cd src/
[root@localhost src]# ll *.rb
-rwxr-xr-x. 1 1000 1000 3600 Mar 12 23:07 redis-trib.rb
[root@localhost src]# cp redis-trib.rb /usr/local/redis-cluster/
[root@localhost src]# cp redis-cli /usr/local/redis-cluster/

 

安装ruby
[root@localhost local]# mkdir ruby
[root@localhost ruby-2.6.6]# ./configure
[root@localhost ruby-2.6.6]# make
[root@localhost ruby-2.6.6]# make PREFIX=/usr/local/ruby install
[root@localhost ~]# cd ruby-2.6.6
[root@localhost ruby-2.6.6]# ruby -version
ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
Traceback (most recent call last):
-e:1:in `<main>‘: undefined local variable or method `rsion‘ for main:Object (NameError)
[root@localhost ruby-2.6.6]#

 

启动集群:
[root@localhost redis-cluster]# ./redis-cli --cluster create 192.168.198.132:6001 192.168.198.132:6002 192.168.198.132:6003 192.168.198.132:6004 192.168.198.132:6005 192.168.198.132:6006 --cluster-replicas 1

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.198.132:6005 to 192.168.198.132:6001
Adding replica 192.168.198.132:6006 to 192.168.198.132:6002
Adding replica 192.168.198.132:6004 to 192.168.198.132:6003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 507c435cb94b5d37fd35949cba52ac3306dec9b5 192.168.198.132:6001
   slots:[0-5460] (5461 slots) master
M: 635d950f173541311e09481aa9d323cbfb6e52a9 192.168.198.132:6002
   slots:[5461-10922] (5462 slots) master
M: 36b583765b542ba35fa8e8378bc3c2c669291c16 192.168.198.132:6003
   slots:[10923-16383] (5461 slots) master
S: 7904d00a56fde596e5e674de47e99f7034a2bfbd 192.168.198.132:6004
   replicates 635d950f173541311e09481aa9d323cbfb6e52a9
S: 1b413d7209108340bb863f38c060712cf0aa1563 192.168.198.132:6005
   replicates 36b583765b542ba35fa8e8378bc3c2c669291c16
S: d43812094132d7f978d73dcef6261d3c1b3449cf 192.168.198.132:6006
   replicates 507c435cb94b5d37fd35949cba52ac3306dec9b5
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
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 192.168.198.132:6001)
M: 507c435cb94b5d37fd35949cba52ac3306dec9b5 192.168.198.132:6001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1b413d7209108340bb863f38c060712cf0aa1563 127.0.0.1:6005
   slots: (0 slots) slave
   replicates 36b583765b542ba35fa8e8378bc3c2c669291c16
M: 635d950f173541311e09481aa9d323cbfb6e52a9 127.0.0.1:6002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7904d00a56fde596e5e674de47e99f7034a2bfbd 127.0.0.1:6004
   slots: (0 slots) slave
   replicates 635d950f173541311e09481aa9d323cbfb6e52a9
S: d43812094132d7f978d73dcef6261d3c1b3449cf 127.0.0.1:6006
   slots: (0 slots) slave
   replicates 507c435cb94b5d37fd35949cba52ac3306dec9b5
M: 36b583765b542ba35fa8e8378bc3c2c669291c16 127.0.0.1:6003
   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.

集群安装启动完成

测试集群

[root@localhost redis-cluster]# redis-cli -h 192.168.198.132 -c -p 6001
-bash: redis-cli: command not found
[root@localhost redis-cluster]# ./redis-cli -h 192.168.198.132 -c -p 6001
192.168.198.132:6001> set a 1000
-> Redirected to slot [15495] located at 127.0.0.1:6003
OK
127.0.0.1:6003> get a
"1000"
127.0.0.1:6003>

测试完成!

##############################Redis集群搭建完成#####################################

参考文档:https://blog.csdn.net/yuhuochengshi/article/details/102610562

Linux(CentOS7) 环境搭建 Redis5.0.8 集群

上一篇:Linux基础篇之系统痕迹查询


下一篇:Linux进程调度