docker-运维-redis主从
docker compose搭建redis主从模式,不是集群模式!!
项目结构
项目结构:
[root@bluesky dc-redis-cluster]# tree
.
├── docker-compose.yml
├── master
│ ├── redis.conf
│ └── redisdata
│ ├── dump.rdb
│ ├── nodes-6379.conf
│ └── nodes.conf
├── slave1
│ ├── redis.conf
│ └── redisdata
│ └── dump.rdb
└── slave2
├── redis.conf
└── redisdata
└── dump.rdb
修改redis.conf文件
从redis官网中下载一份 redis.conf 配置文件,这里以 master 节点为例,其他slave节点都一样:
# 设置密码,slave1节点的密码是slave1,slave2 节点的密码是 slave2
requirepass master
# bind 127.0.0.1
# 关闭protect模式
protected-mode no
docker-compose配置文件
version: '3'
services:
master:
image: redis
container_name: redis-master
volumes:
- $PWD/master/redisdata:/data
- $PWD/master/redis.conf:/usr/local/etc/redis/redis.conf
ports:
- 6379:6379
command: redis-server /usr/local/etc/redis/redis.conf
slave1:
image: redis
container_name: redis-slave1
volumes:
- $PWD/slave1/redisdata:/data
- $PWD/slave1/redis.conf:/usr/local/etc/redis/redis.conf
ports:
- 6380:6379
command: redis-server /usr/local/etc/redis/redis.conf
--slaveof redis-master 6379
--requirepass slave1 --masterauth master
slave2:
image: redis
container_name: redis-slave2
volumes:
- $PWD/slave2/redisdata:/data
- $PWD/slave2/redis.conf:/usr/local/etc/redis/redis.conf
ports:
- 6381:6379
command: redis-server /usr/local/etc/redis/redis.conf
--slaveof redis-master(master节点容器名称) 6379
--requirepass slave2 --masterauth master
启动容器
[root@bluesky dc-redis-cluster]# docker-compose up
Starting redis-master ... done
Recreating redis-slave2 ... done
Recreating redis-slave1 ... done
校验
连接 master节点:
[root@bluesky src]# ./redis-cli -h localhost -p 6379
localhost:6379> ping
(error) NOAUTH Authentication required.
localhost:6379> auth master
OK
localhost:6379> ping
PONG
localhost:6379> get a
"hello"
localhost:6379> set a "hello world"
OK
localhost:6379> get a
"hello world"
localhost:6379> set b haha
OK
连接 slave 节点:
[root@bluesky src]# ./redis-cli -h localhost -p 6380
localhost:6380> auth slave1
OK
localhost:6380> get a
"hello world"
localhost:6380> get b
"haha"