Linuxでのredisクラスタの構築と導入(詳細)


本文は主にインストールしたredisのクラスタの構築を紹介する
redisがインストールされていない場合は、前の記事を参考にしてください.
https://blog.csdn.net/qq_41047000/article/details/106032905

redisインストールディレクトリの下にディレクトリを作成
cd /usr/local/redis
mkdir redis-cluster
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005

redisの下のredisをconfiファイルを各ディレクトリにコピー
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7000
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7001
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7002
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7003
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7004
cp redis-4.0.8/redis.conf /usr/local/redis/redis-cluster/7005

各フォルダの下のredisを変更します.conf
#  
vim redis-cluster/7000/redis.conf

#    
#    
port 7000
#     
daemonize yes
#     
cluster-enabled yes
#        (          )
cluster-config-file nodes-7000.conf
#        (              )
dir /usr/local/redis/redis-cluster/7000/data/
#         
cluster-node-timeout 5000
#   pid      (          )
pidfile /var/run/redis-7000.pid
#   aof
appendonly yes
# aof    
appendfilename "appendonly-7000.aof"
# rdb    
dbfilename dump-7000.rdb

6つの構成内容を変更してから起動します
cd /usr/local/redis

#                          
bin/redis-server redis-cluster/7000/redis.conf
bin/redis-server redis-cluster/7001/redis.conf
bin/redis-server redis-cluster/7002/redis.conf
bin/redis-server redis-cluster/7003/redis.conf
bin/redis-server redis-cluster/7004/redis.conf
bin/redis-server redis-cluster/7005/redis.conf

起動後のプロセスの表示
[root@frank redis]# ps -ef|grep redis

#           
root      1698     1  0 10:11 ?        00:00:00 bin/redis-server *:7000 [cluster]
root      1728     1  0 10:15 ?        00:00:00 bin/redis-server *:7001 [cluster]
root      1733     1  0 10:15 ?        00:00:00 bin/redis-server *:7002 [cluster]
root      1738     1  0 10:15 ?        00:00:00 bin/redis-server *:7003 [cluster]
root      1743     1  0 10:15 ?        00:00:00 bin/redis-server *:7004 [cluster]
root      1749     1  1 10:15 ?        00:00:00 bin/redis-server *:7005 [cluster]

ここまで6つの単一プロセスのredisを起動し、クラスタの作成を開始し、rubyをインストールします.
yum install ruby rubygems -y

gemを使うにはまずミラーリングします
#        
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/

#      
[root@frank redis]# gem sources -l

*** CURRENT SOURCES ***

https://gems.ruby-china.com/

次にruby-redis接続を実行します
[root@frank redis]# gem install redis 

上の手順で資料を間違えて調べた可能性がありますが、バージョンが低すぎる解決策はrvmをインストールしてからrubyバージョンをアップグレードすることです.
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.3.0.

rubyバージョンのアップグレード(エラーが報告されていない場合は、この手順は必要ありません)
#  curl
 sudo yum install curl

#  rvm
 curl -L get.rvm.io | bash -s stable 

 source /usr/local/rvm/scripts/rvm

#  rvm     ruby  
  rvm list known

#    ruby  
  rvm install  2.4.0

#    ruby  
  rvm use  2.4.0

#        
  rvm remove 2.3.0

#    
  ruby  --version

#   redis    
  gem install redis

クラスタの作成
#  src  
cd /usr/local/redis/redis-4.0.8/src

#    
[root@frank src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 1b76695d84bc510b79eedc42c471ed424d06f8e2 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 25beb35c19a0b5967a874b4e151b719d4df2cfeb 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 225f7ab7cee8a4f77bc41ae7c67b90d179875db3 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: cf995294ef29569ed34a32e8202a378732022c4d 127.0.0.1:7003
   replicates 25beb35c19a0b5967a874b4e151b719d4df2cfeb
S: 31cf1120d517dc2a3c65887799680a06fad03efb 127.0.0.1:7004
   replicates 225f7ab7cee8a4f77bc41ae7c67b90d179875db3
S: c2f3c82b875d57422c66bd89e3a60a6b3dc05ffd 127.0.0.1:7005
   replicates 1b76695d84bc510b79eedc42c471ed424d06f8e2
#      yes  
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 127.0.0.1:7000)
M: 1b76695d84bc510b79eedc42c471ed424d06f8e2 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 225f7ab7cee8a4f77bc41ae7c67b90d179875db3 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 25beb35c19a0b5967a874b4e151b719d4df2cfeb 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: c2f3c82b875d57422c66bd89e3a60a6b3dc05ffd 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 1b76695d84bc510b79eedc42c471ed424d06f8e2
S: cf995294ef29569ed34a32e8202a378732022c4d 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 25beb35c19a0b5967a874b4e151b719d4df2cfeb
S: 31cf1120d517dc2a3c65887799680a06fad03efb 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 225f7ab7cee8a4f77bc41ae7c67b90d179875db3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


ここにクラスタがインストールされているので、テストしてください.
# 7000       ,      7001    ,     key   ,    CRC16  ,   keyname    7001   

[root@frank bin]# ./redis-cli -c -p 7000
127.0.0.1:7000> set name frank
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"frank"

起動時に起動するのが面倒なので、起動してファイルを閉じるように構成します.
#    redis   
cd /usr/local/redis

#      
vi stop-all.sh

#      ,      esc    :wq     
#              ,    bin  
bin/redis-cli -p 7000 shutdown
bin/redis-cli -p 7001 shutdown
bin/redis-cli -p 7002 shutdown
bin/redis-cli -p 7003 shutdown
bin/redis-cli -p 7004 shutdown
bin/redis-cli -p 7005 shutdown

#    
chmod u+x stop-all.sh


#      
vi start-all.sh

#      ,      esc    :wq     
#              ,    bin  
bin/redis-server redis-cluster/7000/redis.conf
bin/redis-server redis-cluster/7001/redis.conf
bin/redis-server redis-cluster/7002/redis.conf
bin/redis-server redis-cluster/7003/redis.conf
bin/redis-server redis-cluster/7004/redis.conf
bin/redis-server redis-cluster/7005/redis.conf

#    
chmod u+x start-all.sh

stop-allを起動します.shファイル
#  
[root@frank redis]# ./stop-all.sh 

#    
[root@frank redis]# ps -ef|grep redis
root     20731 20106  0 15:05 pts/0    00:00:00 grep --color=auto redis

start-allを起動します.shファイル
#    
[root@frank redis]# ./start-all.sh 
20751:C 13 May 15:07:29.734 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20751:C 13 May 15:07:29.735 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20751, just started
20751:C 13 May 15:07:29.735 # Configuration loaded
20753:C 13 May 15:07:29.786 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20753:C 13 May 15:07:29.786 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20753, just started
20753:C 13 May 15:07:29.786 # Configuration loaded
20755:C 13 May 15:07:29.839 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20755:C 13 May 15:07:29.839 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20755, just started
20755:C 13 May 15:07:29.839 # Configuration loaded
20757:C 13 May 15:07:29.948 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20757:C 13 May 15:07:29.949 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20757, just started
20757:C 13 May 15:07:29.949 # Configuration loaded
20768:C 13 May 15:07:30.026 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20768:C 13 May 15:07:30.026 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20768, just started
20768:C 13 May 15:07:30.026 # Configuration loaded
20773:C 13 May 15:07:30.122 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
20773:C 13 May 15:07:30.122 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=20773, just started
20773:C 13 May 15:07:30.123 # Configuration loaded

#    
[root@frank redis]# ps -ef|grep redis
root     20752     1  0 15:07 ?        00:00:00 bin/redis-server *:7000 [cluster]
root     20754     1  0 15:07 ?        00:00:00 bin/redis-server *:7001 [cluster]
root     20756     1  0 15:07 ?        00:00:00 bin/redis-server *:7002 [cluster]
root     20767     1  0 15:07 ?        00:00:00 bin/redis-server *:7003 [cluster]
root     20772     1  0 15:07 ?        00:00:00 bin/redis-server *:7004 [cluster]
root     20777     1  0 15:07 ?        00:00:00 bin/redis-server *:7005 [cluster]

ここまで本文の说明も终わって、亲测は有効で、问题があったらブロガーに伝言を残してください.役に立つと思ったらついでにいいですね.