1. 程式人生 > 實用技巧 >[2020多校A層12.1] 禮物

[2020多校A層12.1] 禮物

redis叢集搭建

尊重原創,轉載請標明出處:https://www.cnblogs.com/liyasong

目錄

正文

    為了保證可以進行投票,需要至少3個主節點。每個主節點都需要至少一個從節點,所以需要至少3個從節點所以一共需要6臺redis伺服器,為了模擬6個redis節點,我們可以使用6個redis例項作為節點。6個redis例項的埠號,7001~7006.

    首先在我們的linux中安裝一個redis,如果不會的同學,可以去看我之前的部落格《redis安裝-單機版》然後: 

    1. 把bin目錄裡面的rdb,和aof檔案刪除,準備乾淨的redis

    [root@localhost bin]#rm -rf appendonly.aof

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

    完事效果如下:

    

    退出到上級目錄,將bin複製6份

    把bin複製6份

    [root@localhost bin]#cd ..

    如果沒有redis-cluster資料夾的話需要建立一個

    [root@localhost redis]#mkdir redis-cluster

    [root@localhost redis]#cp -r bin redis-cluster/redis1

    [root@localhost redis]#cp -r bin redis-cluster/redis2

    [root@localhost redis]#cp -r bin redis-cluster/redis3

    [root@localhost redis]#cp -r bin redis-cluster/redis4

    [root@localhost redis]#cp -r bin redis-cluster/redis5

    [root@localhost redis]#cp -r bin redis-cluster/redis6

   這個時候,我們進入redis-cluster目錄下看到的結果是這樣的:

   

   現在,為了讓這6個redis節點執行的時候不起節點的衝突,我們需要修改它們的節點埠號。將埠號一次修改為7001~7006

   首先開啟第一個節點的redis.conf 配置檔案:[root@itcast-01 redis-cluster]#vim redis1/redis.conf

   將埠號修改為7001(將這的6379改為7001)

    

  剩下的程式碼用同樣的方法修改即可。

  分別啟動6個redis節點的服務,或者是寫一個指令碼,然後給指令碼附上執行許可權,然後用指令碼啟動也可以。

回到頂部

 準備叢集安裝環境

  redis叢集的管理工具使用的是ruby指令碼語言,安裝叢集需要ruby環境。

  安裝ruby環境[root@itcast-01 redis-cluster]#yum install ruby

  安裝Ruby的打包系統[root@itcast-01 redis-cluster]# yum install rubygems

  下載一個redis-3.0.0.gem的檔案,然後將檔案上傳,再執行命令安轉。

  

  [root@itcast-01 ~]#gem install redis-3.0.0.gem

  

回到頂部

  安裝叢集

  修改這6個例項的redis.conf配置檔案,開啟redis-cluster

  [root@localhost redis-cluster]#vim redis1/redis.conf

  

  然後再次重啟例項

  叢集管理工具在redis解壓資料夾的src的資料夾中,使用redis-cluster的叢集管理工具啟動叢集

  

  先進入叢集管理工具所在的路徑:[root@localhost redis-cluster]#cd /root/redis-3.0.0/src/

  再啟動命令:[root@localhost src]#./redis-trib.rb create --replicas 1 192.168.37.161:7001 192.168.37.161:7002 192.168.37.161:7003 192.168.37.161:7004 192.168.37.161:7005 192.168.37.161:7006

  此時的啟動資訊如下:

  

>>> Creating cluster
Connecting to node 192.168.37.131:7001: OK
Connecting to node 192.168.37.131:7002: OK
Connecting to node 192.168.37.131:7003: OK
Connecting to node 192.168.37.131:7004: OK
Connecting to node 192.168.37.131:7005: OK
Connecting to node 192.168.37.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.37.131:7001
192.168.37.131:7002
192.168.37.131:7003
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
   slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
   slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
   slots:10923-16383 (5461 slots) master
S: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
   replicates 955567f988830cdf4328854f815719ea28082ca8
S: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
   replicates 4f3eeced04b930aa48193699301745a05a70697a
S: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
   replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
Can I set the above configuration? (type 'yes' to accept):
>>> Creating cluster
Connecting to node 192.168.37.131:7001: OK
Connecting to node 192.168.37.131:7002: OK
Connecting to node 192.168.37.131:7003: OK
Connecting to node 192.168.37.131:7004: OK
Connecting to node 192.168.37.131:7005: OK
Connecting to node 192.168.37.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.37.131:7001
192.168.37.131:7002
192.168.37.131:7003
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
   slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
   slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
   slots:10923-16383 (5461 slots) master
S: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
   replicates 955567f988830cdf4328854f815719ea28082ca8
S: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
   replicates 4f3eeced04b930aa48193699301745a05a70697a
S: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
   replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
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.37.131:7001)
M: 955567f988830cdf4328854f815719ea28082ca8 192.168.37.131:7001
   slots:0-5460 (5461 slots) master
M: 4f3eeced04b930aa48193699301745a05a70697a 192.168.37.131:7002
   slots:5461-10922 (5462 slots) master
M: 871a684dbbc0f43dcc16107710d7bd2f4e6de76a 192.168.37.131:7003
   slots:10923-16383 (5461 slots) master
M: baca3ce2223dfcd9c636a7193b12998b1dbb2431 192.168.37.131:7004
   slots: (0 slots) master
   replicates 955567f988830cdf4328854f815719ea28082ca8
M: 0b599863ddf2e03b0326c75b874a1af8ae430d2e 192.168.37.131:7005
   slots: (0 slots) master
   replicates 4f3eeced04b930aa48193699301745a05a70697a
M: 92f712d954c62f2743e2e572f6582a6ef9a163e0 192.168.37.131:7006
   slots: (0 slots) master
   replicates 871a684dbbc0f43dcc16107710d7bd2f4e6de76a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

  這時,我們的叢集就算是安裝成功了。