5.部署Redis集群
上面是主力机,下面是备用机
如果上面挂了,下面的就要去替代上面的。
[root@Jsxs ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4aba86a517af portainer/portainer "/portainer" 43 hours ago Up 43 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp eager_agnesi [root@Jsxs ~]# docker network create redis --subnet 172.38.0.0/16 b30cea89bd6c28834a7cf459b4b86471a3897e2caa20064f3ac0d3dc0a8681bb [root@Jsxs ~]# docker network ls NETWORK ID NAME DRIVER SCOPE dd0a347647db bridge bridge local 3393698886d8 host host local 5448dff92c6d mynet bridge local bc710e60b055 none null local b30cea89bd6c redis bridge local ------------------------------------------------------------------------- 通过脚本创建六个redis配置 直接复制到命令行运行即可 for port in $(seq 1 6); \ do \ mkdir -p /mydata/redis/node-${port}/conf #再目录/mydata中 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@Jsxs ~]# 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@Jsxs ~]# cd /mydata [root@Jsxs mydata]# ls redis [root@Jsxs mydata]# cd redis/ [root@Jsxs redis]# ls node-1 node-2 node-3 node-4 node-5 node-6 --------------------------------------------------------------------------------- 上面就是创建的6个redis配置 下面开始运行创建的6个节点 # 开始运行并创建6个容器,端口映射6379 名字 数据卷绑定 网络使用redis网络并绑定ip docker run -p 6371:6379 -p 16371: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.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6372:6379 -p 16372:16379 --name redis-2 \ -v /mydata/redis/node-2/data:/data \ -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6373:6379 -p 16373:16379 --name redis-3 \ -v /mydata/redis/node-3/data:/data \ -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6374:6379 -p 16374:16379 --name redis-4 \ -v /mydata/redis/node-4/data:/data \ -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6375:6379 -p 16375:16379 --name redis-5 \ -v /mydata/redis/node-5/data:/data \ -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6376:6379 -p 16376:16379 --name redis-6 \ -v /mydata/redis/node-6/data:/data \ -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf --- [root@Jsxs redis]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0a19eb367dc redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6 68cf748d322f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 17 seconds ago Up 16 seconds 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5 a5504370b19d redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 31 seconds ago Up 29 seconds 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4 f3d61ce52b42 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 39 seconds ago Up 38 seconds 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3 175c61fc2523 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 53 seconds ago Up 52 seconds 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2 d4050cf76348 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1 4aba86a517af portainer/portainer "/portainer" 44 hours ago Up 44 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp eager_agnesi ---
- 搭建集群
[root@Jsxs redis]# docker exec -it redis-1 /bin/bash OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown [root@Jsxs redis]# docker exec -it redis-1 /bin/sh #启动集群1 /data # ls appendonly.aof nodes.conf #搭建集群 [root@Jsxs redis]# redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16: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.15:6379 to 172.38.0.11:6379 Adding replica 172.38.0.16:6379 to 172.38.0.12:6379 Adding replica 172.38.0.14:6379 to 172.38.0.13:6379 M: a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 172.38.0.11:6379 slots:[0-5460] (5461 slots) master M: e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master M: 46e745ac115598f54975b2ad4432dc087d678df7 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master S: 121b91b02057378351c43b63abf62c7c5a281bf6 172.38.0.14:6379 replicates 46e745ac115598f54975b2ad4432dc087d678df7 S: 93ff81b2883180b3266186f42caed428d01b6199 172.38.0.15:6379 replicates a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 S: af14b91dd41e0470d4c28702340716c48e61eb66 172.38.0.16:6379 replicates e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 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.38.0.11:6379) M: a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 172.38.0.11:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 93ff81b2883180b3266186f42caed428d01b6199 172.38.0.15:6379 slots: (0 slots) slave replicates a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 M: 46e745ac115598f54975b2ad4432dc087d678df7 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 121b91b02057378351c43b63abf62c7c5a281bf6 172.38.0.14:6379 slots: (0 slots) slave replicates 46e745ac115598f54975b2ad4432dc087d678df7 S: af14b91dd41e0470d4c28702340716c48e61eb66 172.38.0.16:6379 slots: (0 slots) slave replicates e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
- 测试
/data # redis-cli -c 127.0.0.1:6379> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:454 cluster_stats_messages_pong_sent:471 cluster_stats_messages_sent:925 cluster_stats_messages_ping_received:466 cluster_stats_messages_pong_received:454 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:925 127.0.0.1:6379> cluster nodes e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 172.38.0.12:6379@16379 master - 0 1679619838764 2 connected 5461-10922 93ff81b2883180b3266186f42caed428d01b6199 172.38.0.15:6379@16379 slave a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 0 1679619838262 5 connected 46e745ac115598f54975b2ad4432dc087d678df7 172.38.0.13:6379@16379 master - 0 1679619839795 3 connected 10923-16383 121b91b02057378351c43b63abf62c7c5a281bf6 172.38.0.14:6379@16379 slave 46e745ac115598f54975b2ad4432dc087d678df7 0 1679619838000 4 connected af14b91dd41e0470d4c28702340716c48e61eb66 172.38.0.16:6379@16379 slave e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 0 1679619839565 6 connected a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 172.38.0.11:6379@16379 myself,master - 0 1679619837000 1 connected 0-5460 127.0.0.1:6379> set a b -> Redirected to slot [15495] located at 172.38.0.13:6379 OK 172.38.0.13:6379> get a ^C /data # redis-cli -c #已经停掉主机了,再次重连。我们发现主机变了 127.0.0.1:6379> get a -> Redirected to slot [15495] located at 172.38.0.14:6379 "b" 172.38.0.14:6379> cluster nodes 121b91b02057378351c43b63abf62c7c5a281bf6 172.38.0.14:6379@16379 myself,master - 0 1679620066000 7 connected 10923-16383 a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 172.38.0.11:6379@16379 master - 0 1679620067986 1 connected 0-5460 af14b91dd41e0470d4c28702340716c48e61eb66 172.38.0.16:6379@16379 slave e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 0 1679620067484 6 connected 93ff81b2883180b3266186f42caed428d01b6199 172.38.0.15:6379@16379 slave a1a026ec68ef5d0f1089082bb5d9f3d986f0a920 0 1679620066453 5 connected e36cbe81e72f8b4ebfbf9a82b18fe11ea38c8720 172.38.0.12:6379@16379 master - 0 1679620066553 2 connected 5461-10922 46e745ac115598f54975b2ad4432dc087d678df7 172.38.0.13:6379@16379 master,fail - 1679619945828 1679619945025 3 connected
停掉服务
[root@Jsxs ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0a19eb367dc redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6 68cf748d322f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5 a5504370b19d redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4 f3d61ce52b42 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3 175c61fc2523 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2 d4050cf76348 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1 4aba86a517af portainer/portainer "/portainer" 44 hours ago Up 44 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp eager_agnesi [root@Jsxs ~]# docker stop redis-3 redis-3
6.SpringBoot微服务打包Docker镜像
(1).构建SpringBoot项目并打包
两次测试是否能启动
(2).创建Dockerfile文件并编写配置
# 基础镜像来自java8 FROM java:8 # 拷贝当前路径下所有jar包,到项目的,重命名app.jar COPY *.jar /app.jar # 启动名命令 CMD ["--server.port=8080"] # 暴露端口 EXPOSE 8080 # 执行命令 ENTRYPOINT ["java", "-jar", "/app.jar"]
(3).上传jar包和Dockerfile文件到linux
[root@Jsxs ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4aba86a517af portainer/portainer "/portainer" 44 hours ago Up 44 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp eager_agnesi [root@Jsxs ~]# cd /home [root@Jsxs home]# ls jsxs liloucun liloucunManger properties www [root@Jsxs home]# mkdir idea [root@Jsxs home]# cd idea/ [root@Jsxs idea]# ls [root@Jsxs idea]# ls Dockerfile Docker-hello-0.0.1-SNAPSHOT.jar [root@Jsxs idea]# docker build -t jsxs666 . [+] Building 45.5s (7/7) FINISHED => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 304B 0.0s => [internal] load metadata for docker.io/library/java:8 15.9s => [internal] load build context 0.4s => => transferring context: 17.65MB 0.4s => [1/2] FROM docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 27.2s => => resolve docker.io/library/java:8@sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 0.0s => => sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d 2.00kB / 2.00kB 0.0s => => sha256:fce5728aad85a763fe3c419db16885eb6f7a670a42824ea618414b8fb309ccde 18.54MB / 18.54MB 2.3s => => sha256:d23bdf5b1b1b1afce5f1d0fd33e7ed8afbc084b594b9ccf742a5b27080d8a4a8 4.73kB / 4.73kB 0.0s => => sha256:5040bd2983909aa8896b9932438c3f1479d25ae837a5f6220242a264d0221f2d 51.36MB / 51.36MB 13.6s => => sha256:76610ec20bf5892e24cebd4153c7668284aa1d1151b7c3b0c7d50c579aa5ce75 42.50MB / 42.50MB 11.3s => => sha256:60170fec2151d2108ed1420625c51138434ba4e0223d3023353d3f32ffe3cfc2 593.15kB / 593.15kB 2.7s => => sha256:e98f73de8f0d2ef292f58b004d67bc6e9ee779dcfaff7ebb3964649d4787b872 214B / 214B 3.0s => => sha256:11f7af24ed9cf47597dd6cf9963bb3e9109c963f0135e869a9e9b4999fdc12a3 242B / 242B 3.4s => => sha256:49e2d6393f32abb1de7c9395c04c822ceb2287383d5a90998f7bd8dbfd43d48c 130.10MB / 130.10MB 21.8s => => sha256:bb9cdec9c7f337940f7d872274353b66e118412cbfd433c711361bcf7922aea4 289.05kB / 289.05kB 11.7s => => extracting sha256:5040bd2983909aa8896b9932438c3f1479d25ae837a5f6220242a264d0221f2d 2.0s => => extracting sha256:fce5728aad85a763fe3c419db16885eb6f7a670a42824ea618414b8fb309ccde 0.5s => => extracting sha256:76610ec20bf5892e24cebd4153c7668284aa1d1151b7c3b0c7d50c579aa5ce75 1.9s => => extracting sha256:60170fec2151d2108ed1420625c51138434ba4e0223d3023353d3f32ffe3cfc2 0.1s => => extracting sha256:e98f73de8f0d2ef292f58b004d67bc6e9ee779dcfaff7ebb3964649d4787b872 0.0s => => extracting sha256:11f7af24ed9cf47597dd6cf9963bb3e9109c963f0135e869a9e9b4999fdc12a3 0.0s => => extracting sha256:49e2d6393f32abb1de7c9395c04c822ceb2287383d5a90998f7bd8dbfd43d48c 4.5s => => extracting sha256:bb9cdec9c7f337940f7d872274353b66e118412cbfd433c711361bcf7922aea4 0.0s => [2/2] COPY *.jar /app.jar 0.4s => exporting to image 1.7s => => exporting layers 1.7s => => writing image sha256:36b36b1a1129b33cd570a9dcef34190473009e60c75264f7df0d1c660f595560 0.0s => => naming to docker.io/library/jsxs666
(4).启动测试
[root@Jsxs idea]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE jsxs666 latest 36b36b1a1129 44 seconds ago 661MB tomcat 7 9dfd74e6bc2f 20 months ago 533MB portainer/portainer latest 580c0e4e98b0 2 years ago 79.1MB #启动容器 [root@Jsxs idea]# docker run -d -P --name jsxs66-springboot jsxs666 20c4bc16322db4a60aa96b9c4f24e25a4d40d93d7b9016ce363d261526e373b0 [root@Jsxs idea]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 20c4bc16322d jsxs666 "java -jar /app.jar …" 36 seconds ago Up 34 seconds 0.0.0.0:32782->8080/tcp, :::32781->8080/tcp jsxs66-springboot 4aba86a517af portainer/portainer "/portainer" 45 hours ago Up 44 hours 0.0.0.0:8088->9000/tcp, :::8088->9000/tcp eager_agnesi [root@Jsxs idea]# curl localhost:32782 #测试 {"timestamp":"2023-03-24T01:42:32.441+00:00","status":404,"error":"Not Found","path":"/"}[root@Jsxs idea]# curl localhost:32782/hello hello jsxs![root@Jsxs idea]#