拉取redis5.0.3镜像


# docker pull daocloud.io/library/redis:5.0.3


[root@localhost ~]# docker pull daocloud.io/library/redis:5.0.3 5.0.3: Pulling from library/redis 5e6ec7f28fb7: Pull complete 7fdf7fb3ec49: Pull complete 4bd5dbe7bfa7: Pull complete 71f540684935: Pull complete 8c6dbecedd0f: Pull complete 9d190b6684fc: Pull complete Digest: sha256:e1dc07bf1465661b90b18c1c766c00f733254bf64fcf2ad7a335b2fc1fa31870 Status: Downloaded newer image for daocloud.io/library/redis:5.0.3 [root@localhost ~]# [root@localhost ~]# docker images centos                                                 latest              9f38484d220f        2 weeks ago         202MB daocloud.io/library/redis                              5.0.3               82629e941a38        2 months ago        95MB ubuntu                                                 16.04               7e87e2b3bf7a        2 months ago        117MB mysql                                                  latest              102816b1ee7d        3 months ago        486MB




给redis5.0.3镜像打标签

# docker tag 82629e941a38 redis

删除源镜像标签

# docker rmi daocloud.io/library/redis:5.0.3


[root@localhost ~]# docker images REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE centos                                                 latest              9f38484d220f        2 weeks ago         202MB daocloud.io/library/redis                              5.0.3               82629e941a38        2 months ago        95MB redis                                                  latest              82629e941a38        2 months ago        95MB ubuntu                                                 16.04               7e87e2b3bf7a        2 months ago        117MB mysql                                                  latest              102816b1ee7d        3 months ago        486MB [root@localhost ~]# [root@localhost ~]# docker rmi daocloud.io/library/redis:5.0.3 Untagged: daocloud.io/library/redis:5.0.3 Untagged: daocloud.io/library/redis@sha256:e1dc07bf1465661b90b18c1c766c00f733254bf64fcf2ad7a335b2fc1fa31870 [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# docker images REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE centos                                                 latest              9f38484d220f        2 weeks ago         202MB redis                                                  latest              82629e941a38        2 months ago        95MB ubuntu                                                 16.04               7e87e2b3bf7a        2 months ago        117MB mysql                                                  latest              102816b1ee7d        3 months ago        486MB




创建6个redis容器集群

以redis7000为例:

非集群模式

# docker run --name redis8000 -p 8000:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no


集群模式

# docker run --name redis7000 -p 7000:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes

说明:

redis的为123456

Redis持久化功能已启动

保护模式已经关闭

集群模式已开启


[root@localhost ~]# docker run --name redis7000 -p 7000:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes d8b4eb2b5459b2eaceca1d957b9422123ac906c41a5bf8d23bd65545dda33c77 [root@localhost ~]# docker run --name redis7001 -p 7001:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes 163c705af09706b38e2f327febdecbf9c37d3b1528f7d4bfb667eb257d3a0be7 [root@localhost ~]# docker run --name redis7002 -p 7002:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes e4604f44be05811a9dbcf0af9f3f0b6db976dde9c8c2772d8773e6979fbb7284 [root@localhost ~]# docker run --name redis7003 -p 7003:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes a76f43511bb20945695943d009041b2f8398b1e3a5217524edfa34589ad91311 [root@localhost ~]# docker run --name redis7004 -p 7004:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes 9eace890c3a44314a01ef58fc0d09086f752be95300165868435b8dfbb1f61b2 [root@localhost ~]# docker run --name redis7005 -p 7005:6379 -d redis redis-server --requirepass "123456" --appendonly yes --protected-mode no --cluster-enabled yes e91e99f1c31f2deab8f87f0af75625087a2913ff382ef15e62bce2b8d472e60f [root@localhost ~]#




查看容器状态

# docker ps


[root@localhost ~]# docker ps CONTAINER ID        IMAGE                                                  COMMAND                  CREATED              STATUS              PORTS                    NAMES e91e99f1c31f        redis                                                  "docker-entrypoint.s…"   6 seconds ago        Up 5 seconds        0.0.0.0:7005->6379/tcp   redis7005 9eace890c3a4        redis                                                  "docker-entrypoint.s…"   16 seconds ago       Up 16 seconds       0.0.0.0:7004->6379/tcp   redis7004 a76f43511bb2        redis                                                  "docker-entrypoint.s…"   32 seconds ago       Up 31 seconds       0.0.0.0:7003->6379/tcp   redis7003 e4604f44be05        redis                                                  "docker-entrypoint.s…"   46 seconds ago       Up 45 seconds       0.0.0.0:7002->6379/tcp   redis7002 163c705af097        redis                                                  "docker-entrypoint.s…"   56 seconds ago       Up 56 seconds       0.0.0.0:7001->6379/tcp   redis7001 d8b4eb2b5459        redis                                                  "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:7000->6379/tcp   redis7000




查看6个redis容器的IP

# docker inspect redis7000 | grep IPAddress


[root@localhost ~]# docker inspect redis7000 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.6",                     "IPAddress": "172.17.0.6", [root@localhost ~]# [root@localhost ~]# docker inspect redis7001 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.8",                     "IPAddress": "172.17.0.8", [root@localhost ~]# [root@localhost ~]# docker inspect redis7002 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.9",                     "IPAddress": "172.17.0.9", [root@localhost ~]# [root@localhost ~]# docker inspect redis7003 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.10",                     "IPAddress": "172.17.0.10", [root@localhost ~]# docker inspect redis7004 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.11",                     "IPAddress": "172.17.0.11", [root@localhost ~]# [root@localhost ~]# docker inspect redis7005 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.12",                     "IPAddress": "172.17.0.12", [root@localhost ~]#




进入redis7000容器

# docker exec -it redis7000 bash

客户端交互


[root@localhost ~]# docker exec -it redis7000 bash root@d8b4eb2b5459:/data# root@d8b4eb2b5459:/data# redis-cli 127.0.0.1:6379> exit root@d8b4eb2b5459:/data# root@d8b4eb2b5459:/data# redis-cli -h 172.17.0.6 -p 6379 172.17.0.6:6379> 172.17.0.6:6379> exit root@d8b4eb2b5459:/data# root@d8b4eb2b5459:/data# redis-cli -h 172.17.0.8 -p 6379 172.17.0.8:6379> 172.17.0.8:6379> exit root@d8b4eb2b5459:/data#




集群创建

# redis-cli -a 123456 --cluster create 172.17.0.6:6379 172.17.0.8:6379 172.17.0.9:6379 172.17.0.10:6379 172.17.0.11:6379 172.17.0.12:6379 --cluster-replicas 1


root@d8b4eb2b5459:/data# root@d8b4eb2b5459:/data# redis-cli -a 123456 --cluster create 172.17.0.6:6379 172.17.0.8:6379 172.17.0.9:6379 172.17.0.10:6379 172.17.0.11:6379 172.17.0.12:6379 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 172.17.0.10:6379 to 172.17.0.6:6379 Adding replica 172.17.0.11:6379 to 172.17.0.8:6379 Adding replica 172.17.0.12:6379 to 172.17.0.9:6379 M: 19519487a7db6f2fa5fda5f6e1807befe75665e5 172.17.0.6:6379    slots:[0-5460] (5461 slots) master M: f6adb618124c791db4e31347982d086cd8b58646 172.17.0.8:6379    slots:[5461-10922] (5462 slots) master M: 1b5b85bd1f068a656b88e0e396f7da8bc0242ebf 172.17.0.9:6379    slots:[10923-16383] (5461 slots) master S: ca40bc6c36c7c14a7e91b04bec51bc6bb75eebea 172.17.0.10:6379    replicates 19519487a7db6f2fa5fda5f6e1807befe75665e5 S: b25431b20840b04fc2262d246482981a1a49c1cf 172.17.0.11:6379    replicates f6adb618124c791db4e31347982d086cd8b58646 S: 53515a889f77bbe553c8fb664983cded5d767826 172.17.0.12:6379    replicates 1b5b85bd1f068a656b88e0e396f7da8bc0242ebf Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ... >>> Performing Cluster Check (using node 172.17.0.6:6379) M: 19519487a7db6f2fa5fda5f6e1807befe75665e5 172.17.0.6:6379    slots:[0-5460] (5461 slots) master additional replica(s) M: 1b5b85bd1f068a656b88e0e396f7da8bc0242ebf 172.17.0.9:6379    slots:[10923-16383] (5461 slots) master additional replica(s) S: b25431b20840b04fc2262d246482981a1a49c1cf 172.17.0.11:6379    slots: (0 slots) slave    replicates f6adb618124c791db4e31347982d086cd8b58646 S: 53515a889f77bbe553c8fb664983cded5d767826 172.17.0.12:6379    slots: (0 slots) slave    replicates 1b5b85bd1f068a656b88e0e396f7da8bc0242ebf M: f6adb618124c791db4e31347982d086cd8b58646 172.17.0.8:6379    slots:[5461-10922] (5462 slots) master additional replica(s) S: ca40bc6c36c7c14a7e91b04bec51bc6bb75eebea 172.17.0.10:6379    slots: (0 slots) slave    replicates 19519487a7db6f2fa5fda5f6e1807befe75665e5 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. root@d8b4eb2b5459:/data#





配置文件启动redis方式
docker run -d --name redis_test -p 6379:6379  -v /var/local/conf/redis.conf:/redis.conf redis redis-server /redis.conf