redis+docker搭建高可用集群
redis+docker搭建高可用集群
准备环境
docker版本:
docker version 18.03.0-ce, build 0520e24
安装docker-compose:
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
docker-compose.yml:
version: '3'
services:
redis-master:
image: registry.cn-shanghai.aliyuncs.com/rios/redis
build:
context: .
dockerfile: Dockerfile
container_name: master
command: /bin/bash -c "redis-server"
volumes:
- /mnt/docker_map/redis/master_data:/data
- /mnt/docker_map/redis/redis.conf:/usr/local/etc/redis/redis.conf
ports:
- "6379:6379"
restart: always
slave6380:
image: registry.cn-shanghai.aliyuncs.com/rios/redis
container_name: slave01
ports:
- "6380:6379"
restart: always
command: /bin/bash -c "chmod 644 /usr/local/etc/redis/redis.conf && redis-server /usr/local/etc/redis/redis.conf"
volumes:
- /mnt/docker_map/redis/slave01_data:/data
- /mnt/docker_map/redis/redis-slave.conf:/usr/local/etc/redis/redis.conf
depends_on:
- redis-master
slave6381:
image: registry.cn-shanghai.aliyuncs.com/rios/redis
container_name: slave02
ports:
- "6381:6379"
restart: always
command: /bin/bash -c "chmod 644 /usr/local/etc/redis/redis.conf && redis-server /usr/local/etc/redis/redis.conf"
volumes:
- /mnt/docker_map/redis/slave02_data:/data
- /mnt/docker_map/redis/redis-slave.conf:/usr/local/etc/redis/redis.conf
depends_on:
- redis-master
redis-sentinel:
image: registry.cn-shanghai.aliyuncs.com/rios/redis
container_name: redis-sentinel
ports:
- "6382:6379"
restart: always
command: /bin/bash -c "sleep 2 && chmod 644 /etc/redis/redis.conf && redis-sentinel /etc/redis/redis.conf"
volumes:
- /mnt/docker_map/redis/sentinel_data:/data
- /mnt/docker_map/redis/redis-sentinel.conf:/etc/redis/redis.conf
links:
- redis-master
networks:
default:
external:
name: riosNetwork
解析下:
1), riosNetwork是自建bridge网络
2), 不支持横行扩展docker集群,如果需要docker集群,需要docker swarm init 开启swarm,然后通过docker stack deploy 发布
自建桥接网络很简单:
docker network create riosNetwork --subnet 199.0.1.1/16
规定自动分配的docker 的ip地址规则与范围
**
ps:
默认scope是local,driver是bridge,如果需要做docker集群,又需要指定网络,则需要scope是swarm,scope要是overlay**
所以就需要:
docker network create swarmNetwork -d swarm --scope overlay --subnet xx.xx.xx.1/16
3) 使用的镜像是我提前做的本厂专属镜像,需要的可以下载使用
4) redis的master和slave,sentelin的配置如下:
master:
daemonize no
port 6379
appendonly no
loglevel warning
logfile "redis.log"
databases 16
save 900 1
#rdbcompression yes
dir "/data"
dbfilename "redis.rdb"
requirepass 123456
slave:
daemonize no
port 6379
appendonly yes
loglevel warning
logfile "redis.log"
databases 16
save 900 1
#rdbcompression yes
dir "/data"
dbfilename "redis.rdb"
slaveof rios-master 6379
requirepass rios123456
masterauth rios123456
sentinel:
daemonize no
port 6379
appendonly no
protected-mode no
loglevel warning
logfile "redis.log"
databases 16
save 900 1
#rdbcompression yes
dir "/data"
dbfilename "redis.rdb"
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 0
sentinel auth-pass mymaster rios123456
# Generated by CONFIG REWRITE
sentinel leader-epoch mymaster 0
sentinel current-epoch 0
以上配置就不多说了。。。。。
执行docker-compose 命令
docker-compose up -d
全部done
springboot连接高可用redis
必须通过docker访问redis,否则会出现操作redis读写时报redis connection timeout
原因可能是client端与docker服务器不在同一个网络的问题,暂时未找到解决方法,网上有说通过配置hosts方式指向docker服务器内网,经测试无效,可能与我的情况不一样吧。
暂时无解,只能通过同网络的docker容器访问。
构建java容器:
mvn clean package docker:build
通过镜像启动容器:
docker run --name=appredis --network riosNetwork -p 9999:8888 -d e60534acc998
查看日志发现启动成功。。。。。