使用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 查看效果。