The translation of the article has been prepared especially for students of the Spring Framework Developer course .
Let's create a basic Spring Boot application that will run on a Kubernetes cluster.
โโโ 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 .
.