With this article, you may build your simple API server on JAVA
without super-knowledge, and deploy it on minikube automatically with scripts.
I often make back-end services for mobile apps, and I have gathered experience. As a result, this article has collected instructions, which will help quickly build a fail-over API server and not to step on the rake!
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 this 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 website.
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.
Add to pom.xml:
<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;
}
}
Class ApiController:
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();
}
}
4) Now we could run the application and test our server.
Open in browser http://localhost:8080/v1/demo/test or type in a command line:
curl -v http://localhost:8080/v1/demo/test
Perfectly! It is working! Go further.
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. 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.
For uploading and pulling the image from the docker registry, we need Dockerfile and a deployment YAML file. Also, there are in this open repository from my GitHub.
Dockerfile:
# 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"]
k8s/dc.yaml:
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
In this way after finishing developing we build the image and push it to the docker registry. Then restart deployment in Minikube and containers will start with the last code version.
4. MINIKUBE
Of course, we need Minikube. Why Minikube and not Kubernetes? Because this is enough for our task and easier.
1) Install Minikube on the server from the official guide.
2) HAProxy. We need a load balancer to allow external requests to our API server. So, install HAProxy to the server from here
HAProxy is a free, very fast, and reliable reverse proxy offering high availability, load balancing, and proxying for TCP and HTTP-based applications. Over the years it has become the de-facto standard open-source load balancer.
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}’
And cd.sh:
#!/bin/bash
docker image prune --filter until=48h -f
kubectl apply -f demoapi/k8s/dc.yaml
kubectl rollout restart deploy demoapi
(Don’t forget chmod +x shell files.)
Run scripts and check the result with kubectl:
kubectl get po
kubectl get svc
Finally, when Pods started, let's set up the HAProxy config:
# 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
EXTERNAL-IP is the external IP of your server.
INTERNAL-IP in HAProxy config is EXTERNAL-IP of your service in Minikube.
sudo systemctl restart haproxy
To connect to LoadBalancer services run the command:
minikube tunnel
Finally, now we have an API server with back-end service, which can be updated regularly. Minikube provides provide fault tolerance and rolling update.