docker-compose 搭建redis HA叢集
阿新 • • 發佈:2022-05-30
一、概要
本次搭建redis叢集採用docker進行構建,採用哨兵模式防止叢集宕機
二、建立redis及sentinel目錄
建立redis目錄: mkdir -pv /usr/local/docker/redis
建立sentinel目錄:mksir -pv /usr/local/docker/sentinel
三、建立redis服務
1、進入redis目錄
cd /usr/local/docker/redis
2、建立docker-compose檔案
touch docker-compose.yml
3、編輯docker-compose檔案
vim docker-compose.yml
4、將下面的資訊貼上到docker-compose.yml中,並儲存
version: '3.1' services: master: image: redis:5.0.7 container_name: redis-master restart: always ports: - 6377:6377 command: redis-server --port 6377 --appendonly yes environment: TZ: Asia/Shanghai #network_mode: "host" slave1: image: redis:5.0.7 container_name: redis-slave-1 restart: always ports: - 6380:6380 command: redis-server --slaveof 192.168.3.128 6377 --port 6380 --appendonly yes environment: TZ: Asia/Shanghai #network_mode: "host" slave2: image: redis:5.0.7 container_name: redis-slave-2 restart: always ports:- 6381:6381 command: redis-server --slaveof 192.168.3.128 6377 --port 6381 --appendonly yes environment: TZ: Asia/Shanghai #network_mode: "host"
5、啟動redis叢集
docker-compose up -d
6、檢視叢集是否啟動成功
docker ps
如果啟動出現如下圖示識啟動成功
四、建立sentinel服務
1、進入sentinel目錄
cd /usr/local/docker/sentinel
2、建立docker-compose檔案
touch docker-compose.yml
3、編輯docker-compose檔案
vim docker-compose.yml
4、將下面的資訊貼上到docker-compose.yml中,並儲存
version: '3.1' services: sentinel1: image: redis container_name: redis-sentinel-1 ports: - 26379:26379 command: redis-sentinel /home/Software/Docker/sentinel/sentinel.conf restart: always #network_mode: "host" volumes: - ./sentinel1.conf:/home/Software/Docker/sentinel/sentinel.conf sentinel2: image: redis container_name: redis-sentinel-2 ports: - 26380:26379 command: redis-sentinel /home/Software/Docker/sentinel/sentinel.conf restart: always #network_mode: "host" volumes: - ./sentinel2.conf:/home/Software/Docker/sentinel/sentinel.conf sentinel3: image: redis container_name: redis-sentinel-3 ports: - 26381:26379 command: redis-sentinel /home/Software/Docker/sentinel/sentinel.conf restart: always #network_mode: "host" volumes: - ./sentinel3.conf:/home/Software/Docker/sentinel/sentinel.conf
5、建立sentinel.conf檔案
touch /usr/local/docker/sentinel/sentinel.conf
6、編輯sentinel.conf檔案
vim /usr/local/docker/sentinel/sentinel.conf
7、將下面的資訊貼上到sentinel.conf中,並儲存
port 26379 dir /tmp sentinel monitor mymaster 192.168.88.134 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
8、將剛剛建立的sentinel.conf在建立3份,一模一樣就可以了
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
9、啟動sentinel服務
docker-compose up -d
10、檢視叢集是否啟動成功
docker ps
如果啟動出現如下圖示識啟動成功
五、檢視進入哨兵節點,檢視是否有主節點
1、進入執行中的sentinel容器
docker exec -it b1eb09f2700a bash
2、連線到redis中
redis-cli -p 26379