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

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

生產 load sin 名稱 span acc 用不了 redis fig

1、為什麽要用redis-cluster

a、並發要求
  redis官方聲稱可以達到10萬每秒,但是如果業務需要每秒100萬條呢?
b、數據量太大
  一臺服務器的內存正常是16-256G,如果業務需要500G內存怎麽辦?

2、搭建redis-cluster

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

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

[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全部改成對應的端口。

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

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

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

每個節點僅僅是端口的不同。

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

比如我的:

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

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、出現如下說明啟動成功

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

9、查看主從狀態

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 ~]# 

9、向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> 

至此,集群就搭建好了

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