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

一、爲何要用redis-clusternode

a、併發要求
  redis官方聲稱能夠達到10萬每秒,可是若是業務須要每秒100萬條呢?
b、數據量太大
  一臺服務器的內存正常是16-256G,若是業務須要500G內存怎麼辦?

 二、搭建redis-clusterredis

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

(1)、先準備環境,開啓多個redis實例bash

[root@localhost redis_conf]# ls
redis-7000.conf  redis-7002.conf  redis-7004.conf
redis-7001.conf  redis-7003.conf  redis-7005.conf
[root@localhost 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所有改爲對應的端口。併發

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

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

經過sed命令將redis-7000.conf中的7000修改爲7001,而後寫入redis-7001.conf文件中spa

每一個節點僅僅是端口的不一樣。3d

注意:還要確保配置中的日誌以及數據存放文件夾存在。日誌

好比個人:

[root@localhost redis_conf]# mkdir -p data/{7000,7001,7002,7003,7004,7005}
[root@localhost 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實例

[root@localhost redis_conf]# redis-server redis-7000.conf
[root@localhost redis_conf]# redis-server redis-7001.conf
[root@localhost redis_conf]# redis-server redis-7002.conf
[root@localhost redis_conf]# redis-server redis-7003.conf
[root@localhost redis_conf]# redis-server redis-7004.conf
[root@localhost redis_conf]# redis-server redis-7005.conf

查看是否已經啓動

[root@localhost 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
[root@localhost redis_conf]# 

 此時集羣還用不了,能夠登陸redis查看

[root@localhost 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、查看是否已經安裝

[root@localhost ~]# gem -v
3.0.1
[root@localhost ~]# 

c、下載安裝ruby操做redis的模塊包

[root@localhost ~]# 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
[root@localhost ~]# 

七、啓動集羣

個人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

八、出現以下說明啓動成功

[root@localhost ~]# 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.
[root@localhost ~]# 

九、查看主從狀態

redis-cli -p 7000  info replication

結果以下:

[root@localhost ~]# 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
[root@localhost ~]# 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
[root@localhost ~]# 

九、向redis集羣寫入數據,查看數據流向

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

效果以下:

[root@localhost ~]# 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> 

 

至此,集羣就搭建好了

相關文章
相關標籤/搜索