Github Project Link – https://github.com/kuldeep101990/Spring-kubernates-demo
- Generate Spring Boot App using Spring Initializer:
- Go to Spring Initializr
- Set the project properties, add dependencies (e.g., Spring Web, Spring Boot DevTools).
- Download the generated
.zip
file. - Extract the
.zip
file.
- Navigate to the extracted folder:
cd path_to_your_project
- Build the Project using Maven:
mvn clean install
- Create Dockerfile:
- In the root folder of your project, create a
Dockerfile
:FROM openjdk:17-jdk-slim COPY target/*.jar app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
- In the root folder of your project, create a
- Build the Docker image:
docker build -t k8s-demo-app:1.0 .
- Start Minikube:
minikube start
- Set Docker environment to Minikube: For Windows with Docker Desktop, run the following commands to set the environment properly:
- First, configure Docker for Minikube:
minikube docker-env
- Then, run the following commands:
set DOCKER_TLS_VERIFY=1 set DOCKER_HOST=tcp://<minikube-ip>:2376 set DOCKER_CERT_PATH=<path-to-minikube-cert> set DOCKER_MACHINE_NAME=minikube
Where<minikube-ip>
is the IP of your Minikube cluster (obtain it usingminikube ip
) and<path-to-minikube-cert>
is the certificate path from Minikube (usually located at~/.minikube/certs
). Alternatively, you can use:minikube -p minikube docker-env --shell powershell
- First, configure Docker for Minikube:
- Create Kubernetes deployment YAML (e.g.,
deployment.yaml
):apiVersion: apps/v1 kind: Deployment metadata: name: k8s-demo-app spec: replicas: 2 selector: matchLabels: app: k8s-demo-app template: metadata: labels: app: k8s-demo-app spec: containers: - name: k8s-demo-app image: k8s-demo-app:1.0 ports: - containerPort: 8080
- Create Kubernetes service YAML (e.g.,
service.yaml
):apiVersion: v1 kind: Service metadata: name: k8s-demo-app spec: selector: app: k8s-demo-app ports: - protocol: TCP port: 8080 targetPort: 8080 type: NodePort
- Apply the deployment and service:
kubectl apply -f deployment.yaml kubectl apply -f service.yaml
- Check if the pods are running:
kubectl get pods
- Get the Minikube IP:
minikube ip
- Check the services to get the NodePort:
kubectl get services
- Access the service URL (replace the port with your NodePort):
curl http://<minikube-ip>:<node-port>
- Access service using Minikube service command:
minikube service k8s-demo-app --url
- Open the URL in the browser:
- Go to
http://127.0.0.1:<port>
in your browser.
- Go to
Additional Notes and Solutions for Errors:
- If you face issues where
ssh
is not recognized:- Install OpenSSH from Windows Features (Control Panel > Programs > Turn Windows Features On or Off > OpenSSH Client).
- Alternatively, you can run the
ssh
command directly by checking if the OpenSSH Client is installed and correctly added to the system PATH.
By following these commands step by step, your Spring Boot app should successfully run on Kubernetes using Minikube.