Spring Boot Kubernetes Deploy කරන්න කලිං මේක බලන්න | SC Guide

මචන්ලා, කොහොමද ඉතිං? කට්ටියටම සුවඳම සුවඳ තේ කෝප්පයක් බීලා, අලුත් දෙයක් ඉගෙන ගන්න සෙට් වෙන්න පුළුවන්ද? අද අපි කතා කරන්න යන්නේ ඔයාලාට කම්මැලි නැතුව, ඒත් ගොඩක් වැදගත් වෙන ටොපික් එකක් ගැන – Spring Boot App එකක් Kubernetes එක්ක deploy කරන්නේ කොහොමද කියන එක ගැන. Software Engineering කරන අයට මේ දවස්වල Cloud Native කියන එක නැතුව බෑ නේ. ඔයා පොඩි startup එකක වැඩ කරත්, ලොකු enterprise එකක වැඩ කරත්, මේ Cloud Native concepts දැනගෙන ඉන්න එක අනිවාර්යයි.
ඒ අතරින් Kubernetes කියන්නේ deploy කරන කෙනෙක්ට ජීවිතේ ගලවගන්න පුළුවන් tool එකක්. ඒකට Spring Boot වගේ වේගවත්, lightweight framework එකක් එකතු වුනොත් වැඩේ එල කිව්වොත් එලම තමයි! 🤩 අද අපි මේ දෙක එකට කොහොමද වැඩ කරන්නේ කියලා බලමු. Basic ටික ඉගෙනගෙන, පුංචි app එකක් deploy කරලාම බලමු.
මොකක්ද මේ Kubernetes? 🤔
සරලව කිව්වොත්, Kubernetes කියන්නේ containerized applications automate කරන්න, deploy කරන්න, scale කරන්න, සහ manage කරන්න පුළුවන් open-source platform එකක්. හිතන්න ඔයාට delivery සර්විස් එකක් තියෙනවා කියලා. සාමාන්ය දවස්වල delivery bikes 5ක් ඇති. ඒත් නිවාඩු දවසක (උදා: අවුරුදු කාලේ) demand එක වැඩි වුණොත්, bikes 20ක් විතර ඕනේ වෙන්න පුළුවන්. ඒ වගේම bike එකක් කැඩුනොත්, එතනට ඉක්මනටම වෙන bike එකක් දාන්න ඕනේ. මේ හැමදේම automate කරන boss කෙනෙක් තමයි Kubernetes කියන්නේ. ඒ නිසා තමයි එයාව 'Container Orchestrator' කියලා කියන්නේ.
ඔයාගේ Spring Boot app එක Docker container එකක් ඇතුළේ දාලා, Kubernetes වලට දුන්නම, එයා බලාගන්නවා ඔයාගේ app එක හැමවෙලෙම run වෙනවාද, අවශ්ය තරම් copies තියෙනවාද, traffic එක වැඩි වුනොත් අලුත් copies හදනවාද වගේ දේවල්. මේක හරියට infrastructure එක ගැන ඔයා හිතන එක නවත්තලා, app එකේ business logic එක ගැන විතරක් හිතන්න පුළුවන් වෙනවා වගේ දෙයක්. මාරම පහසුයි නේද? 🚀
Kubernetes වල මූලිකම වාසි ටිකක් කිව්වොත්:
- Service Discovery and Load Balancing: ඔයාගේ app එකේ instances ගොඩක් තියෙනවනම්, එයා auto ඒවාට traffic එක බෙදා දෙනවා.
- Storage Orchestration: අවශ්ය වෙලාවට storage එක automatically mount කරගන්න පුළුවන්.
- Automated Rollouts and Rollbacks: අලුත් version එකක් deploy කරනකොට ඒක හරියට වුනේ නැත්නම්, කලින් version එකට rollback වෙන්න පුළුවන්.
- Automatic Bin Packing: ඔයාගේ resources (CPU, Memory) හොඳටම optimize කරනවා.
- Self-healing: container එකක් crash වුනොත්, එයා auto ඒක restart කරනවා. Node එකක් මැරුනොත්, ඒකේ containers ටික වෙන node එකකට migrate කරනවා.
Spring Boot App එකක් ඇයි Kubernetes වලට? 🤔
දැන් ඔයාට හිතෙන්න පුළුවන්, Spring Boot app එකක් නිකං server එකකට දාලා run කරන්න පුළුවන්නේ, මොකටද මේ Kubernetes කියන වදේ කියලා. ඒත් මචන්ලා, large-scale applications වලදීයි, microservices architecture එකක් එක්ක වැඩ කරනකොටයි, Spring Boot සහ Kubernetes එකට එකතු වුනාම ලැබෙන වාසි මාරමයි.
Spring Boot කියන්නේ standalone, production-ready Spring applications හදන්න පුළුවන් framework එකක්. එයාට embedded Tomcat, Jetty වගේ servers තියෙන නිසා, සාමාන්යයෙන් වෙන application server එකක් ඕනේ වෙන්නේ නෑ. මේක containerization (විශේෂයෙන්ම Docker) වලට මාරම පහසුයි. JAR file එකක් විදිහට පැකේජ් කරලා Docker image එකක් හදාගන්න පුළුවන්. ඒ Docker image එක තමයි Kubernetes වල building block එක.
වාසි ටිකක් බලමු:
- Rapid Development & Deployment: Spring Boot නිසා ඉක්මනට app එක හදන්න පුළුවන්. Kubernetes නිසා deploy කරන එකත් ඉක්මන් වෙනවා.
- Scalability: ඔයාගේ app එකට traffic වැඩි වුනොත්, Kubernetes වලට කියන්න පුළුවන් තව instance හදන්න කියලා. Spring Boot app එක lightweight නිසා ඉක්මනට start වෙනවා.
- Resilience: Spring Boot app එකක instance එකක් crash වුනොත්, Kubernetes ඒක අලුතින් restart කරනවා.
- Resource Efficiency: Kubernetes ඔයාගේ servers වල resources හොඳටම optimize කරනවා.
- DevOps friendly: CI/CD pipelines එක්ක Kubernetes එකතු වුනාම, development එකේ ඉඳන් production එකට යනකල්ම වැඩේ automate කරන්න පුළුවන්.
Kubernetes වල මූලික සංකල්ප (Kubernetes Basics) 💡
Spring Boot app එකක් Kubernetes වලට දාන්න කලින්, Kubernetes වල තියෙන basic concepts ටිකක් අඳුනගෙන ඉමු. මේවා තමයි Kubernetes වලට වැඩ කරන්න පුළුවන් කරන මූලිකම ගොඩනැගිලි කොටස්.
- Pod: මූලිකම ඒකකය. Pod එකක් කියන්නේ එකක් හෝ කීපයක් containers අඩංගු කුඩාම, deploy කළ හැකි unit එක. සාමාන්යයෙන් ඔයාගේ Spring Boot app container එක Pod එකක් ඇතුළේ තමයි run වෙන්නේ. හැම Pod එකකටම තමන්ටම ආවේනික IP address එකක් තියෙනවා. හිතන්න container එකක් තනියම දුවන්න පුළුවන් bike එකක් කියලා. Pod එක කියන්නේ ඒ bike එක නවත්තන පොඩි ගරාජ් එකක් වගේ.
- Deployment: Pod එක manage කරන විදිය කියන එක තමයි Deployment එකක් කියන්නේ. මේකෙන් පුළුවන් Pod එකක replicas (copies) කීයක් run වෙන්න ඕනෙද, අලුත් version එකකට update කරන්නේ කොහොමද, rollback කරන්නේ කොහොමද වගේ දේවල් define කරන්න. අපිට Spring Boot app එකේ instances 2ක් ඕනේ නම්, Deployment එකක් හදලා
replicas: 2
කියලා දාන්න පුළුවන්. - Service: Deployment එකෙන් run කරන Pods වලට external එකෙන් (නැත්නම් internal එකෙන්) access කරන විදිය තමයි Service එකකින් කියන්නේ. Pods වල IP addresses වෙනස් වෙන්න පුළුවන් නිසා, Service එකක් ස්ථාවර IP address එකක් සහ DNS name එකක් ලබා දෙනවා. හිතන්න, delivery bikes ගරාජ් එකේ තියෙනවා. ඒත් ඒ ගරාජ් එකට එළියෙන් order එකක් දෙන්න බෑ. ඒ orders ගන්න office එකක් ඕනේ. ඒ office එක තමයි Service එක. ඒ office එකේ address එක ස්ථාවරයි. Pods කීයක් හිටියත්, Service එකෙන් ඒ හැමෝටම traffic එක load balance කරලා දෙනවා.
- Namespace: ලොකු Kubernetes cluster එකක විවිධ projects, teams වලට අදාළ resources වෙන් කරලා, organize කරලා තියාගන්න Namespace පාවිච්චි කරනවා. Development, Staging, Production වගේ environments වෙන වෙනම Namespace වල හදන්න පුළුවන්.
මේවා තමයි Kubernetes වල කේන්ද්රීය concepts. දැන් අපි බලමු මේවා ප්රායෝගිකව කොහොමද වැඩ කරන්නේ කියලා.
Spring Boot App එකක් Kubernetes වල Deploy කරමු (ප්රායෝගික උදාහරණයක්) 🛠️
හරි, දැන් තමයි වැඩේට බහින්න වෙලාව! අපි පුංචිම Spring Boot REST API එකක් හදලා, ඒක Docker image එකක් කරලා, අන්තිමට Kubernetes වලට deploy කරමු.
අවශ්ය දේවල් (Prerequisites):
- Java Development Kit (JDK) 17+
- Maven හෝ Gradle
- Docker Desktop (Windows/macOS) හෝ Docker Engine (Linux)
- Minikube (පොඩි Kubernetes cluster එකක් ඔයාගේ local machine එකේ run කරන්න) හෝ Kind. Minikube install කරගන්න විදිය Google කරලා බලාගන්න පුළුවන්.
- kubectl (Kubernetes command-line tool). මෙයත් Minikube install කරනකොටම install වෙන්න පුළුවන්.
මුලින්ම minikube start
කියලා command එක දීලා ඔයාගේ local Kubernetes cluster එක start කරගන්න. 💡
1. සරල Spring Boot App එකක් හදමු ☕
මුලින්ම Spring Initializr (start.spring.io) පාවිච්චි කරලා Maven project එකක් හදාගන්න. Dependencies විදිහට Spring Web
එක add කරන්න. Project එක download කරගෙන IDE එකක open කරගන්න.
src/main/java/com/example/demo/HelloController.java
කියන file එක හදලා මේ code එක දාන්න:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "Hello from Spring Boot on Kubernetes! කෝපි එකක් බොමුද? ☕";
}
}
මේක Spring Boot app එකේ root path එකට (/
) HTTP GET request එකක් ආවොත්, simple String message එකක් return කරන controller එකක්.
2. Dockerfile එක ලියමු 🐳
දැන් අපේ Spring Boot app එක Docker container එකක් ඇතුළට දාන්න Dockerfile එකක් ලියමු. Project root එකේ Dockerfile
නමින් file එකක් හදලා මේ content එක දාන්න:
# Dockerfile
FROM openjdk:17-jdk-slim
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
EXPOSE 8080
විස්තරය:
FROM openjdk:17-jdk-slim
: JDK 17 සහිත lightweight Linux base image එකක් use කරනවා.VOLUME /tmp
: Spring Boot වලට temporary files හදන්න/tmp
directory එකක් හදනවා.COPY target/*.jar app.jar
: අපේ Maven build එකෙන් හැදෙන JAR file එක container එක ඇතුළේapp.jar
නමින් copy කරනවා.ENTRYPOINT ["java","-jar","/app.jar"]
: container එක start කරනකොට අපේ Spring Boot app එක run කරන්න command එක දෙනවා.EXPOSE 8080
: app එක 8080 port එකෙන් run වෙනවා කියලා Docker වලට කියනවා.
3. Docker Image එක Build කරමු 🏗️
මුලින්ම Maven පාවිච්චි කරලා Spring Boot app එක build කරලා JAR file එක හදාගන්න:
./mvnw clean package
දැන් Docker image එක build කරන්න. ඔයා minikube cluster එකේ Docker daemon එක පාවිච්චි කරනවා නම්, මේ command එක run කරන්න (මේකෙන් image එක Minikube එක ඇතුළේම build වෙනවා):
eval $(minikube docker-env)
ඊට පස්සේ මේ command එකෙන් image එක build කරන්න:
docker build -t spring-k8s-demo:1.0.0 .
spring-k8s-demo:1.0.0
කියන්නේ අපේ image name එකයි tag එකයි. .
කියන්නේ Dockerfile එක තියෙන directory එක.
4. Kubernetes Manifest Files හදමු (YAML files) 📝
දැන් අපේ app එක Kubernetes වල deploy කරන්න අවශ්ය YAML files හදමු. මේවා Kubernetes වලට කියනවා අපේ app එක deploy කරන්න ඕනේ කොහොමද කියලා.
a. Deployment.yaml
Deployment එකක් හදමු deployment.yaml
කියලා file එකක් project root එකේ හදලා මේ content එක දාන්න:
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-k8s-demo-deployment
labels:
app: spring-k8s-demo
spec:
replicas: 2 # අපේ Spring Boot app instances 2ක් run කරමු
selector:
matchLabels:
app: spring-k8s-demo
template:
metadata:
labels:
app: spring-k8s-demo
spec:
containers:
- name: spring-k8s-demo-container
image: spring-k8s-demo:1.0.0 # අපේ Docker image එකේ name එකයි tag එකයි
ports:
- containerPort: 8080 # Spring Boot app එකේ default port එක
imagePullPolicy: Never # Minikube local image එකක් නිසා 'Never' දානවා. Production වල 'IfNotPresent' හෝ 'Always'
විස්තරය:
kind: Deployment
: මේක Deployment object එකක් කියලා කියනවා.metadata.name
: Deployment එකට නමක් දෙනවා.spec.replicas: 2
: අපේ app එකේ instances දෙකක් හැමවෙලෙම run වෙන්න ඕනේ කියලා කියනවා.spec.selector.matchLabels
: මේ Deployment එකෙන් manage කරන්න ඕනේ Pods මොනවද කියලා මේ labels වලින් කියනවා.template.spec.containers.image
: අපි කලින් build කරපු Docker image එක මෙතන දෙනවා.containerPort: 8080
: අපේ Spring Boot app එක container එක ඇතුළේ 8080 port එකෙන් listen කරනවා කියලා කියනවා.
b. Service.yaml
දැන් අපේ Deployment එකෙන් හැදෙන Pods වලට access කරන්න Service එකක් හදමු. service.yaml
කියලා file එකක් project root එකේ හදලා මේ content එක දාන්න:
apiVersion: v1
kind: Service
metadata:
name: spring-k8s-demo-service
spec:
selector:
app: spring-k8s-demo # මේ Service එකට connect කරන්න ඕනේ Pods වල labels
ports:
- protocol: TCP
port: 80 # Service එකේ port එක (external access සඳහා)
targetPort: 8080 # Pod එකේ app එක run වෙන port එක
type: LoadBalancer # External access දෙන්න LoadBalancer type එක use කරනවා
විස්තරය:
kind: Service
: මේක Service object එකක් කියලා කියනවා.metadata.name
: Service එකට නමක් දෙනවා.spec.selector.app: spring-k8s-demo
: මේ Service එකෙන් traffic යවන්න ඕනේapp: spring-k8s-demo
label එක තියෙන Pods වලට කියලා කියනවා.ports.port: 80
: Service එක external එකෙන් 80 port එකෙන් listen කරනවා.ports.targetPort: 8080
: Service එකට එන request, Pods වල 8080 port එකට redirect කරනවා.type: LoadBalancer
: Service එක Kubernetes cluster එකෙන් පිට (external) IP address එකකින් expose කරන්න මේ type එක පාවිච්චි කරනවා. Minikube වලට මේකෙන් NodePort එකක් වගේ හැසිරෙන්න පුළුවන්.
5. Kubernetes Cluster එකට Deploy කරමු! 🎉
දැන් අපේ YAML files පාවිච්චි කරලා අපේ app එක Kubernetes cluster එකට deploy කරමු. Terminal එකේ මේ commands දෙක run කරන්න:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
මේ commands වලින් Kubernetes වලට කියනවා deployment.yaml
සහ service.yaml
files වල තියෙන configurations apply කරන්න කියලා.
6. Deployment එක Verify කරමු සහ App එක Access කරමු ✅
අපේ Deployment එක හරියට වුනාද කියලා බලන්න commands ටිකක් run කරමු:
kubectl get deployments
# Output එක මේ වගේ වෙයි:
# NAME READY UP-TO-DATE AVAILABLE AGE
# spring-k8s-demo-deployment 2/2 2 2 xxs
kubectl get pods
# Output එක මේ වගේ වෙයි (Pods දෙකක් run වෙන්න ඕනේ):
# NAME READY STATUS RESTARTS AGE
# spring-k8s-demo-deployment-xxxxxxxxx-xxxxx 1/1 Running 0 xxs
# spring-k8s-demo-deployment-xxxxxxxxx-xxxxx 1/1 Running 0 xxs
kubectl get services
# Output එක මේ වගේ වෙයි (Service එකක් LoadBalancer type එකෙන්):
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# spring-k8s-demo-service LoadBalancer 10.xx.xx.xx <pending> 80:3xxxx/TCP xxs
අන්තිමට, අපේ Spring Boot app එක access කරලා බලමු. Minikube වල LoadBalancer Service එකට external IP එකක් ලැබෙන්නේ නැති නිසා, Minikube වලම Service URL එක ගන්න මේ command එක run කරන්න:
minikube service spring-k8s-demo-service
මේ command එක run කලහම ඔයාගේ browser එකේ අලුත් tab එකක් open වෙලා Hello from Spring Boot on Kubernetes! කෝපි එකක් බොමුද? ☕
කියලා message එක පෙනෙන්න ඕනේ. එහෙම පෙනුනොත් ඔයා වැඩේ ගොඩ. 🥳
ඉදිරියට යමු: වැඩිදියුණු කිරීම් සහ තවද දේවල් 🌟
අද අපි කරපු දේ Kubernetes වල Deployment එකකයි, Service එකකයි basicම විදිය. ඒත් Kubernetes කියන්නේ මීට වඩා ගොඩක් දේවල් කරන්න පුළුවන් powerful tool එකක්.
ඉදිරියට ඉගෙනගන්න පුළුවන් දේවල් ටිකක් මෙන්න:
- Ingress: External HTTP/HTTPS access manage කරන්න. එක IP address එකකින් services කීපයකට route කරන්න පුළුවන්.
- ConfigMaps and Secrets: Configuration data සහ sensitive data (passwords, API keys) securely store කරලා applications වලට inject කරන හැටි.
- Persistent Volumes: Database වගේ stateful applications වලට data persistent කරන්න volumes manage කරන හැටි.
- Helm: Kubernetes applications package කරලා deploy කරන්න පුළුවන් package manager එකක්.
- CI/CD with Kubernetes: Jenkins, GitLab CI, GitHub Actions වගේ tools එක්ක Kubernetes deploy කරන විදිය automate කරන හැටි.
- Monitoring and Logging: Prometheus, Grafana, ELK Stack වගේ tools පාවිච්චි කරලා Kubernetes applications monitor කරන හැටි.
අවසානයට 💫
ඉතිං මචන්ලා, අද අපි Spring Boot සහ Kubernetes කියන මේ powerful combo එක ගැන ගොඩක් දේවල් ඉගෙනගත්තා. Kubernetes වල basics ටික දැනගෙන, අපේ Spring Boot app එක containerize කරලා, deploy කරන්නේ කොහොමද කියලා ප්රායෝගිකවම බැලුවා. මේක ඔයාලගේ Software Engineering journey එකේ තවත් එක big step එකක් කියලා මම හිතනවා.
Cloud Native development වලට යනකොට Kubernetes අනිවාර්යයෙන්ම දැනගෙන ඉන්න ඕනේ skill එකක්. ඒක ඔයාගේ applications reliable විදිහට, scalable විදිහට run කරන්න ගොඩක් උදව් වෙනවා. ඒකට Spring Boot වල වේගවත් බවත්, ease of development එකත් එකතු වුනාම, development process එක මාරම පහසු වෙනවා.
මේ guide එක ඔයාලට වැදගත් වෙන්න ඇති කියලා හිතනවා. ඔයාලා මේක try කරලා බැලුවද? මොනවා හරි ප්රශ්න තියෙනවාද? ඔයාගේ අදහස්, ප්රශ්න පහළ comments section එකේ දාන්න. අපි කතා කරමු! 💬
එහෙනම්, තවත් අලුත් article එකකින් හමුවෙමු! Happy Coding! 👋