La traduction de l'article a été préparée spécialement pour les étudiants du cours Spring Framework Developer .
Créons une application Spring Boot de base qui s'exécutera sur un cluster Kubernetes.
âââ Dockerfile
âââ build.gradle
âââ gradle
â âââ wrapper
â âââ gradle-wrapper.jar
â âââ gradle-wrapper.properties
âââ gradlew
âââ k8s
â âââ depl.yaml
âââ settings.gradle
âââ src
âââ main
âââ java
âââ hello
âââ App.java
âââ HelloWorldCtrl.java
App.java â :
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Spring Boot â .
HelloWorldCtrl.java , («/») index, :
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloWorldCtrl {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
Gradle. build.gradle :
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
K8s
K8s Docker-. Dockerfile :
FROM gradle:jdk10
COPY --chown=gradle:gradle . /app
WORKDIR /app
RUN gradle build
EXPOSE 8080
WORKDIR /app
CMD java -jar build/libs/gs-spring-boot-0.1.0.jar
Dockerfile:
- /app
- Gradle
- ,
docker build -t marounbassam/hello-spring .
docker push marounbassam/hello-spring
K8s . (Deployment) (Service):
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 2
template:
metadata:
labels:
app: hello-world
visualize: "true"
spec:
containers:
- name: hello-world-pod
image: marounbassam/hello-spring
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
labels:
visualize: "true"
name: hello-world-service
spec:
selector:
app: hello-world
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
type: ClusterIP
Deployment , , , image.
(Service) ClusterIP ( ). .
:
kubectl create -f <yaml_file>
:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-5bb87c95-6h4kh 1/1 Running 0 7h
hello-world-5bb87c95-bz64v 1/1 Running 0 7h
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-world-service ClusterIP 10.15.242.210 <none> 8080/TCP 5s
kubernetes ClusterIP 10.15.240.1 <none> 443/TCP 7h
$ kubectl exec -it hello-world-5bb87c95-6h4kh bash
$ (inside the pod) curl 10.15.242.210:8080
$ (inside the pod) Greetings from Spring Boot!
, . LoadBalancer ( ) - .
Spring Boot, Docker- K8s, K8s deployment .
.