Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

5.37. Spring boot with Docker stack

5.37.1. 編譯 Docker 鏡像

		
iMac:config neo$ mvn docker:build
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< cn.netkiller:config >-------------------------
[INFO] Building config 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- docker-maven-plugin:1.2.2:build (default-cli) @ config ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier, FixedRegistryAuthSupplier]
[INFO] Copying /Users/neo/workspace/Microservice/config/target/config-0.0.1-SNAPSHOT.jar -> /Users/neo/workspace/Microservice/config/target/docker/srv/config-0.0.1-SNAPSHOT.jar
[INFO] Building image netkiller/config
Step 1/7 : FROM openjdk

 ---> b2324c52d969
Step 2/7 : WORKDIR /srv

 ---> Using cache
 ---> f7c1730935c6
Step 3/7 : ADD /srv/config-0.0.1-SNAPSHOT.jar /srv/

 ---> Using cache
 ---> 8b5a053550ba
Step 4/7 : EXPOSE 8888

 ---> Running in 7f4e35b3564f
Removing intermediate container 7f4e35b3564f
 ---> a968ea58ba64
Step 5/7 : ENTRYPOINT ["java", "-jar", "-Djava.security.egd=file:/dev/./urandom", "/srv/config-0.0.1-SNAPSHOT.jar"]

 ---> Running in 6b110b5d16b7
Removing intermediate container 6b110b5d16b7
 ---> a8ab10c1c186
Step 6/7 : CMD ["java", "-version"]

 ---> Running in 4f2dc6e08404
Removing intermediate container 4f2dc6e08404
 ---> a74bbf7b6c30
Step 7/7 : VOLUME /tmp

 ---> Running in 0a3836ea768f
Removing intermediate container 0a3836ea768f
 ---> 5e13d81a9dea
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 5e13d81a9dea
Successfully tagged netkiller/config:latest
[INFO] Built netkiller/config
[INFO] Tagging netkiller/config with 0.0.1-SNAPSHOT
[INFO] Tagging netkiller/config with latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.516 s
[INFO] Finished at: 2020-09-20T21:49:28+08:00
[INFO] ------------------------------------------------------------------------		
		
		

5.37.2. 

初始化 Swarm

		
iMac:springboot neo$ docker swarm init
Swarm initialized: current node (qvqez97c8ja014ktmroy9sw47) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-49w6mcdjvj9nhb1go4wiazygupvj6qmjy7mgdb7x5bzqspldss-6yfvnij63it1qbs2nwvqw6xv0 192.168.65.3:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.		
		
		

創建 docker-compose.yml 檔案

		
version: '3.8'

services:
  config:
    image: netkiller/config:latest
    ports:
      - "8888"
    volumes:
      - /tmp/config:/tmp
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 50M		
		
		

部署服務

		
iMac:springboot neo$ docker stack deploy -c docker-compose.yml springboot
Creating network springboot_default
Creating service springboot_config		
		
		

查看部署情況

		
iMac:springboot neo$ docker stack ls
NAME                SERVICES            ORCHESTRATOR
springboot          1                   Swarm
iMac:springboot neo$ docker stack services springboot
ID                  NAME                MODE                REPLICAS            IMAGE                     PORTS
viavpkzk6lvo        springboot_config   replicated          0/1                 netkiller/config:latest   *:30001->8888/tcp
		
		
		

查看服務運行狀態

		
iMac:springboot neo$ docker stack ps springboot
ID                  NAME                  IMAGE                     NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
mr30ujsdbti4        springboot_config.1   netkiller/config:latest   docker-desktop      Running             Preparing 4 minutes ago