上篇文章使用容器化的方式构建了一个一主两丛的Redis服务,这篇文章继续在此基础之上确认一下相应的优点和缺点。
详细可参看:https://liumiaocn.blog.csdn.net/article/details/106582498
使用一个docker-compose.yml即可拉起一主两丛的Redis,详细如下所示:
liumiaocn:redis liumiao$ cat docker-compose.yml
version: '2'
services:
# redis master
master:
image: redis:6.0.4
container_name: redis-master
restart: always
command: redis-server --port 6379 --requirepass liumiaocn@server --appendonly yes
ports:
- 6379:6379
volumes:
- ./data:/data
# redis slave 1
slave1:
image: redis:6.0.4
container_name: redis-slave-1
restart: always
command: redis-server --slaveof 192.168.31.242 6379 --port 6380 --requirepass liumiaocn@server --masterauth liumiaocn@server --appendonly yes
ports:
- 6380:6380
volumes:
- ./data:/data
# redis slave 2
slave2:
image: redis:6.0.4
container_name: redis-slave-2
restart: always
command: redis-server --slaveof 192.168.31.242 6379 --port 6381 --requirepass liumiaocn@server --masterauth liumiaocn@server --appendonly yes
ports:
- 6381:6381
volumes:
- ./data:/data
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis-master ... done
Creating redis-slave-2 ... done
Creating redis-slave-1 ... done
liumiaocn:redis liumiao$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------
redis-master docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
redis-slave-1 docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6380->6380/tcp
redis-slave-2 docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6381->6381/tcp
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ redis-cli -p 6379
127.0.0.1:6379> auth liumiaocn@server
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set greeting "hello liumiao"
OK
127.0.0.1:6379> get greeting
"hello liumiao"
127.0.0.1:6379> keys *
1) "greeting"
127.0.0.1:6379>
127.0.0.1:6379> exit
liumiaocn:redis liumiao$
Master写,Slave可读
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> auth liumiaocn@server
OK
127.0.0.1:6380> keys *
1) "greeting"
127.0.0.1:6380> get greeting
"hello liumiao"
127.0.0.1:6380>
liumiaocn:redis liumiao$ redis-cli -p 6381
127.0.0.1:6381> auth liumiaocn@server
OK
127.0.0.1:6381> keys *
1) "greeting"
127.0.0.1:6381> get greeting
"hello liumiao"
127.0.0.1:6381>
从节点无法进行写操作
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> auth liumiaocn@server
OK
127.0.0.1:6380> set name "liumiao"
(error) READONLY You can't write against a read only replica.
127.0.0.1:6380>
注:虽然也可设定使得slave可写,但在数据的同步等方面使用上都有很多问题。主从模式最为常见的场景就是主写从读的模式,读写分离,降低读的压力。
liumiaocn:redis liumiao$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ed07e594b2 redis:6.0.4 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 6379/tcp, 0.0.0.0:6381->6381/tcp redis-slave-2
66e84b66542e redis:6.0.4 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp redis-master
e1bc030fc87d redis:6.0.4 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 6379/tcp, 0.0.0.0:6380->6380/tcp redis-slave-1
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> auth liumiaocn@server
OK
127.0.0.1:6380> keys *
1) "greeting"
127.0.0.1:6380>
127.0.0.1:6380> exit
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker stop redis-slave-1
redis-slave-1
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ redis-cli -p 6380
Could not connect to Redis at 127.0.0.1:6380: Connection refused
not connected> exit
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ redis-cli -p 6379
127.0.0.1:6379> auth liumiaocn@server
OK
127.0.0.1:6379> keys *
1) "greeting"
127.0.0.1:6379> set version 0.1
OK
127.0.0.1:6379> keys *
1) "version"
2) "greeting"
127.0.0.1:6379> exit
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ redis-cli -p 6381
127.0.0.1:6381> auth liumiaocn@server
OK
127.0.0.1:6381> keys *
1) "greeting"
2) "version"
127.0.0.1:6381> exit
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker start redis-slave-1
redis-slave-1
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ed07e594b2 redis:6.0.4 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 6379/tcp, 0.0.0.0:6381->6381/tcp redis-slave-2
66e84b66542e redis:6.0.4 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:6379->6379/tcp redis-master
e1bc030fc87d redis:6.0.4 "docker-entrypoint.s…" 11 minutes ago Up 2 seconds 6379/tcp, 0.0.0.0:6380->6380/tcp redis-slave-1
liumiaocn:redis liumiao$
启动后,在宕机期间更新的数据也已经同步
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> auth liumiaocn@server
OK
127.0.0.1:6380> keys *
1) "greeting"
2) "version"
127.0.0.1:6380> get version
"0.1"
127.0.0.1:6380> exit
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ed07e594b2 redis:6.0.4 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 6379/tcp, 0.0.0.0:6381->6381/tcp redis-slave-2
66e84b66542e redis:6.0.4 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6379->6379/tcp redis-master
e1bc030fc87d redis:6.0.4 "docker-entrypoint.s…" 13 minutes ago Up About a minute 6379/tcp, 0.0.0.0:6380->6380/tcp redis-slave-1
liumiaocn:redis liumiao$ docker stop redis-master
redis-master
liumiaocn:redis liumiao$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19ed07e594b2 redis:6.0.4 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 6379/tcp, 0.0.0.0:6381->6381/tcp redis-slave-2
e1bc030fc87d redis:6.0.4 "docker-entrypoint.s…" 13 minutes ago Up About a minute 6379/tcp, 0.0.0.0:6380->6380/tcp redis-slave-1
liumiaocn:redis liumiao$
只能连接从节点进行读操作,无法执行写操作,需要进行手工干预
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> auth liumiaocn@server
OK
127.0.0.1:6380> keys *
1) "greeting"
2) "version"
127.0.0.1:6380>
127.0.0.1:6380> set name "liumiao"
(error) READONLY You can't write against a read only replica.
127.0.0.1:6380>