learning/k8s-practice/ocp/build.md
本文假设您已经完成了 准备OCP的构建环境和部署环境,在该文档的最后,我们将 Open Capacity Platform 的代码仓库克隆到了 master 节点的 /root/open-capacity-platform。
::: tip
ocpsample)ocpsample
:::修改 /root/open-capacity-platform/pom.xml 文件,修改其中的字段:
project --> properties --> docker.host 修改为 unix:///var/run/docker.sock
project --> properties --> docker.image.prefix 修改为 ocpsample (此处使用你在 https://hub.docker.com 的账号)
::: tip
如果您使用自己的 docker 镜像仓库,您的 docker.image.prefix 要复杂一些,请参考 使用私有仓库中的docker镜像。具体而言,前缀应该由您的docker镜像仓库的多个参数组成,例如:
<font color="blue" weight="500">my-registry.example.com</font>:<font color="green" weight="500">5000</font>/<font color="purple" weight="500">example</font>
:::
<properties>
<java.version>1.8</java.version>
<core.version>2.0.1</core.version>
<log4j2.version>2.1</log4j2.version>
<jasypt.version>1.14</jasypt.version>
<hutool.version>4.1.13</hutool.version>
<fastjson.version>1.2.60</fastjson.version>
<disruptor.version>3.4.1</disruptor.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<docker.host>unix:///var/run/docker.sock</docker.host>
<docker.image.prefix>ocpsample</docker.image.prefix>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<net-devh-grpc.version>2.0.1.RELEASE</net-devh-grpc.version>
<spring-platform.version>Cairo-SR3</spring-platform.version>
<spring.social.version>1.1.6.RELEASE</spring.social.version>
<spring-security.version>5.1.1.RELEASE</spring-security.version>
<security-oauth2.version>2.3.4.RELEASE</security-oauth2.version>
<commons-collections4.version>4.1</commons-collections4.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-validator.verion>5.0.2.Final</hibernate-validator.verion>
<flowable.version>6.4.1</flowable.version>
<spring-cloud-dependencies.version>Finchley.SR2</spring-cloud-dependencies.version>
</properties>
在 master 节点执行 cd /root/open-capacity-platform
在 master 节点执行 mvn clean package -DskipTests,输出结果如下所示
[INFO] zipkin-center ...................................... SUCCESS [ 0.009 s]
[INFO] zipkin-center-es ................................... SUCCESS [ 0.003 s]
[INFO] es-server .......................................... SUCCESS [ 1.156 s]
[INFO] es-client .......................................... SUCCESS [ 0.515 s]
[INFO] new-api-gateway .................................... SUCCESS [ 1.956 s]
[INFO] web-portal ......................................... SUCCESS [ 0.004 s]
[INFO] back-center ........................................ SUCCESS [ 1.735 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:09 min
[INFO] Finished at: 2019-09-24T13:23:44+08:00
[INFO] ------------------------------------------------------------------------
::: tip
mvn clean package -DskipTests 时,由于需要从 maven 中央仓库拉取 OCP 所依赖的 jar 包以及 maven 所需要的 plugin 等资源,耗时比较长,作者第一次执行该命令时等候了 32 分钟。./target/ 目录下,例如 eureka-server 子模块的jar包位于路径 /root/open-capacity-platform/register-center/eureka-server/target/eureka-server.jar
build.finalName 字段指定。例如 /root/open-capacity-platform/register-center/eureka-server/pom.xml 中 build.finalName 字段为 ${project.artifactId},该变量的取值为 eureka-server,因此jar文件的名字为 eureka-server.jar./target/ 目录下找到
:::cd /root/open-capacity-platform/register-center/eureka-servermvn docker:build 输出结果如下所示:ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 2fa9cf75f7ba
Successfully tagged ocpsample/eureka-server:latest
[INFO] Built ocpsample/eureka-server
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.900 s
[INFO] Finished at: 2019-09-24T13:45:45+08:00
[INFO] ------------------------------------------------------------------------
OCP中由于全是 java 项目,因此使用了 docker 的 maven 插件 com.spotify.docker-maven-plugin 进行 docker 镜像的构建
build.plugins 字段/root/open-capacity-platform/pom.xml 文件中,即我们在 修改配置 章节提前修改好的属性 docker.image.prefix 和 docker.host
docker-maven-plugin在 maven 环境中给我们带来了很大的便利,您仍然有必要熟悉docker build命令,以便您能够轻松地处理 php/nodejs/python 等类型的项目
<!-- 首先加入pom ${docker.image.prefix} : 这个是你的dockerhub注册上面的名字 gitgeek 这个是我注册的
${project.artifactId} : 项目的名称 dockerDirectory : dockerfile的文件路径 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<!-- docker远程服务器地址 -->
<dockerHost>${docker.host}</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
执行命令 docker login 输入您在 hub.docker.com 的用户名密码,输出结果如下所示
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: ocpsample
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
执行命令 docker push ocpsample/eureka-server:latest
如果推送成功,将显示
The push refers to repository [docker.io/ocpsample/eureka-server]
daf5b9e5336a: Layer already exists
b59179c58f56: Layer already exists
ceaf9e1ebef5: Layer already exists
9b9b7f3d56a0: Layer already exists
f1b5933fe4b5: Layer already exists
latest: digest: sha256:7fb0f1b566e9d9183c65f2ceed3740d210072b2317523b6399ef2745fe87b367 size: 1371
推送速度取决于您与 hub.docker.com 之间的网络连接速度,时间有点儿长,您也可以直接使用本教程已经推送上去的镜像,直接进入在 Kubernetes 上部署的环节
过程与 eureka-server 相同,因此只写命令,不再写过程
# 切换到目录
cd /root/open-capacity-platform/oauth-center/auth-server
# docker build
mvn docker:build
# docker push
docker push ocpsample/auth-server:latest
# 切换到目录
cd /root/open-capacity-platform/business-center/user-center
# docker build
mvn docker:build
# docker push
docker push ocpsample/user-center:latest
# 切换到目录
cd /root/open-capacity-platform/api-gateway
# docker build
mvn docker:build
# docker push
docker push ocpsample/api-gateway:latest
# 切换到目录
cd /root/open-capacity-platform/web-portal/back-center
# docker build
mvn docker:build
# docker push
docker push ocpsample/back-center:latest
使用 ocpsample 的用户名和密码登录 https://hub.docker.com 的 Repositories 菜单下可查看推送上去的镜像,如下图所示:
::: tip
Open Capacity Platform 中,使用一个三层结构在组织java项目:
这种代码组织结构的优点:
mvn clean build 就可以完成所有项目的构建缺点
相对于类似 OCP 的多层结构,还可以用扁平的结构组织 java 项目,即每一个模块一个 gitee(github) 项目,如果这样,OCP 的项目结构将如下所示:
ocp-root 用于存放原 open-capacity-platform/pom.xml 文件ocp-$(category)-root 用于存放原 open-capacity-platform/register-center/pom.xml 等第二层 【模块分类】 的 pom.xmlocp-$(category)-$(modules) 用于存放原 open-capacity-platform/register-center/eureka-server 等第三层 【子模块代码】 的项目文件这种代码结构的优点:
缺点
::: tip 如何选择
不同的结构,没有最好,只有最合适:
:::