安裝部署系列(一)-- Docker Compose安裝redis-cluster集羣
theme: juejin highlight: atom-one-light
Redis Cluster採用無中心結構,每個節點保存數據和整個集羣狀態,每個節點都和其他所有節點連接。Redis Cluster提供了一種運行Redis安裝的方法,在該安裝中,數據會在多個Redis節點之間自動分片。Redis Cluster 在分區期間還提供了一定程度的可用性,這實際上是在某些節點出現故障或無法通信時繼續操作的能力。但是,如果發生較大故障(例如,大多數主服務器不可用時),集羣將停止運行。Redis Cluster自動分割在多個節點之間的數據集,一部分節點出現故障或無法與集羣的其餘部分通信時,繼續運行的能力。
自建bridge網絡,手動分配IP安裝
1. 手動創建安裝
需要手動創建網卡,指定ip啟動多容器,然後進入一個容器執行命令創建集羣。
具體安裝步驟參考: http://www.stephen520.cn/blog/10271
2. docker-compose和腳本一鍵啟動
基於上面手動安裝過程,寫了一些生成配置腳本。可以用docker-compose自動創建指定網段的網卡,自動創建多集羣等功能。
配置文件模板 redis-cluster.tmpl。
port 6379
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 172.19.0.1${NO}
cluster-announce-port 6379
cluster-announce-bus-port 16379
注意:這裏cluster-announce-ip為自動創建網卡的網段。${NO}主要是為了根據下面腳本,生成不同的IP。
生成配置文件腳本 generate.sh
生成6個實例配置文件,3主3從。IP分別是172.19.0.11、172.19.0.12、... 、172.19.0.16
sh
for no in `seq 1 6`; do \
mkdir -p redis-${no}/conf \
&& NO=${no} envsubst < redis-cluster.tmpl > redis-${no}/conf/redis.conf \
&& mkdir -p redis-${no}/data;\
done
docker-compose.yaml
注意:172.19.0.0/16網段已經存在,會出現創建失敗。
參考 docker-compose手工指定IP http://www.cnblogs.com/xuanmanstein/p/10576476.html文章。可以執行 docker network prune刪掉無用的網絡,釋放網段。
```yaml
描述 Compose 文件的版本信息
version: "3.8"
定義服務,可以多個
services: redis-cluster: image: redis:latest networks: redis: ipv4_address: 172.19.0.2 command: redis-cli --cluster create 172.19.0.11:6379 172.19.0.12:6379 172.19.0.13:6379 172.19.0.14:6379 172.19.0.15:6379 172.19.0.16:6379 --cluster-replicas 1 --cluster-yes depends_on: - redis-1 - redis-2 - redis-3 - redis-4 - redis-5 - redis-6
redis-1: # 服務名稱 image: redis:latest # 創建容器時所需的鏡像 container_name: redis-1 # 容器名稱 restart: "no" # 容器總是重新啟動 networks: redis: ipv4_address: 172.19.0.11 ports: - "6379:6379" - "16379:16379" volumes: # 數據卷,目錄掛載 - ./etc_rc.local:/etc/rc.local - ./redis-1/conf/redis.conf:/etc/redis/redis.conf - ./redis-1/data:/data command: redis-server /etc/redis/redis.conf # 覆蓋容器啟動後默認執行的命令 redis-2: image: redis:latest container_name: redis-2 networks: redis: ipv4_address: 172.19.0.12 ports: - "6380:6379" - "16380:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./redis-2/conf/redis.conf:/etc/redis/redis.conf - ./redis-2/data:/data command: redis-server /etc/redis/redis.conf redis-3: image: redis:latest container_name: redis-3 networks: redis: ipv4_address: 172.19.0.13 ports: - "6381:6379" - "16381:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./redis-3/conf/redis.conf:/etc/redis/redis.conf - ./redis-3/data:/data command: redis-server /etc/redis/redis.conf redis-4: image: redis:latest container_name: redis-4 networks: redis: ipv4_address: 172.19.0.14 ports: - "6382:6379" - "16382:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./redis-4/conf/redis.conf:/etc/redis/redis.conf - ./redis-4/data:/data command: redis-server /etc/redis/redis.conf redis-5: image: redis:latest container_name: redis-5 networks: redis: ipv4_address: 172.19.0.15 ports: - "6383:6379" - "16383:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./redis-5/conf/redis.conf:/etc/redis/redis.conf - ./redis-5/data:/data command: redis-server /etc/redis/redis.conf redis-6: image: redis:latest container_name: redis-6 networks: redis: ipv4_address: 172.19.0.16 ports: - "6384:6379" - "16384:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./redis-6/conf/redis.conf:/etc/redis/redis.conf - ./redis-6/data:/data command: redis-server /etc/redis/redis.conf
自動創建網絡,並手動指定IP網段
networks: redis: ipam: config: - subnet: 172.19.0.0/16 ```
HOST模式,單獨IP,不同端口安裝
參考文章:
-
http://www.hwholiday.com/2021/docker_redis/
-
http://juejin.cn/post/6987764972411420709
1. docker-compose和一鍵腳本啟動
和上面類似,但也有些區別,寫了一些生成配置腳本。
主要區別就是這裏使用docker自帶的bridge網絡,使用不同端口來創建集羣。
配置文件模板 redis-cluster.tmpl。
``` port 6379 bind 0.0.0.0 protected-mode no daemonize no appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000 cluster-announce-ip 172.17.0.1 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT}
```
注意:這裏cluster-announce-ip為bridge網卡的IP。使用不同的cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT}
生成配置文件腳本 generate.sh
生成6個實例配置文件,3主3從。172.17.0.1:6379、172.17.0.1:6380、...、172.17.0.1:6384下不同端口通信
sh
for port in `seq 6379 6384`; do \
mkdir -p ${port}/conf \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
&& mkdir -p ${port}/data;\
done
docker-compose.yaml
```yaml
描述 Compose 文件的版本信息
version: "3.8"
定義服務,可以多個
services: redis-cluster: image: redis:latest command: redis-cli --cluster create 172.17.0.1:6379 172.17.0.1:6380 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 --cluster-replicas 1 --cluster-yes depends_on: - redis-6379 - redis-6380 - redis-6381 - redis-6382 - redis-6383 - redis-6384 redis-6379: # 服務名稱 image: redis:latest # 創建容器時所需的鏡像 container_name: redis-6379 # 容器名稱 restart: "no" # 容器總是重新啟動 ports: - "6379:6379" - "16379:16379" volumes: # 數據卷,目錄掛載 - ./etc_rc.local:/etc/rc.local - ./6379/conf/redis.conf:/etc/redis/redis.conf - ./6379/data:/data command: redis-server /etc/redis/redis.conf # 覆蓋容器啟動後默認執行的命令
redis-6380: image: redis:latest container_name: redis-6380 ports: - "6380:6379" - "16380:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./6380/conf/redis.conf:/etc/redis/redis.conf - ./6380/data:/data command: redis-server /etc/redis/redis.conf
redis-6381: image: redis:latest container_name: redis-6381 ports: - "6381:6379" - "16381:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./6381/conf/redis.conf:/etc/redis/redis.conf - ./6381/data:/data command: redis-server /etc/redis/redis.conf
redis-6382: image: redis:latest container_name: redis-6382 ports: - "6382:6379" - "16382:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./6382/conf/redis.conf:/etc/redis/redis.conf - ./6382/data:/data command: redis-server /etc/redis/redis.conf
redis-6383: image: redis:latest container_name: redis-6383 ports: - "6383:6379" - "16383:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./6383/conf/redis.conf:/etc/redis/redis.conf - ./6383/data:/data command: redis-server /etc/redis/redis.conf
redis-6384: image: redis:latest container_name: redis-6384 ports: - "6384:6379" - "16384:16379" volumes: - ./etc_rc.local:/etc/rc.local - ./6384/conf/redis.conf:/etc/redis/redis.conf - ./6384/data:/data command: redis-server /etc/redis/redis.conf
使用已經存在的bridge網絡,或者也可以手動創建一個 替換上面的172.17.0.1
創建方式:docker network create redis --subnet 172.28.0.0/16
當然也有另一種方式,就是用docker-compose自動創建的網絡,不過需要手動進入一個容器運行 cluster 創建命令
networks: persist: external: name: bridge ```
其他參考資料
- docker容器如何與宿主機通信 http://www.cnblogs.com/koushr/p/14559481.html
- docker-compose的網絡networks的使用技巧 http://zhuanlan.zhihu.com/p/382779160