This blog will share the details of deploying a simple "Hello World" Java application on a local Minikube cluster. Create a new Java project using Maven, create a Dockerfile for building the docker image, and deploying the built image in the local Minikube cluster will be explained through this. You will see how to enable the local Docker client to talk to the Minikube cluster's Docker daemon while reading.
Prerequisites:
- Have Java and Maven installed on your local machine.
- Have Docker and Minikube installed and configured on your local machine.
Step 1: Create a new Java project using Maven
Create a new Java project using Maven by running the following command in the terminal.
mvn archetype:generate -DgroupId=witcentre -DartifactId=java-app -Dversion=1.0 -DarchetypeArtifactId=maven-archetype-quickstart
This will create a basic Java project structure with a sample "Hello World" program in the "App.java" file.
Step 2: Create a Dockerfile for containerization
Create a Dockerfile in the root of your Java project. This file will be used to build a Docker image of your application. The contents of the Dockerfile should look something like following.
FROM openjdk:8-jre-alpineCOPY target/java-app-1.0.jar /app.jarCMD ["java", "-jar", "/app.jar"]
This Dockerfile will use the openjdk:8-jre-alpine image as the base image for the container, copy the compiled jar file of your application to the container and then run the jar file using the "java -jar" command.
Step 3: Build a Docker image using the Dockerfile
Build the Docker image by running the following command in the root of your project, where image-name is the name you want to give to your image.
docker build -t java-app:1.0 .
The full stop sets the Docker context for the docker image. Thus, do not miss it! Before moving to next steps, verify that the image has been created by running the command "docker images" in the terminal.
Step 4: Start the Minikube cluster and set Docker daemon for Minikube
Start a Minikube cluster by running the command "minikube start" in the terminal. Once it is up, run the command "eval $(minikube docker-env)" to configure your local Docker client to talk to the Minikube cluster's Docker daemon.
Step 5: Deploy application to the Minikube cluster
Use the following command to deploy the application to the Minikube cluster. This will create a pod running the application.
kubectl run java-app --image=java-app:1.0 --port=8080
Verify that the application has been deployed by running the command "kubectl get pods" and checking that the pod corresponding to your application is in the "Running" state.
Step 6: Expose the application as a service and access it
Expose your deployment as a service by running the following command.
kubectl expose pod java-app --type=LoadBalancer
Then, get the IP and port to access the application by running the following command.
minikube service java-app --url
Access the application by visiting "http://[IP]:8080/ in your web browser.
Once everything is done, you may using following steps to stop or delete what you have done.
- To stop the cluster use command => "minikube stop"
- To delete the cluster use command => "minikube delete"
Post A Comment:
0 comments: