visit
With this article, you may build your simple API server on JAVA
without super-knowledge, and deploy it on minikube automatically with scripts.
2. CREATE JAVA API SERVICE
First things first, we need to make a spring boot JAVA project. You can do it on your own or just pull open repository from my GitHub.
If you chose to do it on your own: 1) generate and download the template JAVA Spring boot project from the official .
Choose Maven Project and Java language. Then generate and open this project.
2) Need to add spring-boot-starter-web. This is a starter for building web,
including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3) Add classes Service and Controller to our project.
Class ApiService:
package com.example.demo;
public class ApiService {
public static String getTest(){
String result ="Hello!";
return result;
}
}
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("v1/demo")
public class ApiController {
@GetMapping(path = "test")
public String getTest(){
return ApiService.getTest();
}
}
3. DOCKERFILE AND DEPLOYMENT MANIFEST
To deploy the application to server VPS or cloud, you must have
a VPS server and account in the . Docker Hub is the world's largest library and community for container images. Using Docker Hub we can upload the images we need to share. These can then be downloaded as needed.
# syntax=docker/dockerfile:1
FROM openjdk:18.0.1.1-oraclelinux7
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY src ./src
CMD ["./mvnw", "spring-boot:run"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapi
spec:
selector:
matchLabels:
app: demoapi
replicas: 1
template:
metadata:
labels:
app: demoapi
spec:
containers:
- name: demoapi
image: {Your_repository_in_docker_registry}/demoapi-docker:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: demoapi
spec:
type: LoadBalancer
selector:
app: demoapi
ports:
- name: http
port: 80
targetPort: 8080
4. MINIKUBE
Of course, we need Minikube. Why Minikube and not Kubernetes? Because this is enough for our task and easier.
5. AUTOMATION OF RELEASING. CICD
Pay attention, For our automation to work we need to have ssh. Create shell script ci.sh:#!/bin/bash
ssh {user}@{server} 'rm -rf demoapi_bk'
ssh {user}@{server} 'mv demoapi demoapi_bk'
scp -r demoapi {user}@{server}:~/
ssh {user}@{server} 'cd demoapi && docker build --tag {docker_image} .'
ssh {user}@{server} 'cd demoapi && docker push {docker_image_path}:{tag}’
#!/bin/bash
docker image prune --filter until=48h -f
kubectl apply -f demoapi/k8s/dc.yaml
kubectl rollout restart deploy demoapi
kubectl get po
kubectl get svc
# HAproxy for web servers
frontend web-frontend
bind {external_ip}:80
mode http
default_backend web_backend
backend web_backend
balance roundrobin
server web-server1 {internal ip}:80 check
sudo systemctl restart haproxy
minikube tunnel