Docker部署Redis集群

在这里插入图片描述

[root@VM-16-7-centos /]# docker network create redis --subnet 172.38.0.0/16
01a7c284e1b2fb90abd773aeea9d6162323519fecc4ac85e1bd2c5e725fe4497
[root@VM-16-7-centos /]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
cce3f89aabdf   bridge    bridge    local
5bb052258d77   host      host      local
346d9a0447cc   mynet     bridge    local
18339c1989d9   none      null      local
01a7c284e1b2   redis     bridge    local

脚本创建6个redis配置

[root@VM-16-7-centos /] 
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >> /mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
[root@VM-16-7-centos /] cd /mydata
[root@VM-16-7-centos mydata]# ls
redis
[root@VM-16-7-centos mydata] cd redis
[root@VM-16-7-centos redis] ls
node-1  node-2  node-3  node-4  node-5  node-6

[root@VM-16-7-centos redis]# docker run -p 6371:6379 -p 16671:16379 --name redis-1
-v /mydata/redis/node-1/data:/data
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf
-d --net redis --ip 172.38.0.1 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#通过脚本启动6个

for port in $(seq 1 6);\
do \
 docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
 -v /mydata/redis/node-${port}/data:/data \
 -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
 -d --net redis --ip 172.38.0.${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
EOF
done
[root@VM-16-7-centos redis]# 
for port in $(seq 1 6);\
 do \
  docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
  -v /mydata/redis/node-${port}/data:/data \
  -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
  -d --net redis --ip 172.38.0.${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
 EOF
 done
 
Unable to find image 'redis:5.0.9-alpine3.11' locally
5.0.9-alpine3.11: Pulling from library/redis
cbdbe7a5bc2a: Pull complete 
dc0373118a0d: Pull complete 
cfd369fe6256: Pull complete 
3e45770272d9: Pull complete 
558de8ea3153: Pull complete 
a2c652551612: Pull complete 
Digest: sha256:83a3af36d5e57f2901b4783c313720e5fa3ecf0424ba86ad9775e06a9a5e35d0
Status: Downloaded newer image for redis:5.0.9-alpine3.11
5ef7bb42f7de8b539162b724edd7aa64eb6b707a40d08d82ea85e6906510974a
docker: Error response from daemon: Address already in use.
-bash: EOF: command not found
13a6b0cb78b052a12033a467441986498bcb8c15fbf1e871c04c97a31c1b263c
-bash: EOF: command not found
7a9d1e76944be6c8622ad2f91d4283a2c28263ead249dcfeca2e0426c0196800
-bash: EOF: command not found
4a9265db1fe3aa555035bdfc11b92292a9e204153c7a1a5b0c0e8976eb37944c
-bash: EOF: command not found
fbd4c4a7c53c23019ec0a7ec47297e35f1d1b88fc1cf94f0dc04b342a1410c0d
-bash: EOF: command not found
8a9a331727039c9e1410eb7dfd46e0fe07eeabbdeca1b03dbe09ee4d38a5a983
-bash: EOF: command not found
[root@VM-16-7-centos redis] docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                                                                      NAMES
69e4b038b231   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   15 seconds ago   Up 14 seconds   0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16671->16379/tcp, :::16671->16379/tcp   redis-1
8a9a33172703   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16676->16379/tcp, :::16676->16379/tcp   redis-6
fbd4c4a7c53c   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16675->16379/tcp, :::16675->16379/tcp   redis-5
4a9265db1fe3   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16674->16379/tcp, :::16674->16379/tcp   redis-4
7a9d1e76944b   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16673->16379/tcp, :::16673->16379/tcp   redis-3
13a6b0cb78b0   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16672->16379/tcp, :::16672->16379/tcp   redis-2

进入redis
创建集群

[root@VM-16-7-centos redis]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof  nodes.conf
/data # 
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.2:6379 172.38.0.3:6379 172.38.0.4:6379 172.38.0.5:6379 172.38.0.6:6379 --cluster-replicas 1
  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.38.0.5:6379 to 172.38.0.11:6379
Adding replica 172.38.0.6:6379 to 172.38.0.2:6379
Adding replica 172.38.0.4:6379 to 172.38.0.3:6379
M: 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 7455fa410efeeffa0c943567f1004372e7d91843 172.38.0.2:6379
   slots:[5461-10922] (5462 slots) master
M: 974362ac90f23c8bd02e946a5f1492da658081d3 172.38.0.3:6379
   slots:[10923-16383] (5461 slots) master
S: bad325d9211012f714e4e3e2b2824b8d927e6b31 172.38.0.4:6379
   replicates 974362ac90f23c8bd02e946a5f1492da658081d3
S: fe64cf35da2de8396a47d5f7a410ed8eab7d99bd 172.38.0.5:6379
   replicates 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d
S: 9d1455bc072757d3f88c098233a6306b0cd3aa36 172.38.0.6:6379
   replicates 7455fa410efeeffa0c943567f1004372e7d91843
Can I set the above configuration? (type 'yes' to accept): /data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.2:6379 172.38.0.3:6379 172.38.0.4:6379 172.38.0.5:6379 172.38.0.6:6379 --cluster-replicas 1
>>> 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.38.0.5:6379 to 172.38.0.11:6379
Adding replica 172.38.0.6:6379 to 172.38.0.2:6379
Adding replica 172.38.0.4:6379 to 172.38.0.3:6379
M: 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 7455fa410efeeffa0c943567f1004372e7d91843 172.38.0.2:6379
   slots:[5461-10922] (5462 slots) master
M: 974362ac90f23c8bd02e946a5f1492da658081d3 172.38.0.3:6379
   slots:[10923-16383] (5461 slots) master
S: bad325d9211012f714e4e3e2b2824b8d927e6b31 172.38.0.4:6379
   replicates 974362ac90f23c8bd02e946a5f1492da658081d3
S: fe64cf35da2de8396a47d5f7a410ed8eab7d99bd 172.38.0.5:6379
   replicates 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d
S: 9d1455bc072757d3f88c098233a6306b0cd3aa36 172.38.0.6:6379
   replicates 7455fa410efeeffa0c943567f1004372e7d91843
Can I set the above configuration? (type 'yes' to accept): /data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.2:6379 172.38.0.3:6379 172.38.0.4:6379 172.38.0.5:6379 172.38.0.6:6379 --cluster-replicas 1
>>> 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.38.0.5:6379 to 172.38.0.11:6379
Adding replica 172.38.0.6:6379 to 172.38.0.2:6379
Adding replica 172.38.0.4:6379 to 172.38.0.3:6379
M: 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d 172.38.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 7455fa410efeeffa0c943567f1004372e7d91843 172.38.0.2:6379
   slots:[5461-10922] (5462 slots) master
M: 974362ac90f23c8bd02e946a5f1492da658081d3 172.38.0.3:6379
   slots:[10923-16383] (5461 slots) master
S: bad325d9211012f714e4e3e2b2824b8d927e6b31 172.38.0.4:6379
   replicates 974362ac90f23c8bd02e946a5f1492da658081d3
S: fe64cf35da2de8396a47d5f7a410ed8eab7d99bd 172.38.0.5:6379
   replicates 0e45a740d72dbd575c2f3cbe63b10bbbaebe908d
S: 9d1455bc072757d3f88c098233a6306b0cd3aa36 172.38.0.6:6379
   replicates 7455fa410efeeffa0c943567f1004372e7d91843
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
.................................................................................................

/data # redis-cli -c #进入集群
127.0.0.1:6379> cluster info #集群信息
cluster_state:fail
cluster_slots_assigned:5461
cluster_slots_ok:5461
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:1
cluster_current_epoch:1
cluster_my_epoch:1
cluster_stats_messages_ping_sent:10
cluster_stats_messages_pong_sent:851
cluster_stats_messages_sent:861
cluster_stats_messages_ping_received:846
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:851
127.0.0.1:6379> 

cluster nodes 查看集群关联信息

SpringBoot项目打包Docker镜像

创建在这里插入图片描述
创建Dockerfile
在这里插入图片描述

FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

打包 并上传
在这里插入图片描述

[root@VM-16-7-centos /]# cd home
[root@VM-16-7-centos home]# ls
ceshi  dockerfile  docker-test-volume  idea  lighthouse  lisa  mysql  yzkStudy
[root@VM-16-7-centos home]# cd idea
[root@VM-16-7-centos idea]# ls
demo-0.0.1-SNAPSHOT.jar  Dockerfile
[root@VM-16-7-centos idea]# docker build -t yzk666 .
Sending build context to Docker daemon  17.63MB
Step 1/5 : FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY *.jar /app.jar
 ---> 669438b3b7f5
Step 3/5 : CMD ["--server.port=8080"]
 ---> Running in 16e27d9ad515
Removing intermediate container 16e27d9ad515
 ---> 0ac1579f5666
Step 4/5 : EXPOSE 8080
 ---> Running in b4b88db2fbd7
Removing intermediate container b4b88db2fbd7
 ---> 7b1ce0ad03e2
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 2469444b4c66
Removing intermediate container 2469444b4c66
 ---> ffaa5da04db6
Successfully built ffaa5da04db6
Successfully tagged yzk666:latest
[root@VM-16-7-centos idea]# docker images
REPOSITORY   TAG                IMAGE ID       CREATED          SIZE
yzk666       latest             ffaa5da04db6   54 seconds ago   661MB
redis        5.0.9-alpine3.11   3661c84ee9d0   2 years ago      29.8MB
java         8                  d23bdf5b1b1b   5 years ago      643MB

[root@VM-16-7-centos idea]# docker run -d -P --name yzkSpring yzk666
fc92b2d46c36694b27332fee34453c2d14bcc1aec53b37d7439bdbb5d1abd057
[root@VM-16-7-centos idea]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
fc92b2d46c36   yzk666                   "java -jar /app.jar …"   About a minute ago   Up About a minute   0.0.0.0:49166->8080/tcp, :::49166->8080/tcp                                                yzkSpring
[root@VM-16-7-centos idea]# curl localhost:49166/hello
我日 脑壳痛
[root@VM-16-7-centos idea]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值