Developing Spring Boot Web Services on Kubernetes with Google Skaffold

Sirinat Paphatsirinatthi
Nov 6 · 3 min read

เนื่องจากเวลาจะ Deploy Spring Boot Web Services ขึ้น Kubernetes บน Production environment นั้น เราต้อง prove ให้แน่ใจก่อนว่า Service ของเรา run smoothly on Kubernetes เลยเป็นที่มาของการติดตั้ง Google Skaffold (ขอเรียกสั้นๆว่า Skaffold). Skaffold จะเป็นตัวช่วยให้เรา Integrate Spring Boot กับ Local Kubernetes Cluster (Minikube / MicroK8s) เพื่อให้สามารถทดสอบบน local ได้ง่ายยิ่งขึ้น โดยจัดการ Process การ build Docker Image, Publish Docker Image, Deploy Docker Image to Kubernetes ผ่าน Skaffold pipeline (Skaffold.yaml)

โดยวิธี Setup Skaffold กับ Spring Boot Project สามารถทำได้ง่ายๆ ผ่าน Intellij IDE ดังนี้

สิ่งที่ต้องลงก่อน

  1. ทำการติดตั้ง Skaffold (ในตัวอย่างจะใช้ Mac OS)
$ brew install skaffold

2. ทำการสร้าง Spring Boot Project ผ่าน Spring Boot Initializer

เลือก Dependencies ดังนี้

จากนั้นกด Generate

3. Import Project To IntelliJ

เปิด IntelliJ -> Click Import Project

Browse ไปที่ Project ที่พึ่ง Generated จาก Spring Boot Initializer -> หา pom.xml -> Open

เลือก Import Maven projects automatically จากนั้นกด Next

กด Next

กด Next

กด Finish

4. ติดตั้ง Plugin IntelliJ Cloud Code

ไปที่ Intellij Preferences

เลือก Plugins -> ค้นหา Cloud Code -> กด Install

5. Add Maven Jib Plugin to Project pom.xml

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.7.0</version>
</plugin>

6. สร้างไฟล์ Skaffold.yaml ใน Project โดยมีเนื้อหาดังนี้

apiVersion: skaffold/v1beta15
kind: Config
build:
artifacts:
- image: gcr.io/demo-skaffold/spring-boot # (image ที่จะ deploy)
jib: {}

deploy:
kubectl:
manifests:
- deployment.yml # (Kubernetes Deployment File)

7. สร้าง deployment.yml ใน project

Open Intellij Terminal -> Run

$ kubectl run demo-skaffold --image gcr.io/demo-skaffold/spring-boot --dry-run -o yaml > deployment.yml

8. Add Cloud Code Configuration เพื่อ Deploy Spring Boot to Local Kubernetes Cluster

Click Add Configuration -> กด +

เลือก Cloud Code: Kubernetes -> Kubernetes Continuous Deploy

ตั้งชื่อว่า “demo - Dev” -> กด OK

9. กดลูกศรเขียว

จะเห็นว่า Skaffold ทำการ Build Docker Image ให้เราเลย ผ่าน Maven Jib Plugin ที่เราทำการ Add ใส่ pom.xml ไว้ในตอนแรก

หลังจากที่ Build Docker Image เสร็จเรียบร้อยแล้ว, Skaffold ก็ทำการ deploy Docker Image to Local Kubernetes Cluster ให้เราพร้อม Test เลย !! แจ่มไหมล่ะ ?

Sirinat Paphatsirinatthi

Written by

Technology Evangelist @ Digital Ventures

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade