使用google-jib部署springboot应用到阿里云Serverless kubernetes
google-jib是什么
Jib 是一个快速docker镜像构建工具,它可以配置在maven插件中,不需要编写 Dockerfile 或本机安装docker程序。它只将发生变更的层推送到容器镜像服务器大大节约了构建时间。
阿里云Serverless kubernetes是什么
请看我的上篇博客。优点是节约了搭建和管理kubernetes的时间和精力。
实战
拉取已写好的springboot程序源码
https://github.com/ouyangshixiong/jib-demo.git
注意pom.xml文件
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Jib -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>${jib-maven-plugin.version}</version>
<configuration>
<from>
<image>registry.cn-shenzhen.aliyuncs.com/datangliang/java:11</image>
</from>
<to>
<image>registry.cn-shenzhen.aliyuncs.com/datangliang/jib-demo:v1</image>
</to>
<allowInsecureRegistries>true</allowInsecureRegistries>
<container>
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
<jvmFlag>-Xdebug</jvmFlag>
</jvmFlags>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
编译成docker并push到阿里云容器镜像服务器上
阿里云容器镜像服务器需要和Serverless kubernetes同区。
maven的settings.xml中需要给出容器镜像服务器的id,账户和密码
<server>
<id>registry.cn-shenzhen.aliyuncs.com</id>
<username>xxxxxxx</username>
<password>xxxxx</password>
</server>
如下图在Intellij中配置编译命令:
编译成功:
本地docker测试
创建kubernetes集群
并设定好vpc,路由等等。
使用镜像创建无状态的应用
- 选择用户镜像jib-demo:v1,创建相应的pod。(如上图)
- 创建LoadBalance服务。(这里需要购买SLB,在上篇文字中已经讲过。)
- 配置端口映射8080->80
- 打开 http://SLB_IP/hello 查看效果。