redis cluster 集羣搭建

因爲公司用的是redis4,所以這裏記錄4.0.14(redis4的最後一個版本)。

1.Redis是c開發的,因此安裝redis需要c語言的編譯環境,即需要安裝gcc。

[root@centos125 ~]# gcc -v
-bash: gcc: command not found

說明gcc沒有安裝,安裝命令:

yum -y install gcc gcc-c++

最後看到"Complete!",說明安裝完成,再次使用”gcc  -v“可看到結果:

gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 

2.下載redis源碼。

[root@centos125 ~]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz
-bash: wget: command not found

說明沒有安裝wget,安裝命令:

[root@centos125 ~]# yum -y install wget

看到”Complete!“後,再次執行”wget http://download.redis.io/releases/redis-4.0.14.tar.gz“命令。

或者本地下載後通過winscp傳輸到linux服務器,這樣就不需要安裝wget,本地下載地址:http://download.redis.io/releases/redis-4.0.14.tar.gz

3.解壓。

[root@centos125 ~]# tar zxvf redis-4.0.14.tar.gz 
[root@centos125 ~]# cd redis-4.0.14
[root@centos125 redis-4.0.14]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README.md  redis.conf  runtest  runtest-cluster  runtest-sentinel  sentinel.conf  src  tests  utils

3.1 make命令使用Makefile編譯src目錄下的源碼:

[root@centos125 redis-4.0.14]# make

若最後有報錯:

compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/usr/local/tmp/redis-4.0.10/src'
make: *** [install] Error 2

原因是 jemalloc 重載了Linux下的 ANSIC 的 malloc 和 free 函數。解決辦法:

[root@centos125 redis-4.0.14]# make MALLOC=libc

最後看到:


Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/root/redis-4.0.14/src'

說明源碼編譯完成。

3.2 將編譯後的redis源碼安裝到指定目錄:

[root@centos125 redis-4.0.14]# make install PREFIX=/usr/local/redis-cluster/redis01

查看安裝結果:

[root@centos125 redis-4.0.14]# cd /usr/local/redis-cluster/redis01/
[root@centos125 redis01]# ls
bin
[root@centos125 redis01]# cd bin
[root@centos125 bin]# ls
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

4. 前端啓動。

[root@centos125 bin]# ./redis-server 

前端啓動的話,如果客戶端關閉,redis服務也會停掉,所以需要改成後臺啓動redis。

5. 後臺啓動。

注意:因爲剛纔使用了前端啓動,所以需要先刪除剛纔啓動生成的配置文件,否則,後面創建集羣可能會出現問題(我第一次創建集羣的時候失敗了,懷疑是這個原因)。

[root@centos125 bin]# rm -f dump.rdb

5.1 將redis解壓文件夾下的redis.conf複製到bin目錄下:

[root@centos125 bin]# cp /root/redis-4.0.14/redis.conf /usr/local/redis-cluster/redis01/bin/
[root@centos125 bin]# ls
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server

5.2  修改配置文件:

[root@centos125 bin]# vi redis.conf

需要修改的地方有3處:

5.2.1 將daemonize no --> daemonize yes,這樣便將啓動方式修改爲後臺啓動了。(/dae然後回車,vi下的搜索模式)

5.2.2 將cluster-enabled yes 的註釋放開,集羣模式支持開啓。

5.2.3 默認端口號port 6379 --> port 7001。

5.3 啓動redis-server:

[root@centos125 bin]# ./redis-server redis.conf 
5018:C 06 Jun 16:32:39.062 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5018:C 06 Jun 16:32:39.062 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=5018, just started
5018:C 06 Jun 16:32:39.062 # Configuration loaded

6. 再啓動5個redis-server。

6.1 copy5份redis01文件夾:

[root@centos125 redis-cluster]# cp -r redis01/ redis02
[root@centos125 redis-cluster]# cp -r redis01/ redis03
[root@centos125 redis-cluster]# cp -r redis01/ redis04
[root@centos125 redis-cluster]# cp -r redis01/ redis05
[root@centos125 redis-cluster]# cp -r redis01/ redis06
[root@centos125 redis-cluster]# ls
redis01  redis02  redis03  redis04  redis05  redis06
[root@centos125 redis-cluster]# ls redis02/bin/
dump.rdb  nodes.conf  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis.conf  redis-sentinel  redis-server

6.2 修改另外5份文件夾的redis.conf的port 7001:
 

[root@centos125 redis-cluster]# vi redis02/bin/redis.conf 
[root@centos125 redis-cluster]# vi redis03/bin/redis.conf  
[root@centos125 redis-cluster]# vi redis04/bin/redis.conf  
[root@centos125 redis-cluster]# vi redis05/bin/redis.conf  
[root@centos125 redis-cluster]# vi redis06/bin/redis.conf  

分別修改爲:port 7002,port 7003,port 7004,port 7005,port 7006。

6.3 分別啓動新的5個redis-server:

[root@centos125 bin]# cd ../../redis02/bin/
[root@centos125 bin]#  ./redis-server redis.conf
935266:C 10 Jun 16:19:57.091 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
935266:C 10 Jun 16:19:57.091 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=935266, just started
935266:C 10 Jun 16:19:57.091 # Configuration loaded
[root@centos125 bin]# cd ../../redis03/bin/
[root@centos125 bin]#  ./redis-server redis.conf
936823:C 10 Jun 16:20:06.023 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
936823:C 10 Jun 16:20:06.023 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=936823, just started
936823:C 10 Jun 16:20:06.023 # Configuration loaded
[root@centos125 bin]# cd ../../redis04/bin/
[root@centos125 bin]#  ./redis-server redis.conf
939396:C 10 Jun 16:20:20.822 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
939396:C 10 Jun 16:20:20.822 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=939396, just started
939396:C 10 Jun 16:20:20.822 # Configuration loaded
[root@centos125 bin]# cd ../../redis05/bin/
[root@centos125 bin]#  ./redis-server redis.conf
941763:C 10 Jun 16:20:34.203 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
941763:C 10 Jun 16:20:34.203 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=941763, just started
941763:C 10 Jun 16:20:34.203 # Configuration loaded
[root@centos125 bin]# cd ../../redis06/bin/
[root@centos125 bin]#  ./redis-server redis.conf
944393:C 10 Jun 16:20:49.809 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
944393:C 10 Jun 16:20:49.809 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=944393, just started
944393:C 10 Jun 16:20:49.809 # Configuration loaded

最終6個redis-server啓動成功。

[root@centos125 bin]# ps aux | grep redis
root      930247  0.1  0.0 147364  2744 ?        Ssl  16:19   0:00 ./redis-server 127.0.0.1:7001 [cluster]
root      935276  0.1  0.0 147364  2736 ?        Ssl  16:19   0:00 ./redis-server 127.0.0.1:7002 [cluster]
root      936827  0.1  0.0 147364  2752 ?        Ssl  16:20   0:00 ./redis-server 127.0.0.1:7003 [cluster]
root      939465  0.1  0.0 147364  2752 ?        Ssl  16:20   0:00 ./redis-server 127.0.0.1:7004 [cluster]
root      941766  0.0  0.0 147364  2740 ?        Ssl  16:20   0:00 ./redis-server 127.0.0.1:7005 [cluster]
root      944422  0.0  0.0 147364  2748 ?        Ssl  16:20   0:00 ./redis-server 127.0.0.1:7006 [cluster]
root      945477  0.0  0.0 112708   980 pts/0    S+   16:20   0:00 grep --color=auto redis

至此,啓動6個redis節點結束。

7. 創建redis集羣。

7.1 要搭建集羣的話,需要使用一個工具(腳本文件redis-trib.rb),這個工具在redis解壓文件的src下。因爲這個工具是一個ruby腳本文件,所以這個工具的運行需要ruby的運行環境:

[root@centos125 redis-4.0.14]# yum -y install ruby rubygems

再安裝redis的gem包(若失敗可換成本地下載好後傳到linux服務器,要注意redis.gem包版本需要和redis的版本一致):

[root@centos125 redis-4.0.14]# gem install redis
Fetching: redis-4.1.2.gem (100%)
ERROR:  Error installing redis:
        redis requires Ruby version >= 2.3.0.
[root@centos125 redis-4.0.14]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

報錯解決的參考:CenterOS7——報錯redis requires Ruby version -= 2.2.2

(curl -L get.rvm.io | bash -s stable 失敗時,執行額外的命令:

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

7.2 搭建集羣:

7.2.1 將redis的集羣腳本redis-trib.rb copy到redis-cluster下:

[root@centos125 redis-cluster]# cp /root/redis-4.0.14/src/redis-trib.rb .

7.2.2 執行創建集羣命令:

[root@centos125 redis-cluster]# ./redis-trib.rb create --replicas 1 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 127.0.0.1:7006

命令日誌:

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 20d3e411c9729ab5501668a99098138edbcaf82c 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: af9ec87ee2f6bec0ce791b43a13d1784d9773017 127.0.0.1:7004
   replicates 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a
S: aee308c2d15feda5550d5ceba13d1305d3edb941 127.0.0.1:7005
   replicates 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def
S: bec0dc2ae04ea8d2120e66f72c67134236b0c5dc 127.0.0.1:7006
   replicates 20d3e411c9729ab5501668a99098138edbcaf82c
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:7001)
M: 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 20d3e411c9729ab5501668a99098138edbcaf82c 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: af9ec87ee2f6bec0ce791b43a13d1784d9773017 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a
S: bec0dc2ae04ea8d2120e66f72c67134236b0c5dc 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 20d3e411c9729ab5501668a99098138edbcaf82c
S: aee308c2d15feda5550d5ceba13d1305d3edb941 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

說明創建集羣成功,查看集羣信息:

[root@centos125 redis-cluster]# ./redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 20d3e411c9729ab5501668a99098138edbcaf82c 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: af9ec87ee2f6bec0ce791b43a13d1784d9773017 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a
S: bec0dc2ae04ea8d2120e66f72c67134236b0c5dc 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 20d3e411c9729ab5501668a99098138edbcaf82c
S: aee308c2d15feda5550d5ceba13d1305d3edb941 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

使用redis客戶端登錄集羣后,常用命令:

[root@centos125 redis-cluster]# cd redis01/bin/
[root@centos125 bin]# ./redis-cli -c -p 7001
127.0.0.1:7001> cluster nodes
20d3e411c9729ab5501668a99098138edbcaf82c 127.0.0.1:7002@17002 master - 0 1560155956207 2 connected 5461-10922
0aa8014a9f0cb90f9bf9a77e29df462e1acc447a 127.0.0.1:7003@17003 master - 0 1560155955204 3 connected 10923-16383
af9ec87ee2f6bec0ce791b43a13d1784d9773017 127.0.0.1:7004@17004 slave 0aa8014a9f0cb90f9bf9a77e29df462e1acc447a 0 1560155953197 4 connected
bec0dc2ae04ea8d2120e66f72c67134236b0c5dc 127.0.0.1:7006@17006 slave 20d3e411c9729ab5501668a99098138edbcaf82c 0 1560155955000 6 connected
06ee0333aeac6b0f4cf3161c0e9750c72d8a9def 127.0.0.1:7001@17001 myself,master - 0 1560155954000 1 connected 0-5460
aee308c2d15feda5550d5ceba13d1305d3edb941 127.0.0.1:7005@17005 slave 06ee0333aeac6b0f4cf3161c0e9750c72d8a9def 0 1560155955000 5 connected
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:872
cluster_stats_messages_pong_sent:772
cluster_stats_messages_sent:1644
cluster_stats_messages_ping_received:767
cluster_stats_messages_pong_received:872
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1644

1.查看當前集羣信息

cluster info

2.查看集羣節點信息

cluster nodes

 

參考: redis集羣搭建(非常詳細,適合新手)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章