1)上传docker的rpm包到Linux 安装配置docker并设置镜像加速
[root@192 ~]# yum localinstall docker-ce-18.06.3.ce-3.el7.x86_64.rpm
已加载插件:fastestmirror
正在检查 docker-ce-18.06.3.ce-3.el7.x86_64.rpm: docker-ce-18.06.3.ce-3.el7.x86_64
加速上传daemon.json到/etc/docker目录下即可
[root@192 ~]# mkdir /etc/docker
[root@192 ~]# vi /etc/docker/daemon.json
[root@192 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://mzxx8xy8.mirror.aliyuncs.com"]
}
关闭防火墙,并启动docker
[root@192 ~]# systemctl stop firewalld
[root@192 ~]# setenforce 0
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl restart docker
[root@192 ~]#
2)搜索redis镜像并拉取
搜索镜像:
[root@192 ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 8260 [OK]
bitnami/redis Bitnami Redis Docker Image 147 [OK]
sameersbn/redis 80 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0 68
rediscommander/redis-commander Alpine image for redis-commander - Redis man… 40 [OK]
kubeguide/redis-master redis-master with "Hello World!" 31
redislabs/redis Clustered in-memory database engine compatib… 25
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 23
oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 21
arm32v7/redis Redis is an open source key-value store that… 21
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 14 [OK]
webhippie/redis Docker images for Redis 12 [OK]
redislabs/redisgraph A graph database module for Redis 11 [OK]
s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
insready/redis-stat Docker image for the real-time Redis monitor… 9 [OK]
arm64v8/redis Redis is an open source key-value store that… 9
redislabs/redisinsight RedisInsight - The GUI for Redis 7
redislabs/redismod An automated build of redismod - latest Redi… 7 [OK]
centos/redis-32-centos7 Redis in-memory data structure store, used a… 5
circleci/redis CircleCI images for Redis 4 [OK]
clearlinux/redis Redis key-value data structure server with t… 2
runnable/redis-stunnel stunnel to redis provided by linking contain… 1 [OK]
tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK]
wodby/redis Redis container image with orchestration 1 [OK]
xetamus/redis-resource forked redis-resource 0 [OK]
[root@192 ~]#
拉取redis镜像
[root@192 ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
8559a31e96f4: Pull complete
85a6a5c53ff0: Pull complete
b69876b7abed: Pull complete
1f590ddd7edb: Pull complete
e28bb38e9c0d: Pull complete
eb4bcff1416b: Pull complete
Digest: sha256:9217de21e5fe15ed5417768670ba8bec79f9fd9418456bcfa6d54979e53be1d7
Status: Downloaded newer image for redis:latest
[root@192 ~]#
3)查看系统当前镜像
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest c349430fd524 12 hours ago 104MB
[root@192 ~]#
4)运行redis-server容器并做持久化配置
[root@192 ~]# docker run --name redis1 -d -p 6379:6379 -v /opt/redis1:/data redis
e2a0dd909fdadb1d2fe08f410f89dff3845152a2e495a249ad142cb93cf29062
[root@192 ~]# docker run --name redis2 -d -p 6380:6379 -v /opt/redis2:/data redis
838b7c9932d8d3a0c4b2e459bd95f09549693471366801efa9513a75089421d0
[root@192 ~]# docker run --name redis3 -d -p 6381:6379 -v /opt/redis2:/data redis
433372a0f1d0156f2536ab03e8127d7e8af56039bf0100780447dbbc5dff1afc
[root@192 ~]#
5)docker启动3个redis容器服务,使用6379-6381这3个端口
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
433372a0f1d0 redis "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6381->6379/tcp redis3
838b7c9932d8 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6380->6379/tcp redis2
e2a0dd909fda redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6379->6379/tcp redis1
6)进入docker容器内部查看当前redis角色
[root@192 ~]# docker exec -it redis1 redis-cli
127.0.0.1:6379> role
1) "master"
2) (integer) 0
3) (empty array)
127.0.0.1:6379>
7)配置redis集群,一主两从
我们将redis1配置为主,redis2和redis3配置为从
登录redis2
[root@192 ~]# docker exec -it redis2 redis-cli
配置同步
127.0.0.1:6379> slaveof 192.168.237.129 6379
OK
127.0.0.1:6379> ROLE
1) "slave"
2) "192.168.237.129"
3) (integer) 6379
4) "connected"
5) (integer) 14
127.0.0.1:6379>
登录redis3做相同操作
[root@192 ~]# docker exec -it redis3 redis-cli
执行同步redis1操作
127.0.0.1:6379> slaveof 192.168.237.129 6379
OK
127.0.0.1:6379> role
1) "slave"
2) "192.168.237.129"
3) (integer) 6379
4) "connected"
5) (integer) 224
127.0.0.1:6379>
8)测试是否成功
在redis1上创建一个键值对儿
[root@192 ~]# docker exec -it redis1 redis-cli
127.0.0.1:6379> set name tomcat
OK
127.0.0.1:6379>
来redis2和redis3上查看是否同步成功
[root@192 ~]# docker exec -it redis2 redis-cli
127.0.0.1:6379> get name
"tomcat"
127.0.0.1:6379> exit
[root@192 ~]# docker exec -it redis3 redis-cli
127.0.0.1:6379> get name
"tomcat"
127.0.0.1:6379>
数据同步成功!搭建完毕