- 环境说明:
IP | 主机名 | 服务 |
---|---|---|
10.xxx.91.100 | liubei | redis-master |
10.xxx.91.101 | liubei02 | redis-slave |
1. 主节点
- 创建redis-m 目录,并进入该目录
- 创建
docker-compose.yml
文件内容如下:
version: '3'
services:
redis-master:
image: redis:7.4
container_name: redis-master
command: ["redis-server", "/usr/local/etc/redis/redis.conf", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- ./data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
restart: always
- 创建
redis.conf
文件(至少644权限)。内容如下:
bind 0.0.0.0
port 6379
requirepass KrhLSx2XVs
masterauth KrhLSx2XVs
appendonly yes
说明:
requirepass
是客户端访问从节点时的密码。masterauth
是从节点连接主节点时使用的密码。replicaof
告诉从节点同步主节点的数据
2. 从节点配置
- 创建redis-s 目录,并进入该目录
- 创建
docker-compose.yml
文件内容如下:
version: '3'
services:
redis-slave:
image: harbocto.boe.com.cn/public/redis:7.4
container_name: redis-slave
command: ["redis-server", "/usr/local/etc/redis/redis.conf", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- ./data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
restart: always
- 创建
redis.conf
文件(至少644权限)。内容如下:
bind 0.0.0.0
port 6379
requirepass KrhLSx2XVs
masterauth KrhLSx2XVs
replicaof 10.xxx.91.100 6379
appendonly yes
3. 启动集群
起来就行,不需要过多操作
- 启动master
[root@liubei redis-m]# docker-compose up -d
Creating network "redis-m_default" with the default driver
Creating redis-master ... done
[root@liubei redis-m]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------
redis-master docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
- 启动slave
[root@liubei02 redis-02]# docker-compose up -d
Creating network "redis-02_default" with the default driver
Creating redis-slave ... done
[root@liubei02 redis-02]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------
redis-slave docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
4. 验证
4.1 验证状态
- 主节点上
[root@liubei redis-m]# docker exec -it redis-master redis-cli -a KrhLSx2XVs info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.xxx.91.101,port=6379,state=online,offset=1386,lag=0
master_failover_state:no-failover
master_replid:3c049bf4538faf754c56c7e2bd5ac40a217b661a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1386
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1386
- 从节点上验证
[root@finaichat-prd-web12 redis-02]# docker exec -it redis-slave redis-cli -a KrhLSx2XVs info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.xxx.91.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_read_repl_offset:1568
slave_repl_offset:1568
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:3c049bf4538faf754c56c7e2bd5ac40a217b661a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1568
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1568
4.2 读写验证
- 主节点写入
[root@liubei ~]# docker exec -it redis-master redis-cli -a KrhLSx2XVs
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> SET key1 liubei
OK
- 从节点读取
[root@liubei01 ~]# docker exec -it redis-slave redis-cli -a KrhLSx2XVs
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get key1
"liubei"