Documentation - Spring boot container + kubernates
(minikube)
Requirements:
- Spring boot
- minikube (https://minikube.sigs.k8s.io/docs/start)
- kompose (https://github.com/kubernetes/kompose/releases)
1. Create your spring app (ex.: demo)
a. Create some controller base (for chek app activity)
i. <Code and prints of implementation>
@RestController
//@RequestMapping("/cj")
public class Hello {
@GetMapping
public ResponseEntity<String> hello(){
return ResponseEntity.ok("Server is runnig!");
}
@GetMapping("/get-all")
public ResponseEntity<String> getAll(){
return ResponseEntity.ok("getting all!");
}
run: below command (on root of your project) to generate .jar file
# ./mvnw clean package or # mvn clean package
If you’re using gradle:
# gradle clean build
It will generate a jar file as you can see bellow:
target/demo-0.0.1-SNAPSHOT.jar app.jar
b. Create you Dockerfile, and docker-compose file with your services
i. <code and prints)
ii. dockerfile
FROM openjdk:21-jdk-slim
LABEL authors="CJ-Prog"
# Defina o diretório de trabalho
WORKDIR /app
COPY . .
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
iii. Dockerignore to ignore unnecessary files:
# Ignore all files
*
# Do not ignore necessary files
!Dockerfile
# Include the target directory and the JAR file inside it
!target/
!target/demo-0.0.1-SNAPSHOT.jar
c. Create your compose file to run all (container and pull image in one command):
version: '2'
services:
demo-server:
container_name: demo-server-app-container
image: demo-app-kubernetes
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
networks:
- spring-cloud-network
networks:
spring-cloud-network:
driver: bridge
d. To use docker with minikube we will need to switch the docker context to using
minikube. The commands bellows allow you to work in context (session) of
minikube on docker, to build images inside minikube.
eval $(minikube docker-env)
or on windows
& minikube -p minikube docker-env --shell powershell |
Invoke-Expression
e. Configure kompose and add on environment variables to be accessed globally
(compose will help you to generate a deployment file from your compose file).
run the command to convert you compose in a pod/deployment file:
$> kompose convert -f compose.yaml
It will generate a deployment.yam file and service.yaml file
f. You deployment file will be like that:
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: C:\minikube\kompose.exe convert -f compose.yaml
kompose.version: 1.34.0 (cbf2835db)
labels:
io.kompose.service: demo-server
name: demo-server
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: demo-server
template:
metadata:
annotations:
kompose.cmd: C:\minikube\kompose.exe convert -f compose.yaml
kompose.version: 1.34.0 (cbf2835db)
labels:
io.kompose.service: demo-server
spec:
containers:
- image: demo-app-kubernetes
name: demo-server-app-container
#add this line on your file to look first on your local
images
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
protocol: TCP
restartPolicy: Always
To deploy a locally built image in Minikube, set imagePullPolicy to
IfNotPresent. This uses the local image if available, otherwise it pulls from
Docker Hub
2. Deployment (run those commands):
minikube start --force
Build the Docker Image:
$> docker build -t demo-app:latest .
docker image ls (to see your images built)
Apply the Deployment and Service:
$> kubectl apply -f deployment.yaml
$> kubectl apply -f service.yaml
Check the Status:
$> kubectl get pods
$> kubectl get services
Get the Minikube IP and access your application via the
NodePort:
$> minikube ip