博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DockerSwarm 微服务部署
阅读量:6304 次
发布时间:2019-06-22

本文共 9872 字,大约阅读时间需要 32 分钟。

hot3.png

文章首发于公众号《程序员果果》

地址:

一、简介

之前《服务Docker化》中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-compose.yml) 来配置、启动多个服务。

在《DockerSwarm集群环境搭建》中,我们使用docker service create 来部署服务时,一次只能部署一个服务,这一节就讲解 DockerSwarm 集群环境中, 使用 docker-compose.yml 一次启动多个关联的服务。

二、创建 SpringCloud 项目

创建一个springcloud项目 ,包含eureka-server、service-hi、service-ribbon。

1. eureka-server 项目

pom.xml

4.0.0
com.gf
eureka-server
0.0.1-SNAPSHOT
jar
eureka-server
Demo project for Spring Boot
com.gf
chapter02
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
repackage

application.yml

server:  port: 8761spring:  application:    name: eureka-servereureka:  client:    register-with-eureka: false    fetch-registry: false    service-url:      defaultZone: http://eureka-server:8761/eureka/  instance:    prefer-ip-address: true    instance-id: eureka-server:8761

EurekaServerApplication

@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaServerApplication.class, args);	}}

2. service-hi 项目

pom.xml

4.0.0
com.gf
service-hi
0.0.1-SNAPSHOT
jar
service-hi
Demo project for Spring Boot
com.gf
chapter02
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
repackage

application.yml

server:  port: 8763spring:  application:    name: service-hieureka:  client:    serviceUrl:      defaultZone: http://eureka-server:8761/eureka/  instance:    prefer-ip-address: true    instance-id: service-hi:8763

ServiceHiApplication

@EnableEurekaClient@SpringBootApplication@RestControllerpublic class ServiceHiApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceHiApplication.class, args);	}	@Value( "${server.port}" )	private String port;	@GetMapping("/hi")	public String hi() {		return "hello , port is " + port;	}}

3. service-ribbon 项目

pom.xml

4.0.0
com.gf
service-ribbon
0.0.1-SNAPSHOT
jar
service-ribbon
Demo project for Spring Boot
com.gf
chapter02
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
repackage

application.yml

server:  port: 8764spring:  application:    name: service-ribboneureka:  client:    serviceUrl:      defaultZone: http://eureka-server:8761/eureka/  instance:    prefer-ip-address: true    instance-id: eureka-server:8764

HelloService

@Servicepublic class HelloService {    @Autowired    private RestTemplate restTemplate;    public String hiService() {        return restTemplate.getForObject( "http://service-hi:8763/hi" , String.class );    }}

HelloControler

@RestControllerpublic class HelloControler {    @Autowired    private HelloService helloService;    @GetMapping(value = "/hi")    public String hi() {        return helloService.hiService();    }}

ServiceRibbonApplication

@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class ServiceRibbonApplication {	public static void main(String[] args) {		SpringApplication.run(ServiceRibbonApplication.class, args);	}	@Bean	@LoadBalanced	RestTemplate restTemplate() {		return new RestTemplate();	}}

三、构建镜像

1. Dockerfile

编写Dockerfile ,把项目构建成镜像,需要把 项目jar包 复制到 镜像中,而且镜像中要有java的运行环境,所以现在给每个项目都创建一个Dockerfile,内容如下:

eureka-server 项目的 Dockerfile

FROM hub.gf.com:9090/jdk/openjdk:8-jreMAINTAINER gf gf@163.comCOPY target/eureka-server-0.0.1-SNAPSHOT.jar /eureka-server-0.0.1-SNAPSHOT.jarENTRYPOINT ["java" , "-jar" , "/eureka-server-0.0.1-SNAPSHOT.jar"]

service-hi 项目的 Dockerfile

FROM hub.gf.com:9090/jdk/openjdk:8-jreMAINTAINER gf gf@163.comCOPY target/service-hi-0.0.1-SNAPSHOT.jar /service-hi-0.0.1-SNAPSHOT.jarENTRYPOINT ["java" , "-jar" , "/service-hi-0.0.1-SNAPSHOT.jar"]

service-ribbon 项目的 Dockerfile

#!/usr/bin/env bashmvn package -Dmaven.test.skip=truedocker build -t hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest .docker login -u admin -p Harbor12345 hub.gf.com:9090docker push hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest

2. 创建 build.sh

为了方便,三个项目根目录下创建 build.sh 脚本,来一键执行项目的打jar包、构建镜像、推送到私有仓库。

eureka-server 项目的 build.sh

#!/usr/bin/env bashmvn package -Dmaven.test.skip=truedocker build -t hub.gf.com:9090/springboot-ribbon/eureka-server:latest .docker login -u admin -p Harbor12345 hub.gf.com:9090docker push hub.gf.com:9090/springboot-ribbon/eureka-server:latest

service-hi 项目的 build.sh

#!/usr/bin/env bashmvn package -Dmaven.test.skip=truedocker build -t hub.gf.com:9090/springboot-ribbon/service-hi:latest .docker login -u admin -p Harbor12345 hub.gf.com:9090docker push hub.gf.com:9090/springboot-ribbon/service-hi:latest

service-ribbon 项目的 build.sh

#!/usr/bin/env bashmvn package -Dmaven.test.skip=truedocker build -t hub.gf.com:9090/springboot-ribbon/service-ribbon:latest .docker login -u admin -p Harbor12345 hub.gf.com:9090docker push hub.gf.com:9090/springboot-ribbon/service-ribbon:latest

分别执行三个 build.sh 脚本,这样私有仓库就有三个项目的镜像了,如图:

三、部署服务

1. 启动集群环境

启动之前搭建好的 docker swarm 集群环境:

docker-machine start myvm-1 myvm-2 myvm-3

要在管理节点下部署服务,所以需要知道哪台是管理节点,随便连接一台机器,通过 docker node 命令查看节点信息:

docker-machine ssh myvm-1
docker node lsID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSIONib1498ex2q18i7gznb2zgicqq *   myvm-1              Ready               Active              Leader              18.09.1-beta2vels0fe3eh5s5cxj1s573v9wx     myvm-2              Ready               Active              Reachable           18.09.1-beta2obxnnqelh4p16wajrwvyn6j8v     myvm-3              Ready               Active              Reachable           18.09.1-beta2

myvm-1 就是管理节点,不需要切换节点了。

2. 编写 services.yml

之后用 docker stack 部署服务,所以需要编写服务编排文件,内容如下:

version: "3.4"services:  eureka-server:    image: hub.gf.com:9090/springcloud-ribbon/eureka-server:latest    deploy:      endpoint_mode: vip      resources:        limits:          cpus: "0.5"          memory: "1024M"    ports:      - "8761:8761"  service-hi:    image: hub.gf.com:9090/springcloud-ribbon/service-hi:latest    deploy:      endpoint_mode: vip      resources:        limits:          cpus: "0.5"          memory: "1024M"    ports:      - "8763:8763"    depends_on:      - eureka-server  service-ribbon:    image: hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest    deploy:      endpoint_mode: vip      resources:        limits:          cpus: "0.5"          memory: "1024M"    ports:      - "8764:8764"    depends_on:      - eureka-server      - service-hinetworks:  default:    external:      name: my-overlay

文件详细说明,这里就不说了,可以网上查一下。

3. 启动服务

通过 docker stack deploy 命令 启动服务:

docker stack deploy -c services.yml ms

通过 docker service ls 查看服务启动状态:

docker service lsID                  NAME                MODE                REPLICAS            IMAGE                                                      PORTSq99gd5rquv3f        ms_eureka-server    replicated          1/1                 hub.gf.com:9090/springcloud-ribbon/eureka-server:latest    *:8761->8761/tcpwjsv5s6fce6k        ms_service-hi       replicated          1/1                 hub.gf.com:9090/springcloud-ribbon/service-hi:latest       *:8763->8763/tcpzjwe7cnpn42y        ms_service-ribbon   replicated          1/1                 hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest   *:8764->8764/tcp

服务启动后 ,访问 192.168.99.100:8761 , 192.168.99.100:8763/hi , 192.168.99.100:8764/hi ,都可以正常访问,说明已经部署成功了。

关注我

欢迎扫码或微信搜索公众号《程序员果果》关注我,关注有惊喜~

转载于:https://my.oschina.net/u/2367201/blog/3056340

你可能感兴趣的文章
Go语言基础之结构体
查看>>
SpringCloud:Eureka Client项目搭建(Gradle项目)
查看>>
jqueryValidate
查看>>
ATL使用IE控件,并且屏蔽右键
查看>>
Jenkins
查看>>
linux下使用screen和ping命令对网络质量进行监控
查看>>
数据库设计技巧
查看>>
css定位概述
查看>>
C# 动态修改配置文件 (二)
查看>>
BOM:文档对象模型 --树模型
查看>>
我的Android进阶之旅------>WindowManager.LayoutParams介绍
查看>>
segment
查看>>
获取鼠标的原始移动值
查看>>
Linux信号 编程
查看>>
有关滚动与位置
查看>>
Box2D自定义重力
查看>>
chpasswd
查看>>
mysqldump --single-transaction 和--lock-tables参数详解
查看>>
android 数据库_sql语句总结
查看>>
python购物车
查看>>