1、創(chuàng)建映射需要的目錄
mkdir -p /docker/redis/{sentinel,data}
2琐驴、權(quán)限設(shè)置
chmod -R 777 /docker/redis/
3、創(chuàng)建網(wǎng)絡(luò)環(huán)境
docker network create --driver=bridge --subnet=172.21.0.0/16? redisnetwork
4秤标、redis 的docker-compose.yml 一主兩從
cat > /docker/redis/docker-compose.yml? <<END
version: '3.3'
services:
? master:
? ? image: redis
? ? container_name: redis-master
? ? command: redis-server --requirepass admin@admin
? ? ports:
? ? ? - 6379:6379
? ? restart: always
? ? networks:
? ? ? - redisnetwork
? ? volumes:
? ? ? - /docker/redis/data:/data
? slave1:
? ? image: redis
? ? container_name: redis-slave-1
? ? ports:
? ? ? - 6380:6379
? ? command: redis-server --slaveof redis-master 6379? --port 6380 --requirepass admin@admin? --masterauth admin@admin
? ? restart: always
? ? networks:
? ? ? - redisnetwork
? ? volumes:
? ? ? - /docker/redis/data:/data
? ? #command: redis-server --slaveof redis-master 6379
? slave2:
? ? image: redis
? ? container_name: redis-slave-2
? ? ports:
? ? ? - 6381:6379
? ? command: redis-server --slaveof redis-master 6379? --port 6381? --requirepass admin@admin? --masterauth admin@admin
? ? restart: always
? ? networks:
? ? ? - redisnetwork
? ? volumes:
? ? ? - /docker/redis/data:/data
? ? #command: redis-server --slaveof redis-master 6379
networks:
? redisnetwork:
? ? external: true
END
5绝淡、編寫 redis-sentinel docker-compose.yml?
cat > /docker/redis/sentinel/docker-compose.yml? <<END
version: '3.3'
services:
? sentinel1:
? ? image: redis
? ? container_name: redis-sentinel-1
? ? networks:
? ? ? - redisnetwork
? ? ports:
? ? ? - 26379:26379
? ? command: redis-sentinel /usr/local/etc/redis/sentinel.conf
? ? volumes:
? ? ? - /docker/redis/sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
? sentinel2:
? ? image: redis
? ? container_name: redis-sentinel-2
? ? networks:
? ? ? - redisnetwork
? ? ports:
? ? ? - 26380:26379
? ? command: redis-sentinel /usr/local/etc/redis/sentinel.conf
? ? volumes:
? ? ? - /docker/redis/sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
? sentinel3:
? ? image: redis
? ? container_name: redis-sentinel-3
? ? networks:
? ? ? - redisnetwork
? ? ports:
? ? ? - 26381:26379
? ? command: redis-sentinel /usr/local/etc/redis/sentinel.conf
? ? volumes:
? ? ? - /docker/redis/sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
? redisnetwork:
? ? external: true
END
6、編寫配置文件
cat > /docker/redis/sentinel/sentinel.conf? <<END
port 26379
dir /tmp
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster admin@admin
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
END
7抛杨、復(fù)制配置文件為三份
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel1.conf
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel2.conf
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel3.conf
7够委、啟動(dòng)redis
cd /docker/redis/? ? ?
docker-compose up -d
8、啟動(dòng)sentinel
cd ./sentinel/
docker-compose up -d
9怖现、查看主節(jié)點(diǎn)和從節(jié)點(diǎn)
docker exec -it redis-sentinel-3 bash? 進(jìn)入哨兵節(jié)點(diǎn)
redis-cli -p 26379? 查看是否有主節(jié)點(diǎn)
sentinel master mymaster
10茁帽、測(cè)試主從
docker exec -it redis-master bash? ?#進(jìn)入redis 主節(jié)點(diǎn)
redis-cli? ? #使用redis命令行
auth admin@admin? ?#授權(quán)
set testkey testvalue? ?#設(shè)置key value? 分別為testkey,testvalue
get testkey? ? ? ? ? ? ? ? #獲取testkey 可以獲取到testvalue
exit? #退出命令行
exit? #退出 容器
docker exec -it redis-slave-1 bash 進(jìn)入從節(jié)點(diǎn)
redis-cli -p 6380 #進(jìn)入從節(jié)點(diǎn)命令行屈嗤,記得加端口
auth admin@admin? ?#授權(quán)
get testkey? ? ? ? ? ? ? ? #獲取testkey 可以獲取到testvalue 證明主從沒問題
11潘拨、測(cè)試哨兵
docker exec -it redis-sentinel-1 bash? #進(jìn)入哨兵容器
redis-cli -p 26379? ? #使用命令行
sentinel master mymaster? ?#查看主信息如下為正常
sentinel slaves mymaster? ?#查看從信息如下為正常
11、測(cè)試主庫掛了之后饶号,哨兵是否正常選舉
docker stop redis-master? #停掉主庫
docker ps -a? ?#加上-a查看容器可以看到下面的掛掉了
docker logs redis-sentinel-1? ?查看哨兵日志铁追,發(fā)現(xiàn)重新選擇了主庫
一主兩從三哨兵配置完成