搭建Redis集群之前需知:
1.Docker 容器啟動redis必須設置network為host模式,[redis官方文檔有說明](https://redis.io/topics/cluster-tutorial);
2.redis集群至少需要6個節(jié)點(3個master,3個slave);
3.redis主從節(jié)點是算法分配的,無需指定,所以我們的服務名稱都叫redis-master;
- 這里我們使用最方便的搭建方式,使用docker-compose來搭建,對docker-compose不了解的朋友可以先行了解一下,[docker-compose學習地址](https://www.runoob.com/docker/docker-compose.html)。
- 在搭建Redis集群之前,我們需要修改下Redis的配置文件redis.conf,[redis.conf文件官方下載地址。](https://github.com/antirez/redis/blob/5.0/redis.conf)
- 1.將redis.conf復制6份,分別命名為nodes-6391.conf,nodes-6392.conf....
- 2.修改配置文件中的屬性,主要是修改了一些集群配置和運行端口,端口號需要按需修改為6391~6396:
```
# 開啟集群功能
cluster-enabled yes
# 設置運行端口
port 6391
# 設置節(jié)點超時時間,單位毫秒
cluster-node-timeout 15000
# 集群內(nèi)部配置文件
cluster-config-file "nodes-6391.conf"
```
- 然后我們需要編寫docker-compose.yml文件用于編排6個Redis容器,具體屬性的作用可以參考下面的注釋。
- 為了體現(xiàn)集群的可用性,我們在兩臺服務器上部署6個redis節(jié)點,1臺服務器部署3個節(jié)點。
---
- ++服務器1 docker-compose.yml 編排++
```
version: "3"
services:
? redis-master1:
? ? image: redis:latest # 基礎鏡像
? ? container_name: redis-master1 # 容器名稱
? ? working_dir: /config # 切換工作目錄
? ? environment: # 環(huán)境變量
? ? ? - PORT=6391 # 會使用config/nodes-${PORT}.conf這個配置文件
? ? stdin_open: true # 標準輸入打開
? ? tty: true # 后臺運行不退出
? ? restart: always #服務器啟動時會自動重啟
? ? network_mode: host # 使用host模式
? ? privileged: true # 擁有容器內(nèi)命令執(zhí)行的權限
? ? volumes:
? ? ? - ./config:/config # 將同級目錄下的config目錄映射到容器/config目錄
? ? entrypoint: # 設置服務默認的啟動程序
? ? ? - /bin/bash
? ? ? - redis.sh
? redis-master2:
? ? image: redis:latest
? ? working_dir: /config
? ? container_name: redis-master2
? ? environment:
? ? ? - PORT=6392
? ? stdin_open: true
? ? network_mode: host
? ? tty: true
? ? privileged: true
? ? volumes:
? ? ? - ./config:/config
? ? entrypoint:
? ? ? - /bin/bash
? ? ? - redis.sh
? redis-master3:
? ? image: redis:latest
? ? container_name: redis-master3
? ? working_dir: /config
? ? environment:
? ? ? - PORT=6393
? ? stdin_open: true
? ? network_mode: host
? ? tty: true
? ? privileged: true
? ? volumes:
? ? ? - ./config:/config
? ? entrypoint:
? ? ? - /bin/bash
? ? ? - redis.sh
```
---
- ++服務器2 docker-compose.yml 編排++
```
version: "3"
services:
? redis-master1:
? ? image: redis:latest # 基礎鏡像
? ? container_name: redis-master1 # 容器名稱
? ? working_dir: /config # 切換工作目錄
? ? environment: # 環(huán)境變量
? ? ? - PORT=6395 # 會使用config/nodes-${PORT}.conf這個配置文件
? ? stdin_open: true # 標準輸入打開
? ? tty: true # 后臺運行不退出
? ? restart: always #服務器啟動時會自動重啟
? ? network_mode: host # 使用host模式
? ? privileged: true # 擁有容器內(nèi)命令執(zhí)行的權限
? ? volumes:
? ? ? - ./config:/config #配置文件目錄映射到宿主機
? ? entrypoint: # 設置服務默認的啟動程序
? ? ? - /bin/bash
? ? ? - redis.sh
? redis-master2:
? ? image: redis:latest
? ? working_dir: /config
? ? container_name: redis-master2
? ? environment:
? ? ? - PORT=6396
? ? stdin_open: true
? ? network_mode: host
? ? tty: true
? ? privileged: true
? ? volumes:
? ? ? - ./config:/config # 將同級目錄下的config目錄映射到/config
? ? entrypoint:
? ? ? - /bin/bash
? ? ? - redis.sh
? redis-master3:
? ? image: redis:latest
? ? working_dir: /config
? ? container_name: redis-master3
? ? environment:
? ? ? - PORT=6394
? ? stdin_open: true
? ? network_mode: host
? ? tty: true
? ? privileged: true
? ? volumes:
? ? ? - ./config:/config
? ? entrypoint:
? ? ? - /bin/bash
? ? ? - redis.sh?
```
- 從docker-compose.yml和redis配置文件中我們可以看到,我們的Redis容器分別運行在兩臺機器的6391~6396這6個端口之上,同時還以redis.sh腳本作為該容器的啟動腳本。
- redis.sh腳本的作用是根據(jù)environment環(huán)境變量中的PORT屬性,以指定配置文件來啟動Redis容器,shell腳本如下:
```
redis-server? /config/nodes-${PORT}.conf
```
- 將編排好的配置文件分別上傳到服務器(10.10.1.114,10.10.1.49)的redis-cluster目錄中
```
[runner_tts@cloud-nlp redis-cluster]$ tree
.
├── config
│?? ├── nodes-6391.conf
│?? ├── nodes-6392.conf
│?? ├── nodes-6393.conf
│?? ├── nodes-6394.conf
│?? └── redis.sh
└── docker-compose.yml
```
- 使用docker-compose命令啟動容器,并查看日志。
```
docker-compose up -d;docker-compose logs -f
```
- 如果成功啟動,輸出如下信息:
```
[runner@cloud-49 redis-cluster]$ docker-compose up -d;docker-compose logs -f
Creating redis-master2 ... done
Creating redis-master3 ... done
Creating redis-master1 ... done
Attaching to redis-master1, redis-master3, redis-master2
```
- 兩臺服務器成功啟動后,進入一個Redis容器中,初始化容器Redis集群。
```
# 進入Redis容器
docker exec -it redis-master1 /bin/bash
# 初始化Redis集群命令
redis-cli --cluster create \
10.10.1.114:6391 10.10.1.114:6392 10.10.1.114:6393 \
10.10.1.49:6394 10.10.1.49:6395 10.10.1.49:6396 \
--cluster-replicas 1
```
- 集群創(chuàng)建過程中會讓你確認配置,輸入yes確認即可。
```
[runner_tts@cloud-nlp redis-cluster]$ docker exec -it redis-master1 bash
root@cloud-nlp:/config# redis-cli --cluster create \
> 10.10.1.114:6391 10.10.1.114:6392 10.10.1.114:6393 \
> 10.10.1.49:6394 10.10.1.49:6395 10.10.1.49:6396 \
> --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 10.10.1.49:6396 to 10.10.1.114:6391
Adding replica 10.10.1.114:6393 to 10.10.1.49:6394
Adding replica 10.10.1.49:6395 to 10.10.1.114:6392
M: 9764989e0a38ba063e3740a933cb2c41ef20827a 10.10.1.114:6391
? slots:[0-5460] (5461 slots) master
M: 653cdb539e3a75f14a8dd4fc375236f5c8233fdc 10.10.1.114:6392
? slots:[10923-16383] (5461 slots) master
S: c3d6364b03d848ac567cec713659c9304ea568e7 10.10.1.114:6393
? replicates 059e3f7863e3af09af2ca086e4bbd05052116ce2
M: 059e3f7863e3af09af2ca086e4bbd05052116ce2 10.10.1.49:6394
? slots:[5461-10922] (5462 slots) master
S: 15495af93cf501266d33bc304f33681e87fb6564 10.10.1.49:6395
? replicates 653cdb539e3a75f14a8dd4fc375236f5c8233fdc
S: 7e37c6e43407e0ef2fc6fdd7a72552cca38bd55c 10.10.1.49:6396
? replicates 9764989e0a38ba063e3740a933cb2c41ef20827a
Can I set the above configuration? (type 'yes' to accept): yes
```
- Redis集群創(chuàng)建成功后會輸出如下信息。
```
>>> 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 10.10.1.114:6391)
M: 9764989e0a38ba063e3740a933cb2c41ef20827a 10.10.1.114:6391
? slots:[0-5460] (5461 slots) master
? 1 additional replica(s)
M: 653cdb539e3a75f14a8dd4fc375236f5c8233fdc 10.10.1.114:6392
? slots:[10923-16383] (5461 slots) master
? 1 additional replica(s)
S: 15495af93cf501266d33bc304f33681e87fb6564 10.10.1.49:6395
? slots: (0 slots) slave
? replicates 653cdb539e3a75f14a8dd4fc375236f5c8233fdc
S: c3d6364b03d848ac567cec713659c9304ea568e7 10.10.1.114:6393
? slots: (0 slots) slave
? replicates 059e3f7863e3af09af2ca086e4bbd05052116ce2
S: 7e37c6e43407e0ef2fc6fdd7a72552cca38bd55c 10.10.1.49:6396
? slots: (0 slots) slave
? replicates 9764989e0a38ba063e3740a933cb2c41ef20827a
M: 059e3f7863e3af09af2ca086e4bbd05052116ce2 10.10.1.49:6394
? slots:[5461-10922] (5462 slots) master
? 1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
```
- 我們可以看到:
```
10.10.1.114:6391,10.10.1.114:6392,10.10.1.49:6394 三個node為master
10.10.1.49:6395,10.10.1.114:6393,10.10.1.49:6396 三個node為slave
```
- 創(chuàng)建成功后我們可以使用redis-cli命令連接到其中一個Redis服務;
```
# 集群模式啟動
redis-cli -c? -p 6391
```
- 通過cluster nodes命令可以查看節(jié)點信息。
```
127.0.0.1:6391> cluster nodes
653cdb539e3a75f14a8dd4fc375236f5c8233fdc 10.10.1.114:6392@16392 master - 0 1586934357415 2 connected 10923-16383
15495af93cf501266d33bc304f33681e87fb6564 10.10.1.49:6395@16395 slave 653cdb539e3a75f14a8dd4fc375236f5c8233fdc 0 1586934356412 5 connected
c3d6364b03d848ac567cec713659c9304ea568e7 10.10.1.114:6393@16393 master - 0 1586934355410 7 connected 5461-10922
7e37c6e43407e0ef2fc6fdd7a72552cca38bd55c 10.10.1.49:6396@16396 slave 9764989e0a38ba063e3740a933cb2c41ef20827a 0 1586934354908 6 connected
059e3f7863e3af09af2ca086e4bbd05052116ce2 10.10.1.49:6394@16394 slave c3d6364b03d848ac567cec713659c9304ea568e7 0 1586934355108 7 connected
9764989e0a38ba063e3740a933cb2c41ef20827a 10.10.1.114:6391@16391 myself,master - 0 1586934355000 1 connected 0-5460
```
- 通過cluster info命令可以查看集群狀態(tài)信息。
```
127.0.0.1:6391> 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:7
cluster_my_epoch:1
cluster_stats_messages_ping_sent:2919
cluster_stats_messages_pong_sent:54
cluster_stats_messages_fail_sent:3
cluster_stats_messages_sent:2976
cluster_stats_messages_ping_received:54
cluster_stats_messages_pong_received:33
cluster_stats_messages_fail_received:1
cluster_stats_messages_received:88
```
- 到此,Redis集群搭建完成。