1. 程式人生 > >redis3.2集群搭建

redis3.2集群搭建

redis 集群

在一臺機器上搭建redis集群 redis版本3.2

我這也是需要搭建然後網上找文檔然最後總結的
參考:https://blog.csdn.net/u012042021/article/details/72818759

1,解壓源碼包安裝redis

#安裝需要的一些包
yum install ruby rubygem openssl 

#此步安裝時候,是沒有輸出的,只要不報錯慢慢等待就好了或者加 -V 參數看過程
gem install redis --version 3.0.0

#解壓安裝
tar xf redis-3.2.9.tar.gz
cd redis-3.2.9
make
make install PREFIX=/usr/local/redis #把編譯好的redis命令安裝到這個目錄下面

2,配置redis節點;redis3.x 最少需要6個節點[3主3從的模式]</p>

#配置節點目錄
mkdir -p /opt/redis/ && cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005
cp -rf /usr/local/redis/bin 7000
cp -rf /usr/local/redis/bin 7001
cp -rf /usr/local/redis/bin 7002
cp -rf /usr/local/redis/bin 7003
cp -rf /usr/local/redis/bin 7004
cp -rf /usr/local/redis/bin 7005

#節點配置文件
cd /opt/redis/7000
vim redis.conf
bind 192.168.16.186 127.0.0.1 #這裏可以多個ip用空格分開
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-require-full-coverage no
appendonly yes
pidfile /var/run/redis_7000.pid
logfile /var/log/redis/redis_7000.log
dbfilename dump_7000.rdb
appendfilename "appendonly_7000.aof"
其他幾個節點的配置文件和這個一樣就行,需要註意需要改動對應端口和一些文件名稱

#創建日誌目錄
mkdir /var/log/redis

#啟動各節點
cd /opt/redis/7000
./bin/redis-server ./redis.conf
cd /opt/redis/7001
./bin/redis-server ./redis.conf
cd /opt/redis/7002
./bin/redis-server ./redis.conf
cd /opt/redis/7003
./bin/redis-server ./redis.conf
cd /opt/redis/7004
./bin/redis-server ./redis.conf
cd /opt/redis/7005
./bin/redis-server ./redis.conf

3,創建集群

#進去源碼包目錄下src目錄下面,執行集群創建
cd redis-3.2.9/src/
./redis-trib.rb create --replicas 1 192.168.16.186:7000 192.168.16.186:7001 192.168.16.186:7002 192.168.16.186:7003 192.168.16.186:7004 192.168.16.186:7005
>>> Creating cluster
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 25 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 26 (expected array)

......此處省略......

/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 104 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release

>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.16.186:7000
192.168.16.186:7001
192.168.16.186:7002
Adding replica 192.168.16.186:7003 to 192.168.16.186:7000
Adding replica 192.168.16.186:7004 to 192.168.16.186:7001
Adding replica 192.168.16.186:7005 to 192.168.16.186:7002
M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000
   slots:0-5460 (5461 slots) master
M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001
   slots:5461-10922 (5462 slots) master
M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002
   slots:10923-16383 (5461 slots) master
S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003
   replicates 346226a5653bfe33f6d887920e99609b9bbe94ec
S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004
   replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc
S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005
   replicates be357c14ac96528f86b5a6d5457086e307d05b7e
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..
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array)

......此處省略......

/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
>>> Performing Cluster Check (using node 192.168.16.186:7000)
M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004
   slots: (0 slots) slave
   replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc
M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005
   slots: (0 slots) slave
   replicates be357c14ac96528f86b5a6d5457086e307d05b7e
S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003
   slots: (0 slots) slave
   replicates 346226a5653bfe33f6d887920e99609b9bbe94ec
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

技術分享圖片
輸入 yes
技術分享圖片
創建成功!
4,連接測試

[root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186
192.168.16.186:7000> set testkey 123456
OK
192.168.16.186:7000> get testkey
"123456"
192.168.16.186:7000> exit
[root@localhost ~]# redis-cli -c -p 7005 -h 192.168.16.186
192.168.16.186:7005> get testkey
-> Redirected to slot [4757] located at 192.168.16.186:7000
"123456"
192.168.16.186:7000> exit

#查看節點信息
[root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186
192.168.16.186:7000> CLUSTER NODES
4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004 slave 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 0 1522393285325 5 connected
be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002 master - 0 1522393286828 3 connected 10923-16383
5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001 master - 0 1522393284824 2 connected 5461-10922
9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005 slave be357c14ac96528f86b5a6d5457086e307d05b7e 0 1522393286828 6 connected
346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000 myself,master - 0 0 1 connected 0-5460
99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003 slave 346226a5653bfe33f6d887920e99609b9bbe94ec 0 1522393286328 4 connected

5,redis 集群的啟動、停止[使用腳本]

啟動
[root@localhost redis]# cat start-redis.sh 
#!/bin/bash
#7000
cd /opt/redis/7000
./bin/redis-server ./redis.conf

#7001
cd /opt/redis/7001
./bin/redis-server ./redis.conf

#7002
cd /opt/redis/7002
./bin/redis-server ./redis.conf

#7003
cd /opt/redis/7003
./bin/redis-server ./redis.conf

#7004
cd /opt/redis/7004
./bin/redis-server ./redis.conf

#7005
cd /opt/redis/7005
./bin/redis-server ./redis.conf

#查看redis進程
ps -ef |grep redis

#停止
[root@localhost redis]# cat stop-redis.sh 
#!/bin/bash
/opt/redis/7000/bin/redis-cli -p 7000 -h 192.168.16.186 shutdown
/opt/redis/7001/bin/redis-cli -p 7001 -h 192.168.16.186 shutdown
/opt/redis/7002/bin/redis-cli -p 7002 -h 192.168.16.186 shutdown
/opt/redis/7003/bin/redis-cli -p 7003 -h 192.168.16.186 shutdown
/opt/redis/7004/bin/redis-cli -p 7004 -h 192.168.16.186 shutdown
/opt/redis/7005/bin/redis-cli -p 7005 -h 192.168.16.186 shutdown
ps -ef |grep redis

技術分享圖片
技術分享圖片

2臺機器搭建集群
每臺3個節點;配置大同小異這裏就不在貼出過程了
1,同樣道理安裝不變,配置節點每臺為3個就行,2臺機器6個節點即可;端口一樣不一樣都可以
2,配置文件bind的IP地址修改,兩臺機器要互通
3,創建集群時在2臺機器任意一臺即可,註意節點地址和端口的變化

redis3.2集群搭建