Spring Boot Knative Deployment | Serverless Kubernetes Sinhala Tutorial

Spring Boot Knative Deployment | Serverless Kubernetes Sinhala Tutorial

ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ මේ දවස්වල Software Engineering ලෝකයේ ගොඩක් දෙනෙක් අතර ජනප්‍රිය වෙලා තියෙන Serverless කියන concept එක ගැන සහ ඒක Knative එක්ක එකතු වෙලා Spring Boot application එකක් Deploy කරන්නේ කොහොමද කියන එක ගැනයි. Microservices, Cloud Native development කියන දේවල් එක්ක වැඩ කරනවා නම්, Knative කියන්නේ ඔයාලා අනිවාර්යයෙන්ම දැනගෙන ඉන්න ඕන දෙයක්.

සාමාන්‍යයෙන් application එකක් deploy කරනකොට අපිට servers maintain කරන්න, scaling ගැන හිතන්න ගොඩක් දේවල් තියෙනවා. ඒත් serverless වලදී මේ වගකීම් cloud provider එකට බාර දෙන නිසා developer කෙනෙක් විදියට අපිට code කරන එක විතරක් බැලුවම ඇති. Knative කියන්නේ Kubernetes cluster එකක් serverless platform එකක් විදියට පාවිච්චි කරන්න අපිට උදව් කරන open-source solution එකක්. Spring Boot application එකක් Knative එක්ක deploy කරලා, ඒක කොච්චර පහසුවෙන් scale වෙනවද, maintain කරන්න පුළුවන්ද කියලා අද අපි බලමු.

මේ guide එක අවසානයේදී ඔයාලට පුළුවන් වෙයි තමන්ගේම Spring Boot application එකක් Knative Service එකක් විදියට Kubernetes මත successfuly deploy කරන්න. එහෙනම් වැඩේට බහිමුද?

Knative කියන්නේ මොකක්ද? (What is Knative?)

Knative කියන්නේ Google එකෙන් develop කරපු Kubernetes-based platform එකක්. මේකේ ප්‍රධාන components දෙකක් තියෙනවා:

  1. Knative Serving: Serverless applications deploy කරන්න, run කරන්න, scale කරන්න සහ manage කරන්න මේක උදව් වෙනවා. Requests නැති වෙලාවට automatically 0 දක්වා scale වෙන එක (scale-to-zero), request වැඩි වෙනකොට automatically scale up වෙන එක (auto-scaling), traffic splitting, revision management වගේ ගොඩක් දේවල් Knative Serving වලින් handle කරනවා.
  2. Knative Eventing: Event-driven architectures develop කරන්න මේක උදව් වෙනවා. විවිධ sources වලින් එන events (उदा. CloudEvents) consume කරලා, ඒවා applications වලට යවන්න මේකෙන් පුළුවන්.

සරලවම කිව්වොත්, Knative කියන්නේ Kubernetes මත serverless experience එකක් දෙන්න හදපු layer එකක්. ඒකෙන් අපිට infrastructure ගැන හිතන්නේ නැතුව අපේ code එක develop කරන එකට විතරක් අවධානය දෙන්න පුළුවන්.

Spring Boot Application එකක් සූදානම් කරගනිමු (Preparing a Spring Boot Application)

අපි Knative එක්ක deploy කරන්න යන Spring Boot application එකක් සූදානම් කරගන්න ඕනේ. මේකට අපිට පොඩි RESTful API එකක් තියෙන සරල Spring Boot project එකක් හදාගමු. Spring Initializr (start.spring.io) පාවිච්චි කරලා ඉක්මනට project එකක් හදාගන්න පුළුවන්. Dependencies විදියට 'Spring Web' එකතු කරගන්න.

මෙන්න මේ වගේ Controller එකක් තියෙනවා කියලා හිතමු:

package com.example.knativedemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class KnativeDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(KnativeDemoApplication.class, args);
    }

    @GetMapping("/hello")
    public String helloKnative() {
        return "ආයුබෝවන් Knative! Spring Boot application එක වැඩ!";
    }
}

මේක සාමාන්‍ය Spring Boot application එකක්. මේක අපිට JAR file එකක් විදියට build කරගන්න පුළුවන්. Maven පාවිච්චි කරනවා නම්, command prompt එකේ / terminal එකේ මේ විදියට build කරන්න පුළුවන්:

./mvnw clean package

දැන් target folder එක ඇතුළේ knative-demo-0.0.1-SNAPSHOT.jar වගේ JAR file එකක් හැදිලා ඇති. මේක තමයි අපි Knative Service එකක් විදියට deploy කරන්න හදන්නේ.

Dockerize කිරීම (Dockerizing the Application)

Knative service එකක් විදියට deploy කරන්න කලින්, අපේ Spring Boot application එක Docker image එකක් විදියට හදන්න ඕනේ. Docker images කියන්නේ applications run කරන්න අවශ්‍ය සියලු දේවල් (code, runtime, libraries, dependencies) එකට තියෙන lightweight, standalone, executable packages.

අපේ project root එකේ Dockerfile කියලා file එකක් හදලා මේ content එක paste කරගන්න:

# Use an official OpenJDK runtime as a parent image
FROM openjdk:17-jdk-slim

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
EXPOSE 8080

# The application's JAR file
ARG JAR_FILE=target/knative-demo-0.0.1-SNAPSHOT.jar

# Add the application's JAR to the container
ADD ${JAR_FILE} app.jar

# Run the JAR file
ENTRYPOINT ["java","-jar","/app.jar"]

මේ Dockerfile එකෙන් කියන්නේ:

  • openjdk:17-jdk-slim කියන base image එක පාවිච්චි කරන්න.
  • /tmp කියන folder එක mount කරන්න.
  • 8080 port එක expose කරන්න. (Spring Boot default port එක).
  • අපේ JAR_FILE එක app.jar විදියට container එකට add කරන්න.
  • අන්තිමට java -jar /app.jar command එකෙන් application එක run කරන්න.

දැන් මේ Dockerfile එක පාවිච්චි කරලා Docker image එක build කරමු. ඔයාලගේ DockerHub username එක your-dockerhub-username කියලා හිතමු.

docker build -t your-dockerhub-username/knative-demo:latest .

Image එක build උනාට පස්සේ, ඒක DockerHub එකට push කරන්න ඕනේ. මොකද Knative වලට Kubernetes cluster එකේ තියෙන image registry එකකින් image එක pull කරගන්න අවශ්‍ය වෙනවා.

docker push your-dockerhub-username/knative-demo:latest

මේකෙන් ඔයාලගේ application එක DockerHub එකට successfuly upload වෙයි. private registry එකක් නම්, ඒකට අදාල credentials Kubernetes වලට add කරන්න වෙනවා. මේ tutorial එකට අපි public registry එකක් පාවිච්චි කරමු.

Knative Service එකක් Deploy කරමු (Deploying a Knative Service)

Knative Service එකක් deploy කරන්න කලින්, ඔයාලට Kubernetes cluster එකක් සහ ඒකේ Knative Serving components install කරලා තියෙන්න ඕනේ. ඒ ගැන information Knative official documentation එකේ තියෙනවා (knative.dev). අපි හිතමු ඒ ඔක්කොම හරි කියලා.

දැන් අපි knative-service.yaml කියලා file එකක් හදලා අපේ Knative Service definition එක ලියමු:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: knative-spring-demo
spec:
  template:
    spec:
      containers:
        - image: your-dockerhub-username/knative-demo:latest
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "Knative"

මේ YAML file එකේ තියෙන වැදගත් කොටස් බලමු:

  • apiVersion: serving.knative.dev/v1: මේක Knative Serving API version එක.
  • kind: Service: අපි deploy කරන්නේ Knative Service එකක් කියලා මේකෙන් කියනවා. Knative Service එකක් කියන්නේ Kubernetes Deployment, Service සහ Ingress වගේ දේවල් simplify කරන abstract එකක්.
  • metadata.name: knative-spring-demo: අපේ service එකට දෙන නම.
  • spec.template.spec.containers: මේකේ තමයි අපේ Docker image එකේ details දෙන්නේ.
    • image: your-dockerhub-username/knative-demo:latest: අපි කලින් build කරපු Docker image එකේ නම. මේක ඔයාලගේ DockerHub username එකට හරවන්න අමතක කරන්න එපා!
    • ports: - containerPort: 8080: application එක listen කරන port එක.
    • env: - name: TARGET value: "Knative": අපි application එකට environment variable එකක් add කරලා තියෙනවා. (මේක අපේ Spring Boot code එකේ helloKnative() method එකේදී පාවිච්චි කරන්න පුළුවන්, මේ example එකට අපි ඒක පාවිච්චි කරලා නෑ. ඒත් application එකට config send කරන්න පුළුවන් කියලා පෙන්නන්න දැම්මා.)

දැන් මේ service එක deploy කරමු. Command prompt එකේ මේ command එක run කරන්න:

kubectl apply -f knative-service.yaml

Deploy වුනාට පස්සේ, Knative විසින් Revision, Configuration, Route වගේ Kubernetes resources ටිකක් automatically හදනවා.

අපේ Knative Service එකේ status බලන්න මේ command එක පාවිච්චි කරන්න පුළුවන්:

kn service describe knative-spring-demo

ඔයාලට මේ වගේ output එකක් පෙනෙයි (Output එකේ URLs වෙනස් වෙන්න පුළුවන්):


Name:        knative-spring-demo
Namespace:   default
URL:         http://knative-spring-demo.default.example.com

Revisions:
  knative-spring-demo-00001 (ACTIVE) @latest
    traffic:  100%

Conditions:
  OK TYPE                 AGE REASON
  ++ Ready                 5m
  ++ ConfigurationsReady   5m
  ++ RoutesReady           5m

උඩ තියෙන URL එක තමයි අපේ Knative Service එක access කරන්න තියෙන URL එක. අපි දැන් ඒක test කරමු. curl command එක පාවිච්චි කරලා request එකක් යවන්න පුළුවන්:

curl -H "Host: knative-spring-demo.default.example.com" http://<Ingress-IP>/hello

මෙහිදී <Ingress-IP> කියන්නේ ඔයාලගේ Kubernetes cluster එකේ Ingress Gateway එකේ IP address එක. ඒක Knative install කරනකොට setup වෙනවා. සාමාන්‍යයෙන් Kourier හෝ Istio වගේ Ingress controller එකක් Knative එක්ක එනවා. ඔයාලගේ setup එක අනුව Ingress IP එක හොයාගන්න විවිධ ක්‍රම තියෙනවා.

නැත්නම්, kn CLI tool එක install කරලා නම්, direct URL එකෙන්ම access කරන්න පුළුවන්:

curl "$(kn service describe knative-spring-demo -o url)"/hello

මේකෙන් ඔයාලට ආයුබෝවන් Knative! Spring Boot application එක වැඩ! වගේ message එකක් ලැබෙන්න ඕනේ.

Scaling to Zero:

දැන් විනාඩි කිහිපයක් කිසිම request එකක් යවන්නේ නැතුව ඉන්න. Knative එකේ විශේෂත්වය තමයි application එක automatically 0 pods දක්වා scale වෙන එක. ඒ කියන්නේ resources කිසිම එකක් use කරන්නේ නැතුව dormancy mode එකට යනවා. ආයෙත් request එකක් ආවොත්, Knative එකෙන් ඉක්මනටම pod එකක් spin up කරලා request එක handle කරනවා. මේක serverless වල තියෙන ලොකුම වාසියක්!

Knative වල වාසි සහ Next Steps (Benefits of Knative and Next Steps)

Spring Boot application එකක් Knative එක්ක deploy කරන එකෙන් අපිට ගොඩක් වාසි ලැබෙනවා:

  • Auto-Scaling: Requests වලට අනුව automatically scale up සහ scale down වීම. Request නැති වෙලාවට 0 දක්වා scale වීම (scale-to-zero) නිසා cost savings ලැබෙනවා.
  • Revision Management: Application එකේ විවිධ versions deploy කරලා ඒවා අතර traffic split කරන්න (Canary deployments, Blue/Green deployments) පහසුවෙන් පුළුවන්. Rollback කිරීමත් හරිම ලේසියි.
  • Simplified Deployments: Knative Service definition එක සරල නිසා Kubernetes concepts ගැන වැඩිදුර දැනුමක් නැතුව වුණත් serverless applications deploy කරන්න පුළුවන්.
  • Eventing: Knative Eventing එක්ක එකතු වෙලා Event-Driven Microservices architectures හදන්න පුළුවන්. Kafka, RabbitMQ, Cloud Storage events වගේ ගොඩක් sources වලින් එන events process කරන්න Knative Eventing උදව් වෙනවා.
  • Cost Efficiency: භාවිතයට අනුව පමණක් ගෙවන (pay-per-use) model එකක් ලැබෙනවා, මොකද idle වෙලාවට resources භාවිතා වෙන්නේ නෑ.

Next Steps:

  1. Custom Domains: ඔයාලගේ Knative Service එකට custom domain එකක් set කරන හැටි ඉගෙන ගන්න.
  2. Traffic Splitting: Application එකේ අලුත් version එකක් deploy කරලා, traffic එකෙන් පොඩි කොටසක් අලුත් version එකට යවලා test කරන හැටි (canary deployment) අත්හදා බලන්න.
  3. Knative Eventing: Knative Eventing පාවිච්චි කරලා event-driven application එකක් හදන්න උත්සාහ කරන්න.
  4. Monitoring and Logging: Knative Services monitoring සහ logging කරන්නේ කොහොමද කියලා බලන්න.

නිගමනය (Conclusion)

ඉතින් යාළුවනේ, අද අපි Spring Boot application එකක් Knative එක්ක Serverless Service එකක් විදියට Kubernetes මත deploy කරන්නේ කොහොමද කියලා විස්තරාත්මකව ඉගෙන ගත්තා. මේකෙන් අපිට infrastructure manage කිරීමේ කරදර නැතුව, අපේ code එක develop කරන එකට විතරක් අවධානය දෙන්න පුළුවන් බව තේරුම් ගත්තා. Knative ගෙනෙන auto-scaling, revision management, සහ simplified deployments වගේ වාසි අතිවිශාලයි.

මේ tutorial එක ඔයාලට ගොඩක් ප්‍රයෝජනවත් වෙන්න ඇති කියලා මම හිතනවා. දැන් ඔයාලට පුළුවන් මේ concept එක තමන්ගේ projects වලටත් apply කරන්න. කිසියම් ගැටලුවක් ආවොත් හෝ අමතර දැනුමක් එකතු කරන්න කැමති නම්, comment section එකේ ඔයාලගේ අදහස් share කරන්න අමතක කරන්න එපා. අපි තවත් මේ වගේ වැදගත් tutorial එකකින් හමුවෙමු! සුබ දවසක්!