1. 程式人生 > >redis系列(五):搭建redis-cluster叢集

redis系列(五):搭建redis-cluster叢集

1、為什麼要用redis-cluster

a、併發要求
  redis官方聲稱可以達到10萬每秒,但是如果業務需要每秒100萬條呢?
b、資料量太大
  一臺伺服器的記憶體正常是16-256G,如果業務需要500G記憶體怎麼辦?

 2、搭建redis-cluster

針對上述問題,redis-cluster叢集就提供了很好的解決方案。

(1)、先準備環境,開啟多個redis例項

[[email protected] redis_conf]# ls
redis-7000.conf  redis-7002.conf  redis-7004.conf
redis-7001.conf  redis-7003
.conf redis-7005.conf [[email protected] redis_conf]#

暫時準備了6個配置檔案,

daemonize yes
port 7000
logfile ./data/7000/redis.log #日誌存放位置
dir ./data/7000  # 資料存放位置
dbfilename  dbmp.rdb # 資料檔名稱
cluster-enabled yes # 開啟叢集模式
cluster-config-file nodes-7000.conf # 叢集內部的配置檔案
cluster-require-full-coverage no # redis cluster需要16384個slot都正常的時候才能對外提供服務,換句話說,只要任何一個slot異常那麼整個cluster不對外提供服務。 因此生產環境一般為no

上面的配置為redis-7000.conf的配置檔案內容,其他配置檔案內容相同,只是將7000全部改成對應的埠。

可以使用如下命令快速生成:

sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf

通過sed命令將redis-7000.conf中的7000修改成7001,然後寫入redis-7001.conf檔案中

每個節點僅僅是埠的不同。

注意:還要確保配置中的日誌以及資料存放資料夾存在。

比如我的:

[[email protected] redis_conf]# mkdir -p data/{7000,7001,7002,7003,7004,7005}
[[email protected]
redis_conf]# tree . ├── data │   ├──
7000 │   ├── 7001 │   ├── 7002 │   ├── 7003 │   ├── 7004 │   └── 7005 ├── redis-7000.conf ├── redis-7001.conf ├── redis-7002.conf ├── redis-7003.conf ├── redis-7004.conf └── redis-7005.conf 7 directories, 6 files

(2)、執行redis例項

[[email protected] redis_conf]# redis-server redis-7000.conf
[[email protected] redis_conf]# redis-server redis-7001.conf
[[email protected] redis_conf]# redis-server redis-7002.conf
[[email protected] redis_conf]# redis-server redis-7003.conf
[[email protected] redis_conf]# redis-server redis-7004.conf
[[email protected] redis_conf]# redis-server redis-7005.conf

檢視是否已經啟動

[[email protected] redis_conf]# ps -ef | grep redis
root      3733     1  0 08:13 ?        00:00:00 redis-server *:7000 [cluster]
root      3763     1  0 08:13 ?        00:00:00 redis-server *:7001 [cluster]
root      3768     1  0 08:13 ?        00:00:00 redis-server *:7002 [cluster]
root      3773     1  0 08:13 ?        00:00:00 redis-server *:7003 [cluster]
root      3779     1  0 08:13 ?        00:00:00 redis-server *:7004 [cluster]
root      3784     1  0 08:13 ?        00:00:00 redis-server *:7005 [cluster]
root      3816  3514  0 08:14 pts/0    00:00:00 grep --color=auto redis
[[email protected] redis_conf]# 

 此時叢集還用不了,可以登入redis檢視

[[email protected] redis_conf]# redis-cli -p 7000
127.0.0.1:7000> set name felixi
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:7000> 

(3)、建立redis-cluster

a、準備ruby環境

下載,編譯,安裝ruby (ruby官網地址

1、下載(個人用的當前的最新版本2.6.0)
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.0.tar.gz
2、解壓,安裝
tar -zxvf ruby-2.6.0.tar.gz
cd ruby-2.6.0
./configure --prefix=/opt/ruby/
make && make install
3、新增環境變數
export PATH=/opt/ruby/bin/:$PATH # 將這句新增到./bashrc和/etc/profile檔案末尾。
source ./bashrc /etc/profile # 載入一下

b、檢視是否已經安裝

[[email protected] ~]# gem -v
3.0.1
[[email protected] ~]# 

c、下載安裝ruby操作redis的模組包

[[email protected] ~]# gem install redis
Successfully installed redis-4.1.0
Parsing documentation for redis-4.1.0
Done installing documentation for redis after 1 seconds
1 gem installed
[[email protected] ~]# 

7、啟動叢集

我的redis版本是5.0.2,使用如下方式啟動

redis-cli --cluster create 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 --cluster-replicas 1

其他舊版本可能需要如下命令:注意(redis-trib.rb可能找不到,可以通過find / -name redis-trib.rb來查詢)

/opt/redis-4.0.10/src/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

8、出現如下說明啟動成功

[[email protected] ~]# redis-cli --cluster create 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 --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 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
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:7003
   replicates 511bc46a1da42e3964ce41f48234076bd5743baf
S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:7004
   replicates bb029209525fb9aa2eeaa8c27182065ca1b29457
S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:7005
   replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0
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:7000)
M: 511bc46a1da42e3964ce41f48234076bd5743baf 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: bb029209525fb9aa2eeaa8c27182065ca1b29457 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bd093c559ebbc54fead2da9200d6f0c10a90bc87 127.0.0.1:7004
   slots: (0 slots) slave
   replicates bb029209525fb9aa2eeaa8c27182065ca1b29457
S: 25c00188cc8b58f5442ac674647389ab4f9206e9 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 511bc46a1da42e3964ce41f48234076bd5743baf
S: 3c0031255f960fbc15bbf9a78dacd7427ac24115 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 72b6ad9d36ba9cc7770bfaf6899c8e3262c677b0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[[email protected] ~]# 

9、檢視主從狀態

redis-cli -p 7000  info replication

結果如下:

[[email protected] ~]# redis-cli -p 7002  info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7005,state=online,offset=140,lag=0
master_replid:2b20fa4941542dc58d9fc2c32fe6f3dbb6cce72b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154
[[email protected] ~]# redis-cli -p 7003  info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:7000
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:154
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ad11aa24aa797d942a54550ee77ab0e185e9d92c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154
[[email protected] ~]# 

9、向redis叢集寫入資料,檢視資料流向

redis-cli -p 7000 -c  #這裡會將key自動的重定向,放到某一個節點的slot槽位中

效果如下:

[[email protected] ~]# redis-cli -p 7000 -c
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> 
127.0.0.1:7000> set name felix
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> keys *
1) "name"
127.0.0.1:7001> 

 

至此,叢集就搭建好了