标签:语法 code href https dock closed tps 成长之路 关于
0.前言
一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂。因此学习东西还是要循序渐进,慢慢来。先了解单机编排技术Docker Compose,了解一些技术细节及原理后,在入手K8s。还是不能一口吃成胖子,要多吃几口才可以。而且目前公司都是一些小项目,能用得上DockerCompose已经很不错了,还想要上K8s,估计是不现实的。
1. 安装
可以通过运行下面命令进行安装,
1 curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o docker-compose 2 chmod +x docker-compose 3 mv docker-compose /usr/local/bin
安装Tab自动补全
curl -L https://raw.githubusercontent.com/docker/compose/1.23.1/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
由于我看得是《Spring Cloud与Docker微服务架构实战 第二版》这本书,里面刚好有个例子,这里就用这个做练手。
1 git clone https://github.com/itmuch/spring-cloud-docker-microservice-book-code-docker/ 2 cd spring-cloud-docker-microservice-book-code-docker 3 cd docker-1-simple 4 mvn clean package docker:build
里面的每个微服务都构建成一个Docker Image,然后就可以开干了。
2. 简单例子
从一个简单的例子开始
Dockerfile 文件
1 FROM java:latest 2 VOLUME /tmp 3 ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar app.jar 4 RUN bash -c ‘touch /app.jar‘ 5 EXPOSE 9000 6 ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
docker-compose.yml 文件
1 version: ‘3‘ 2 services: 3 eureka: 4 build: . 5 ports: 6 - "8761:8761"
执行 docker-compose up
从下图可以看出,docker-compose 执行后,分别创建了 compose_eureka Image, compose_default Network, compose_eureka_1_b5 Container
以上就是入门了,关于docker-compose更多的语法,这里就不展开了,具体肯定是参考其他博客,或者官方文档吧
3. 实战练习
参考书本上的例子进行练习
3.1 基于Spring Boot微服务的Compose编写
这里要使用上述 github 代码里 docker-1-simple 这个目录下的代码进行编译
1 version: ‘3‘ 2 services: 3 discovery: 4 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 microservice-provider-user: 8 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-provider-user:0.0.1-SNAPSHOT 9 microservice-consumer-movie-ribbon-hystrix: 10 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT 11 microservice-gateway-zuul: 12 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-gateway-zuul:0.0.1-SNAPSHOT 13 microservice-hystrix-dashboard: 14 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-hystrix-dashboard:0.0.1-SNAPSHOT 15 ports: 16 - "8030:8030" 17 microservice-hystrix-turbine: 18 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-hystrix-turbine:0.0.1-SNAPSHOT
运行后,就启动对应的6个Image,以下是运行后效果
使用之前的weave scope工具查看docker, 可以看到5个微服务通过Spring Boot的Eureka连接在一起了。还有一个microservice-hystrix-dashboard这个没有在里面是因为这个是一个仪表盘(Dashboard),不属于实际业务,所以代码里面没有关联起来,实际上是已启动的。
从图中还可以看到,后面的那两个集群etcd和elasticsearch,这两个是之前做练习时创建的。初学者,利用这个weave scope工具来查看和监控,挺好用的。
3.2 基于Eureka高可用(HA)的Compose编写
这里要使用上述 github 代码里 docker-2-eureka-ha 这个目录下的代码进行编译
1 version: "3" 2 services: 3 peer1: # 默认情况下,其他服务可使用服务名称连接到该服务。对于peer2节点,它需连接http://peer1:8761/eureka/,因此,我们可配置该服务的名称为peer1。 4 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 environment: 8 - spring.profiles.active=peer1 9 peer2: 10 image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 11 hostname: peer2 12 ports: 13 - "8762:8762" 14 environment: 15 - spring.profiles.active=peer2 16 17 ## 使用Compose编排高可用的Eureka Server
通过Eureka自带的Dashboard可以看到启动的两个容器已经互相注册,实现高可用(HA)功能
3.3 基于Spring Boot微服务集群及动态伸缩的Compose编写
这里要使用上述 github 代码里 docker-3-complex 这个目录下的代码进行编译,要在每个项目的根目录下,执行 mvn clean package docker:build
1 version: "3" 2 services: 3 peer1: 4 image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 5 ports: 6 - "8761:8761" 7 environment: 8 - spring.profiles.active=peer1 9 peer2: 10 image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT 11 hostname: peer2 12 ports: 13 - "8762:8762" 14 environment: 15 - spring.profiles.active=peer2 16 microservice-provider-user: 17 image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT 18 microservice-consumer-movie-ribbon-hystrix: 19 image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT 20 microservice-gateway-zuul: 21 image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT 22 microservice-hystrix-turbine: 23 image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
通过Eureka的Dashboard查看
仔细查看下图,可以观察到各个Container间箭头的指向,用于理解Spring Boot各个微服务间关系
执行以下命令,为各个微服务动态扩容。让各个微服务都执行3个实例,由于docker-compose版本问题,建议使用 --scale 参数来设置启动实例。
1 docker-compose up --scale microservice-provider-user=3 2 --scale microservice-consumer-movie-ribbon-hystrix=3 3 --scale microservice-gateway-zuul=3 4 --scale microservice-hystrix-turbine=3
Eureka的Dashboard查看
3.4 修改之前博客中的etcd集群配置,改为docker-compose方式启动(自己实践)
1 version: "3" 2 services: 3 etcd01: 4 image: etcd:3.3 5 ports: 6 - "10001:2379" 7 - "10011:2380" 8 #volumes: 9 #- "/root/workspace/docker/k8s/etcd/data11:/data" 10 command: 11 - /bin/etcd 12 - --data-dir 13 - /data 14 - -name 15 - etcd01 16 - -advertise-client-urls 17 - http://etcd01:2379 18 - -listen-client-urls 19 - http://0.0.0.0:2379 20 - -initial-advertise-peer-urls 21 - http://etcd01:2380 22 - -listen-peer-urls 23 - http://0.0.0.0:2380 24 - -initial-cluster-state 25 - new 26 - -initial-cluster-token 27 - docker-etcd 28 - -initial-cluster 29 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380 30 etcd02: 31 image: etcd:3.3 32 ports: 33 - "10002:2379" 34 - "10012:2380" 35 #volumes: 36 #- "/root/workspace/docker/k8s/etcd/data22:/data" 37 command: 38 - /bin/etcd 39 - --data-dir 40 - /data 41 - -name 42 - etcd02 43 - -advertise-client-urls 44 - http://etcd02:2379 45 - -listen-client-urls 46 - http://0.0.0.0:2379 47 - -initial-advertise-peer-urls 48 - http://etcd02:2380 49 - -listen-peer-urls 50 - http://0.0.0.0:2380 51 - -initial-cluster-state 52 - new 53 - -initial-cluster-token 54 - docker-etcd 55 - -initial-cluster 56 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380 57 etcd03: 58 image: etcd:3.3 59 ports: 60 - "10003:2379" 61 - "10013:2380" 62 #volumes: 63 #- "/root/workspace/docker/k8s/etcd/data33:/data" 64 command: 65 - /bin/etcd 66 - --data-dir 67 - /data 68 - -name 69 - etcd03 70 - -advertise-client-urls 71 - http://etcd03:2379 72 - -listen-client-urls 73 - http://0.0.0.0:2379 74 - -initial-advertise-peer-urls 75 - http://etcd03:2380 76 - -listen-peer-urls 77 - http://0.0.0.0:2380 78 - -initial-cluster-state 79 - new 80 - -initial-cluster-token 81 - docker-etcd 82 - -initial-cluster 83 - etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380
测试,实际效果是,对3个节点的增删改查操作都是等效的
curl http://127.0.0.1:10001/v2/keys/hello -XPUT -d value="hello world"
4. 小结
这几天的练习与了解,算是对Docker及Docker Compose有了一定的了解,接下来,就以实际项目中需要用到的服务构建成一个个Docker Image并推送到阿里云容器私有仓库上。初期会构建的有STUN服务、Nginx-RTMP服务、EMQ-MQTT服务、Redis与MongoDB服务,暂时会先构建这几个服务。服务间还涉及到认证与鉴权的,还会写代码,业务代码应该还是用Spring Cloud全家桶,以练促学,未来之路,任重而道远呀!加油!
本文地址: https://www.cnblogs.com/wunaozai/p/9936306.html
物联网架构成长之路(24)-Docker练习之Compose容器编排
标签:语法 code href https dock closed tps 成长之路 关于
原文地址:https://www.cnblogs.com/wunaozai/p/9936306.html