1.安装配置docker并设置镜像加速
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
sudo service docker start
docker --version
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://jk2v9c0j.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
2.搜索redis镜像并拉取
docker search redis
docker pull redis
3.查看系统镜像
[root@aly lnmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest f9b990972689 7 days ago 104MB
4.运行redis-server容器并做持久化配置
[root@aly lnmp]# docker run --name redis1 -d -p 6379:6379 -v /opt/redis1:/data redis
aa1690e478b14b6e180a5661036a5114efe5685f6716ee5238341a92bf5260c0
[root@aly lnmp]# docker run --name redis2 -d -p 6380:6379 -v /opt/redis1:/data redis
997ce608ffc10b66e7162e8064d9e83da433d2f3237f95d86badaaaf51daa11f
[root@aly lnmp]# docker run --name redis3 -d -p 6381:6379 -v /opt/redis1:/data redis
5.docker启动3个redis容器服务,使用6379-6381这3个端口
[root@aly lnmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c02271f42da4 redis "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 0.0.0.0:6381->6379/tcp redis3
997ce608ffc1 redis "docker-entrypoint.s…" 25 seconds ago Up 24 seconds 0.0.0.0:6380->6379/tcp redis2
aa1690e478b1 redis "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:6379->6379/tcp redis1
6.进入docker容器内部查看当前redis角色
[root@aly lnmp]# docker exec -it redis1 redis-cli
127.0.0.1:6379> role
1) "master"
2) (integer) 0
3) (empty array)
7.配置redis集群,一主两从
我们将redis1配置为主,redis2和redis3配置为从
#登录redis2
[root@aly lnmp]# docker exec -it redis2 redis-cli
#配置同步
127.0.0.1:6379> slaveof 172.17.23.192 6379
OK
#测试是否成功
127.0.0.1:6379> role
1) "slave"
2) "172.17.23.192"
3) (integer) 6379
4) "connected"
5) (integer) 0
127.0.0.1:6379> exit
#登录redis3做同样的操作
[root@aly lnmp]# docker exec -it redis3 redis-cli
127.0.0.1:6379> slaveof 172.17.23.192 6379
OK
#测试是否成功
127.0.0.1:6379> role
1) "slave"
2) "172.17.23.192"
3) (integer) 6379
4) "connected"
5) (integer) 28
127.0.0.1:6379> exit
8.测试是否成功
在redis1上创建一个键值对儿
#在redis1上创建一个键值对儿
[root@aly lnmp]# docker exec -it redis1 redis-cli
127.0.0.1:6379> set name tom
OK
127.0.0.1:6379> get name
"tom"
9.来redis2和redis3上查看是否同步成功
[root@aly lnmp]# docker exec -it redis3 redis-cli
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> exit
[root@aly lnmp]# docker exec -it redis2 redis-cli
127.0.0.1:6379> get name
"tom"