redis 3マスタ3スレーブ構築(2台の仮想マシン)

19567 ワード

環境centos 7 ip:192.168.20.181 192.168.20.182を準備し、インストール依存
yum -y install gcc gcc-c++ wget

2、ディレクトリの作成
mkdir -p /redis/data

3、ディレクトリに入り、redisインストールパッケージをダウンロードし、インストールを解凍し、コンパイルする
cd /redis/data/
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
tar zxf redis-5.0.2.tar.gz
make && make install

4、別の機械の上で同じ操作で、redisをインストールしてクラスタを構築し始めました.ここで構築したのはredisクラスタの3主3従なので、3つのmasterノードと3つのslaveノードが必要で、6つのノードが必要です.1 2台のサーバの下にclusterディレクトリを作成する
mkdir -p /redis/cluster
cd /redis/cluster/
mkdir 7001 7002 7003
ls
7001  7002  7003

5、別のサーバーで同様に操作し、700470057006 2を作成し、プロファイルを変更します.
daemonize yes
port 7001(               )
dir /redis/cluster/7001/(          ,            ,       )
cluster-enabled yes(      )
cluster-config-file nodes-7001.conf(        ,  700x   port   )
cluster-node-timeout 5000
bind 192.168.20.181
protected-mode no (      )
appendonly yes
dbfilename dump_7002.rdb

3、変更したプロファイルをそれぞれ7001-7003にコピーし、応答するポート、ファイル名を変更します.
cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7001/
cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7002/
cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7003/

4,redisをそれぞれ起動
cd /redis/redis-5.0.2/src/
./redis-server /redis/cluster/7001/redis.conf 
./redis-server /redis/cluster/7002/redis.conf 
./redis-server /redis/cluster/7003/redis.conf
ps -ef | grep redis
root      65904      1  0 11:50 ?        00:00:00 ./redis-server 192.168.20.181:7001 [cluster]
root      65909      1  0 11:51 ?        00:00:00 ./redis-server 192.168.20.181:7002 [cluster]
root      65915      1  0 11:51 ?        00:00:00 ./redis-server 192.168.20.181:7003 [cluster]
root      65928   5474  0 11:51 pts/0    00:00:00 grep --color=auto redis

5、ファイアウォールとselinuxを閉じる
systemctl stop firewalld.service 
setenforce 0

6,181のプロファイルを182にコピーする
scp /redis/cluster/7001/redis.conf root@192.168.20.182:/redis/cluster/7004
scp /redis/cluster/7002/redis.conf root@192.168.20.182:/redis/cluster/7005
scp /redis/cluster/7003/redis.conf root@192.168.20.182:/redis/cluster/7006

7、rubyをインストールする
[root@bogon ~]# yum -y install ruby
[root@bogon ~]# gem install redis
Fetching: redis-4.1.4.gem (100%)
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.3.0

8、クラスタの作成
[root@bogon ~]# /redis/redis-5.0.2/src/redis-cli --cluster create 192.168.20.181:7001 192.168.20.181:7002 192.168.20.181:7003 192.168.20.182:7004 192.168.20.182:7005 192.168.20.182:7006 --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.20.182:7005 to 192.168.20.181:7001
Adding replica 192.168.20.181:7003 to 192.168.20.182:7004
Adding replica 192.168.20.182:7006 to 192.168.20.181:7002
M: 8190ed94b58c8c0320ec8584ebba0ae6503199fb 192.168.20.181:7001
   slots:[0-5460] (5461 slots) master
M: c11233b8a36dc32b0a4de35a4f500acbddd99d08 192.168.20.181:7002
   slots:[10923-16383] (5461 slots) master
S: e09d9147ab92d73fb24cd9ad22c5e9a608594134 192.168.20.181:7003
   replicates d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3
M: d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3 192.168.20.182:7004
   slots:[5461-10922] (5462 slots) master
S: eefe3ac899aef116a01cc09d6eeda33ee4d92bd0 192.168.20.182:7005
   replicates 8190ed94b58c8c0320ec8584ebba0ae6503199fb
S: 764595d022ebfb64a8f46d05440053f34e91b508 192.168.20.182:7006
   replicates c11233b8a36dc32b0a4de35a4f500acbddd99d08
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.20.181:7001)
M: 8190ed94b58c8c0320ec8584ebba0ae6503199fb 192.168.20.181:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c11233b8a36dc32b0a4de35a4f500acbddd99d08 192.168.20.181:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 764595d022ebfb64a8f46d05440053f34e91b508 192.168.20.182:7006
   slots: (0 slots) slave
   replicates c11233b8a36dc32b0a4de35a4f500acbddd99d08
M: d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3 192.168.20.182:7004
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e09d9147ab92d73fb24cd9ad22c5e9a608594134 192.168.20.181:7003
   slots: (0 slots) slave
   replicates d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3
S: eefe3ac899aef116a01cc09d6eeda33ee4d92bd0 192.168.20.182:7005
   slots: (0 slots) slave
   replicates 8190ed94b58c8c0320ec8584ebba0ae6503199fb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

9、テスト
[root@bogon ~]# /redis/redis-5.0.2/src/redis-cli -h 192.168.20.181 -p 7001
192.168.20.181:7001> set bb 22
OK
192.168.20.181:7001> get bb
"22"
192.168.20.181:7001> 
[root@bogon src]# /redis/redis-5.0.2/src/redis-cli -c -h 192.168.20.182 -p 7006
192.168.20.182:7006> get bb
-> Redirected to slot [1180] located at 192.168.20.181:7001
"22"
192.168.20.181:7001>