0.前言
一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂。因此学习东西还是要循序渐进,慢慢来。先了解单机编排技术Docker Compose,了解一些技术细节及原理后,在入手K8s。还是不能一口吃成胖子,要多吃几口才可以。而且目前公司都是一些小项目,能用得上DockerCompose已经很不错了,还想要上K8s,估计是不现实的。
1. 安装
可以通过运行下面命令进行安装,
curl -L https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`uname -s`-`uname -m` -o docker-compose
chmod +x docker-compose
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微服务架构实战 第二版》这本书,里面刚好有个例子,这里就用这个做练手。
git clone https://github.com/itmuch/spring-cloud-docker-microservice-book-code-docker/
cd spring-cloud-docker-microservice-book-code-docker
cd docker--simple
mvn clean package docker:build
里面的每个微服务都构建成一个Docker Image,然后就可以开干了。
2. 简单例子
从一个简单的例子开始
Dockerfile 文件
FROM java:latest
VOLUME /tmp
ADD microservice-discovery-eureka-0.0.-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
docker-compose.yml 文件
version: ''
services:
eureka:
build: .
ports:
- "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 这个目录下的代码进行编译
version: '3'
services:
discovery:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka:0.0.1-SNAPSHOT
ports:
- "8761:8761"
microservice-provider-user:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-provider-user:0.0.1-SNAPSHOT
microservice-consumer-movie-ribbon-hystrix:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
microservice-gateway-zuul:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-gateway-zuul:0.0.1-SNAPSHOT
microservice-hystrix-dashboard:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
ports:
- "8030:8030"
microservice-hystrix-turbine:
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 这个目录下的代码进行编译
version: "3"
services:
peer1: # 默认情况下,其他服务可使用服务名称连接到该服务。对于peer2节点,它需连接http://peer1:8761/eureka/,因此,我们可配置该服务的名称为peer1。
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
ports:
- "8761:8761"
environment:
- spring.profiles.active=peer1
peer2:
image: registry.cn-shenzhen.aliyuncs.com/wunaozai/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
hostname: peer2
ports:
- "8762:8762"
environment:
- spring.profiles.active=peer2 ## 使用Compose编排高可用的Eureka Server
通过Eureka自带的Dashboard可以看到启动的两个容器已经互相注册,实现高可用(HA)功能
3.3 基于Spring Boot微服务集群及动态伸缩的Compose编写
这里要使用上述 github 代码里 docker-3-complex 这个目录下的代码进行编译,要在每个项目的根目录下,执行 mvn clean package docker:build
version: "3"
services:
peer1:
image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
ports:
- "8761:8761"
environment:
- spring.profiles.active=peer1
peer2:
image: itmuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
hostname: peer2
ports:
- "8762:8762"
environment:
- spring.profiles.active=peer2
microservice-provider-user:
image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
microservice-consumer-movie-ribbon-hystrix:
image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
microservice-gateway-zuul:
image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
microservice-hystrix-turbine:
image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
通过Eureka的Dashboard查看
仔细查看下图,可以观察到各个Container间箭头的指向,用于理解Spring Boot各个微服务间关系
执行以下命令,为各个微服务动态扩容。让各个微服务都执行3个实例,由于docker-compose版本问题,建议使用 --scale 参数来设置启动实例。
docker-compose up --scale microservice-provider-user=3 \
--scale microservice-consumer-movie-ribbon-hystrix=3 \
--scale microservice-gateway-zuul=3 \
--scale microservice-hystrix-turbine=3
Eureka的Dashboard查看
3.4 修改之前博客中的etcd集群配置,改为docker-compose方式启动(自己实践)
version: "3"
services:
etcd01:
image: etcd:3.3
ports:
- "10001:2379"
- "10011:2380"
#volumes:
#- "/root/workspace/docker/k8s/etcd/data11:/data"
command:
- /bin/etcd
- --data-dir
- /data
- -name
- etcd01
- -advertise-client-urls
- http://etcd01:2379
- -listen-client-urls
- http://0.0.0.0:2379
- -initial-advertise-peer-urls
- http://etcd01:2380
- -listen-peer-urls
- http://0.0.0.0:2380
- -initial-cluster-state
- new
- -initial-cluster-token
- docker-etcd
- -initial-cluster
- etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380
etcd02:
image: etcd:3.3
ports:
- "10002:2379"
- "10012:2380"
#volumes:
#- "/root/workspace/docker/k8s/etcd/data22:/data"
command:
- /bin/etcd
- --data-dir
- /data
- -name
- etcd02
- -advertise-client-urls
- http://etcd02:2379
- -listen-client-urls
- http://0.0.0.0:2379
- -initial-advertise-peer-urls
- http://etcd02:2380
- -listen-peer-urls
- http://0.0.0.0:2380
- -initial-cluster-state
- new
- -initial-cluster-token
- docker-etcd
- -initial-cluster
- etcd01=http://etcd01:2380,etcd02=http://etcd02:2380,etcd03=http://etcd03:2380
etcd03:
image: etcd:3.3
ports:
- "10003:2379"
- "10013:2380"
#volumes:
#- "/root/workspace/docker/k8s/etcd/data33:/data"
command:
- /bin/etcd
- --data-dir
- /data
- -name
- etcd03
- -advertise-client-urls
- http://etcd03:2379
- -listen-client-urls
- http://0.0.0.0:2379
- -initial-advertise-peer-urls
- http://etcd03:2380
- -listen-peer-urls
- http://0.0.0.0:2380
- -initial-cluster-state
- new
- -initial-cluster-token
- docker-etcd
- -initial-cluster
- 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全家桶,以练促学,未来之路,任重而道远呀!加油!