Deploying a Spring Boot Application to Minikube
Building a Spring Boot Application
Let's start by creating a simple Spring Boot application that we can deploy to Minikube. You can use the Spring Initializr (https://start.spring.io/) to generate a basic Spring Boot project, or you can create one manually.
Here's an example of a simple Spring Boot application:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/")
public String hello() {
return "Hello from Spring Boot!";
}
}
Creating a Docker Image
To deploy your Spring Boot application to Minikube, you'll need to package it into a Docker image. You can do this by creating a Dockerfile
in the root of your Spring Boot project:
FROM openjdk:11-jdk-slim
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
This Dockerfile
uses the openjdk:11-jdk-slim
base image, copies the Spring Boot application JAR file into the container, and sets the entry point to run the application.
Build the Docker image using the following command:
docker build -t your-spring-boot-app .
Replace your-spring-boot-app
with a name that you prefer for your Docker image.
Deploying to Minikube
Now that you have a Docker image of your Spring Boot application, you can deploy it to your Minikube cluster. First, make sure your Minikube cluster is running:
minikube status
Next, use the kubectl
command-line tool to create a Kubernetes Deployment and a Service for your application:
kubectl create deployment spring-boot-app --image=your-spring-boot-app
kubectl expose deployment spring-boot-app --type=NodePort --port=8080
This will create a Kubernetes Deployment named spring-boot-app
that uses the Docker image you built earlier, and a Service that exposes the application on a random port.
You can check the status of your deployment and service using the following commands:
kubectl get deployment spring-boot-app
kubectl get service spring-boot-app
The output will show the details of your deployed application, including the URL you can use to access it.
By following these steps, you've successfully deployed a Spring Boot application to your Minikube cluster, making it ready for further development, testing, and exploration.