Maison  >  Article  >  développement back-end  >  Enregistrement complet de la construction du cluster Redis

Enregistrement complet de la construction du cluster Redis

巴扎黑
巴扎黑original
2017-09-21 10:51:022843parcourir

Cet article résume les connaissances de base des concepts de cluster redis, ainsi que les problèmes et solutions rencontrés par les individus lors de la construction de clusters redis. Il est très détaillé. Les amis dans le besoin peuvent s'y référer

. Redis Cluster est un assembly qui permet le partage de données entre plusieurs nœuds Redis.

Le cluster Redis ne prend pas en charge les commandes permettant de traiter plusieurs clés.

Le cluster Redis offre un certain degré de disponibilité grâce au partitionnement. Les commandes peuvent continuer à être traitées lorsqu'un nœud est en panne ou indisponible.

Partagement des données du cluster Redis

Dans le cluster Redis, le partage des données (sharding) est utilisé à la place du hachage de cohérence (hachage de cohérence) pour obtenir un cluster Redis Contient 16384 hachages slots (emplacement de hachage). Chaque clé de la base de données existe dans l'un de ces emplacements de hachage, qui est déterminé en prenant le modulo 16384 après la vérification CRC16.

 Supposons qu'il y ait maintenant trois nœuds dans le cluster, alors

 ①Le nœud A contient les emplacements de hachage 0 à 5500.

 ②Le nœud B contient les emplacements de hachage 5501 à 11000.

 ③Le nœud C contient les emplacements de hachage 11001 à 16384.

Ensuite si on veut ajouter un nœud, on obtiendra une partie des slots des trois nœuds ABC à D. Si nous supprimons un nœud, l'emplacement de séparation du nœud supprimé sera déplacé vers l'emplacement restant.

Modèle de réplication maître-esclave Redis

 Afin d'atteindre la haute disponibilité du cluster Redis. Redis fournit un modèle de réplication maître-esclave. Chaque nœud aura N-1 répliques.

Si nous ajoutons un nœud esclave à chaque nœud lors de la création du cluster, à ce moment, si l'un des nœuds maître raccroche, son nœud esclave sera utilisé comme nouveau nœud maître continue de fournir des services. Mais si le nœud maître et le nœud esclave raccrochent, ils ne peuvent pas continuer à être utilisés.

Construction du cluster Redis

Le cluster Redis se compose de plusieurs nouvelles instances Redis cloud en mode cluster. Le mode cluster de l'instance doit être activé via la configuration.

Ce qui suit est un exemple de fichier de configuration de cluster avec un minimum d'options :


port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
pidfile /var/run/redis_xxxx.pid
dir /usr/local/redis-cluster/xxxx

port spécifie le numéro de port du nœud

L'option cluster-enabled yes est utilisée pour activer le mode cluster de l'instance

cluster-config-file nodes.conf définit le chemin pour enregistrer le fichier de configuration du nœud. Ce fichier n'a pas besoin de le faire. être modifié et est créé au démarrage du cluster.

cluster-node-timeout 5000 : définit le temps d'attente en cas d'échec de la méthode. Autrement dit, si le nœud n'est pas accessible dans les 5 secondes, le nœud est considéré comme indisponible.

appendonly yes : utilisé pour activer la persistance

pidfile /var/run/redis_xxxx.pid définit l'emplacement du fichier pid, où xxxx est le numéro de port

dir /usr /local/redis-cluster/xxxx définit le répertoire de travail, où xxxx est le numéro de port.

Les fichiers du répertoire en ce moment sont comme ça


[root@localhost 10:49 /usr/local/redis-cluster]# ll 7000 7001 7002 7003 7004 7005
7000:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:44 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 09:50 redis-server

7001:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:46 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7002:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:46 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7003:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:47 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7004:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:48 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7005:
总用量 7080
-rw-r--r--. 1 root root  57787 9月 10 10:48 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server
[root@localhost 10:49 /usr/local/redis-cluster]#

Démarrez les six nœuds ci-dessous


[root@localhost 10:51 /usr/local/redis-cluster]# ./7000/redis-server ./7000/redis.conf
3547:C 10 Sep 10:51:58.519 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3547:C 10 Sep 10:51:58.519 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3547, just started
3547:C 10 Sep 10:51:58.519 # Configuration loaded
[root@localhost 10:51 /usr/local/redis-cluster]# ./7001/redis-server ./7001/redis.conf
3552:C 10 Sep 10:52:05.549 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3552:C 10 Sep 10:52:05.550 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3552, just started
3552:C 10 Sep 10:52:05.550 # Configuration loaded
[root@localhost 10:52 /usr/local/redis-cluster]# ./7002/redis-server ./7002/redis.conf
3557:C 10 Sep 10:52:13.098 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3557:C 10 Sep 10:52:13.098 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3557, just started
3557:C 10 Sep 10:52:13.098 # Configuration loaded
[root@localhost 10:52 /usr/local/redis-cluster]# ./7003/redis-server ./7003/redis.conf
3563:C 10 Sep 10:52:18.986 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3563:C 10 Sep 10:52:18.986 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3563, just started
3563:C 10 Sep 10:52:18.986 # Configuration loaded
[root@localhost 10:52 /usr/local/redis-cluster]# ./7004/redis-server ./7004/redis.conf
3568:C 10 Sep 10:52:23.709 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3568:C 10 Sep 10:52:23.710 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3568, just started
3568:C 10 Sep 10:52:23.710 # Configuration loaded
[root@localhost 10:52 /usr/local/redis-cluster]# ./7005/redis-server ./7005/redis.conf
3573:C 10 Sep 10:52:27.146 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3573:C 10 Sep 10:52:27.147 # Redis version=4.0.1, bits=32, commit=00000000, modified=0, pid=3573, just started
3573:C 10 Sep 10:52:27.147 # Configuration loaded
[root@localhost 10:52 /usr/local/redis-cluster]#

Affichez le processus après le démarrage :


[root@localhost 10:52 /usr/local/redis-cluster]# ps aux|grep redis
root   3548 0.2 0.2 41964 2288 ?    Ssl 10:51  0:00 ./7000/redis-server 127.0.0.1:7000 [cluster]
root   3553 0.1 0.2 41964 2288 ?    Ssl 10:52  0:00 ./7001/redis-server 127.0.0.1:7001 [cluster]
root   3558 0.2 0.2 41964 2288 ?    Ssl 10:52  0:00 ./7002/redis-server 127.0.0.1:7002 [cluster]
root   3564 0.1 0.2 41964 2292 ?    Ssl 10:52  0:00 ./7003/redis-server 127.0.0.1:7003 [cluster]
root   3569 0.2 0.2 41964 2292 ?    Ssl 10:52  0:00 ./7004/redis-server 127.0.0.1:7004 [cluster]
root   3574 0.1 0.2 41964 2288 ?    Ssl 10:52  0:00 ./7005/redis-server 127.0.0.1:7005 [cluster]
root   3580 0.0 0.0  6048  784 pts/2  S+  10:52  0:00 grep redis
[root@localhost 10:52 /usr/local/redis-cluster]#

Affichez à nouveau le répertoire des fichiers :


[root@localhost 11:01 /usr/local/redis-cluster]# ll 7000 7001 7002 7003 7004 7005
7000:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:51 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:51 nodes-7000.conf
-rw-r--r--. 1 root root  57787 9月 10 10:44 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 09:50 redis-server

7001:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:52 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:52 nodes-7001.conf
-rw-r--r--. 1 root root  57787 9月 10 10:46 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7002:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:52 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:52 nodes-7002.conf
-rw-r--r--. 1 root root  57787 9月 10 10:46 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7003:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:52 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:52 nodes-7003.conf
-rw-r--r--. 1 root root  57787 9月 10 10:47 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7004:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:52 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:52 nodes-7004.conf
-rw-r--r--. 1 root root  57787 9月 10 10:48 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server

7005:
总用量 7084
-rw-r--r--. 1 root root    0 9月 10 10:52 appendonly.aof
-rw-r--r--. 1 root root   114 9月 10 10:52 nodes-7005.conf
-rw-r--r--. 1 root root  57787 9月 10 10:48 redis.conf
-rwxr-xr-x. 1 root root 7185872 9月 10 10:04 redis-server
[root@localhost 11:01 /usr/local/redis-cluster]#

Vous pouvez voir que les fichiers aof et nodes sont générés, et un fichier de dump sera généré lors de la sauvegarde.

En regardant le pid


[root@localhost 11:03 /usr/local/redis-cluster]# ll /var/run/redis*
-rw-r--r--. 1 root root 5 9月 10 10:51 /var/run/redis_7000.pid
-rw-r--r--. 1 root root 5 9月 10 10:52 /var/run/redis_7001.pid
-rw-r--r--. 1 root root 5 9月 10 10:52 /var/run/redis_7002.pid
-rw-r--r--. 1 root root 5 9月 10 10:52 /var/run/redis_7003.pid
-rw-r--r--. 1 root root 5 9月 10 10:52 /var/run/redis_7004.pid
-rw-r--r--. 1 root root 5 9月 10 10:52 /var/run/redis_7005.pid
[root@localhost 11:03 /usr/local/redis-cluster]#

Ensuite, nous devons utiliser l'outil redis-trib et ces six nœuds pour créer un cluster.

redis-trib se trouve dans le répertoire src du code source redis.

Nous copions une copie dans le répertoire redis-cluster.


[root@localhost 11:03 /usr/local/redis-cluster]# ll ../redis/src/redis-trib*
-rwxrwxr-x. 1 root root 60843 7月 24 22:58 ../redis/src/redis-trib.rb
[root@localhost 11:04 /usr/local/redis-cluster]# cp ../redis/src/redis-trib.rb ./
[root@localhost 11:05 /usr/local/redis-cluster]# ll
总用量 84
drwxr-xr-x. 2 root root 4096 9月 10 10:51 7000
drwxr-xr-x. 2 root root 4096 9月 10 10:52 7001
drwxr-xr-x. 2 root root 4096 9月 10 10:52 7002
drwxr-xr-x. 2 root root 4096 9月 10 10:52 7003
drwxr-xr-x. 2 root root 4096 9月 10 10:52 7004
drwxr-xr-x. 2 root root 4096 9月 10 10:52 7005
-rwxr-xr-x. 1 root root 60843 9月 10 11:05 redis-trib.rb
[root@localhost 11:05 /usr/local/redis-cluster]#

Démarrez le cluster :


[root@localhost 11:13 /usr/local/redis-cluster]# ./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
/usr/bin/env: ruby: 没有那个文件或目录
[root@localhost 11:15 /usr/local/redis-cluster]#

Ce qui suit est ma version d'installation ayant échoué, et là sont aussi d'autres versions ultérieures réussies.

Parce qu'il s'agit d'un programme Ruby, nous devons installer Ruby,

yum -y install  ruby

Continuez pour démarrer le cluster :


[root@localhost 11:21 /usr/local/redis-cluster]# ./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
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
    from ./redis-trib.rb:24
[root@localhost 11:22 /usr/local/redis-cluster]#

Une erreur est toujours signalée, disant que rubygems est nécessaire, nous continuons l'installation :

yum -y install rubygems

Continuons pour démarrer le cluster :


[root@localhost 11:24 /usr/local/redis-cluster]# ./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
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from ./redis-trib.rb:25
[root@localhost 11:24 /usr/local/redis-cluster]#

L'erreur est toujours signalée..., nous devons installer une version supérieure de Ruby.

J'ai donc décidé de désinstaller Ruby 1.8 en premier.

yum -y remove ruby

Puis réinstallez


wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
tar xzvf ruby-2.4.1.tar.gz
cd ruby-2.4.1
./configure
make
make install

Puis vérifiez la version Ruby :


[root@localhost 12:47 ~]# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [i686-linux]
[root@localhost 12:47 ~]#

Enfin, pour exécuter redis-trib, nous devons installer la gemme redis.

Si l'erreur suivante se produit


[root@localhost 13:01 /usr/local/redis-cluster]# gem install redis
ERROR: Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
  undefined method `invoke_with_build_args' for nil:NilClass

On continue d'ouvrir le cluster : l'erreur est toujours signalée, et après plusieurs rebondissements, elle n'a toujours pas été résolu

Le mieux est de trouver la méthode suivante.

Version réussie

Installez Ruby via rvm, l'outil de gestion Ruby.

Installez d'abord rvm. Si la commande suivante indique que curl est introuvable, vous pouvez installer crul via yum install -y crul.


[root@localhost 19:45 ~]# curl -L get.rvm.io | bash -s stable
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100 24090 100 24090  0   0 11517   0 0:00:02 0:00:02 --:--:-- 48276
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 05时59分21秒 JST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 无法检查签名:No public key
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

  gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

  command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

the key can be compared with:

  https://rvm.io/mpapis.asc
  https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

[root@localhost 19:45 ~]#

Nous avons constaté que cela avait échoué, nous avons suivi les instructions.


[root@localhost 19:45 ~]# curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
gpg: 钥匙环‘/root/.gnupg/secring.gpg'已建立
gpg: /root/.gnupg/trustdb.gpg:建立了信任度数据库
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) <mpapis@gmail.com>”已导入
gpg: 合计被处理的数量:1
gpg:      已导入:1 (RSA: 1)
gpg: 没有找到任何绝对信任的密钥
[root@localhost 19:45 ~]# curl -L get.rvm.io | bash -s stable
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100 24090 100 24090  0   0  3763   0 0:00:06 0:00:06 --:--:-- 3763
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 05时59分21秒 JST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”
gpg:        亦即“Michal Papis <michal.papis@toptal.com>”
gpg:        亦即“[jpeg image of size 5015]”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:    没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3
子钥指纹: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17
GPG verified &#39;/usr/local/rvm/archives/rvm-1.29.3.tgz&#39;
Creating group &#39;rvm&#39;

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

 * First you need to add all users that will be using rvm to &#39;rvm&#39; group,
  and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

 * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
  in all your open shell windows, in rare cases you need to reopen all shell windows.
[root@localhost 19:46 ~]

Il s'avère que rvm a été installé avec succès Pour que la configuration prenne effet immédiatement, nous devons utiliser la commande source pour importer la configuration rvm. déposer.

Vérifiez d'abord le chemin du fichier de configuration, puis importez-le.


[root@localhost 19:46 ~]# find / -name rvm.sh
/etc/profile.d/rvm.sh
[root@localhost 19:49 ~]# source /etc/profile.d/rvm.sh

接下来就可以安装高版本的ruby了,建议安装2.2.3版本,不然的话,等下gen install redis的时候,可能会报下面的错误:


[root@localhost 19:59 ~]# gem install redis
Fetching: redis-4.0.0.gem (100%)
ERROR: Error installing redis:
    redis requires Ruby version >= 2.2.2.

废话少说,接下来安装2.2.3版本的ruby


[root@localhost 20:16 ~]# rvm install 2.3.3
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/6/i386/ruby-2.3.3.
Continuing with compilation. Please read &#39;rvm help mount&#39; to get more information on binary rubies.
Checking requirements for centos.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.3.3, this may take a while depending on your cpu(s)...
ruby-2.3.3 - #downloading ruby-2.3.3, this may take a while depending on your connection...
 % Total  % Received % Xferd Average Speed  Time  Time   Time Current
                 Dload Upload  Total  Spent  Left Speed
100 13.7M 100 13.7M  0   0 1285k   0 0:00:10 0:00:10 --:--:-- 1394k
ruby-2.3.3 - #extracting ruby-2.3.3 to /usr/local/rvm/src/ruby-2.3.3....
ruby-2.3.3 - #applying patch /usr/local/rvm/patches/ruby/ruby_2_3_gcc7.patch.
ruby-2.3.3 - #applying patch /usr/local/rvm/patches/ruby/2.3.3/random_c_using_NR_prefix.patch.
ruby-2.3.3 - #configuring..........................................................
ruby-2.3.3 - #post-configuration..
ruby-2.3.3 - #compiling....................................................................................-
ruby-2.3.3 - #installing...........................
ruby-2.3.3 - #making binaries executable..
ruby-2.3.3 - #downloading rubygems-2.6.13
ruby-2.3.3 - #extracting rubygems-2.6.13.....
ruby-2.3.3 - #removing old rubygems.........
ruby-2.3.3 - #installing rubygems-2.6.13..........................
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3@global
ruby-2.3.3 - #importing gemset /usr/local/rvm/gemsets/global.gems..........................................|
ruby-2.3.3 - #generating global wrappers........
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3
ruby-2.3.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.3 - #generating default wrappers........
ruby-2.3.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.3.3 - #complete
Please be aware that you just installed a ruby that requires 2 patches just to be compiled on an up to date linux system.
This may have known and unaccounted for security vulnerabilities.
Please consider upgrading to ruby-2.4.1 which will have all of the latest security patches.
Ruby was built without documentation, to build it run: rvm docs generate-ri
[root@localhost 20:27 ~]# ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [i686-linux]
[root@localhost 20:28 ~]# gem -v
2.6.13
[root@localhost 20:28 ~]#

可以看到,不进ruby安装好了2.3.3版本,还把rubygems也安装好了。

如果要删除掉某个版本的ruby可以用这个命令:rvm remove 1.9.3

我们继续安装:


[root@localhost 20:28 ~]# gem install redis
Fetching: redis-4.0.0.gem (100%)
Successfully installed redis-4.0.0
Parsing documentation for redis-4.0.0
Installing ri documentation for redis-4.0.0
Done installing documentation for redis after 3 seconds
1 gem installed
[root@localhost 20:33 ~]#

看到这里,折腾了两个晚上终于弄好了。接下来就是使用我们的redis-trib开启集群了。

为了方便,我把redis-trib.rb这个文件复制了一份到/usr/local/bin这个目录下,因为这个目录在PATH下面,里面的命令可以直接执行。

再次确认下六个redis节点的运行是ok的。


[root@localhost 20:35 /usr/local/redis-cluster]# ps aux|grep redis
root   2486 0.1 0.2 46060 2304 ?    Ssl 18:30  0:12 ./7000/redis-server 127.0.0.1:7000 [cluster]
root   2491 0.1 0.2 46060 2308 ?    Ssl 18:30  0:13 ./7001/redis-server 127.0.0.1:7001 [cluster]
root   2496 0.1 0.2 46060 2308 ?    Ssl 18:30  0:13 ./7002/redis-server 127.0.0.1:7002 [cluster]
root   2501 0.1 0.2 46060 2304 ?    Ssl 18:30  0:13 ./7003/redis-server 127.0.0.1:7003 [cluster]
root   2506 0.1 0.2 46060 2308 ?    Ssl 18:30  0:14 ./7004/redis-server 127.0.0.1:7004 [cluster]
root   2511 0.1 0.2 46060 2304 ?    Ssl 18:30  0:12 ./7005/redis-server 127.0.0.1:7005 [cluster]
root   31426 0.0 0.0  6048  784 pts/2  S+  20:35  0:00 grep redis
[root@localhost 20:35 /usr/local/redis-cluster]#


[root@localhost 20:35 /usr/local/redis-cluster]# 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:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2 127.0.0.1:7000
  slots:0-5460 (5461 slots) master
M: 9bf2ee5d32fc350ec411d3eaad18f82492796e99 127.0.0.1:7001
  slots:5461-10922 (5462 slots) master
M: 6de604de12b4b4a3be46766bb95ccf4618dc0d75 127.0.0.1:7002
  slots:10923-16383 (5461 slots) master
S: f1bdbd841e37825169426486d6502e2cd99b76fe 127.0.0.1:7003
  replicates 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2
S: 3ff95d180ec47fc58c9c5f73e2f54354128bfe7e 127.0.0.1:7004
  replicates 9bf2ee5d32fc350ec411d3eaad18f82492796e99
S: a4a09d80a4bb6c82345c376c59e5ae49e1d49701 127.0.0.1:7005
  replicates 6de604de12b4b4a3be46766bb95ccf4618dc0d75
Can I set the above configuration? (type &#39;yes&#39; 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: 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2 127.0.0.1:7000
  slots:0-5460 (5461 slots) master
  1 additional replica(s)
S: f1bdbd841e37825169426486d6502e2cd99b76fe 127.0.0.1:7003
  slots: (0 slots) slave
  replicates 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2
M: 6de604de12b4b4a3be46766bb95ccf4618dc0d75 127.0.0.1:7002
  slots:10923-16383 (5461 slots) master
  1 additional replica(s)
M: 9bf2ee5d32fc350ec411d3eaad18f82492796e99 127.0.0.1:7001
  slots:5461-10922 (5462 slots) master
  1 additional replica(s)
S: 3ff95d180ec47fc58c9c5f73e2f54354128bfe7e 127.0.0.1:7004
  slots: (0 slots) slave
  replicates 9bf2ee5d32fc350ec411d3eaad18f82492796e99
S: a4a09d80a4bb6c82345c376c59e5ae49e1d49701 127.0.0.1:7005
  slots: (0 slots) slave
  replicates 6de604de12b4b4a3be46766bb95ccf4618dc0d75
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost 20:37 /usr/local/redis-cluster]#

在创建的过程中,redis会给出一份预想的配置给我们看,确定没问题就输入yes并回车。系统就给我们创建了三个主,三个从的集群结构。

可以看到All 16384 slots covered。

redis-cli

redis-cli默认情况下连接的是本地的6379端口的redis服务器。

现在有六个端口,所以需要指定参数。

redis-cli -h xxx -p xxx -a xxx

-h:指定服务器

-p:指定端口号

-a:指定密码

-c:开启集群模式


[root@localhost 20:53 /usr/local/redis-cluster]# redis-cli -c -p 7000
127.0.0.1:7000> set name zhangsan
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> exit
[root@localhost 20:53 /usr/local/redis-cluster]# redis-cli -c -p 7002
127.0.0.1:7002> get name
-> Redirected to slot [5798] located at 127.0.0.1:7001
"zhangsan"
127.0.0.1:7001>

可以看到,已经可以共享数据了。

 但是,如果我们把某一个主节点杀掉呢?


[root@localhost 19:52 /usr/local/redis-cluster]# redis-cli -p 7002 debug segfault
Error: Server closed the connection
[root@localhost 19:53 /usr/local/redis-cluster]# redis-trib.rb check 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2 127.0.0.1:7000
  slots:0-5460 (5461 slots) master
  1 additional replica(s)
M: 9bf2ee5d32fc350ec411d3eaad18f82492796e99 127.0.0.1:7001
  slots:5461-10922 (5462 slots) master
  1 additional replica(s)
S: 3ff95d180ec47fc58c9c5f73e2f54354128bfe7e 127.0.0.1:7004
  slots: (0 slots) slave
  replicates 9bf2ee5d32fc350ec411d3eaad18f82492796e99
M: a4a09d80a4bb6c82345c376c59e5ae49e1d49701 127.0.0.1:7005
  slots:10923-16383 (5461 slots) master
  0 additional replica(s)
S: f1bdbd841e37825169426486d6502e2cd99b76fe 127.0.0.1:7003
  slots: (0 slots) slave
  replicates 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost 19:54 /usr/local/redis-cluster]#

可以看到7005端口的节点已经变成主节点了。那如果继续把7005节点也宕掉呢?


[root@localhost 19:58 /usr/local/redis-cluster]# redis-cli -p 7005 debug segfault
Error: Server closed the connection
[root@localhost 19:58 /usr/local/redis-cluster]# redis-trib.rb check 127.0.0.1:7000
[ERR] Sorry, can&#39;t connect to node 127.0.0.1:7005
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2 127.0.0.1:7000
  slots:0-5460 (5461 slots) master
  1 additional replica(s)
M: 9bf2ee5d32fc350ec411d3eaad18f82492796e99 127.0.0.1:7001
  slots:5461-10922 (5462 slots) master
  1 additional replica(s)
S: 3ff95d180ec47fc58c9c5f73e2f54354128bfe7e 127.0.0.1:7004
  slots: (0 slots) slave
  replicates 9bf2ee5d32fc350ec411d3eaad18f82492796e99
S: f1bdbd841e37825169426486d6502e2cd99b76fe 127.0.0.1:7003
  slots: (0 slots) slave
  replicates 6d996f9e34f40b02afe06aa9d3c8f18a41875cb2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
[root@localhost 19:58 /usr/local/redis-cluster]#

可以看到集群已经出现问题了。

追记:

在搭建多机多点集群的时候,遇到以下问题:

1、一直在等待...

>>> 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.......................

解决办法:确认端口是否开放。如果没有开发,可以修改/etc/sysconfig/iptables文件,开放端口(复制一行前面开放端口的来修改),然后service iptables restart重启防火墙。

极端的可以service iptables stop来关闭防火墙

2、槽被利用


/usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb:119:in `call&#39;: ERR Slot 5461 is already busy (Redis::CommandError)
 from /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis.rb:2742:in `block in method_missing&#39;
 from /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis.rb:45:in `block in synchronize&#39;
 from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize&#39;
 from /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis.rb:45:in `synchronize&#39;
 from /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis.rb:2741:in `method_missing&#39;
 from /usr/local/bin/redis-trib.rb:212:in `flush_node_config&#39;
 from /usr/local/bin/redis-trib.rb:776:in `block in flush_nodes_config&#39;
 from /usr/local/bin/redis-trib.rb:775:in `each&#39;
 from /usr/local/bin/redis-trib.rb:775:in `flush_nodes_config&#39;
 from /usr/local/bin/redis-trib.rb:1296:in `create_cluster_cmd&#39;
 from /usr/local/bin/redis-trib.rb:1700:in `<main>&#39;

解决办法:删除掉在redis.conf的dir目录下的几个文件redis.conf文件除外。

如果还是有相同的问题,可以考虑对所有节点执行下面两个命令。

redis-cli -p 端口号-h 主机物理地址 FLUSHALL

redis-cli -p 端口号-h 主机物理地址 CLUSTER RESET SOFT

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn