Spring boot – Kubernetes deployment

Github Project Link – https://github.com/kuldeep101990/Spring-kubernates-demo

  1. 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.
  2. Navigate to the extracted folder: cd path_to_your_project
  3. Build the Project using Maven: mvn clean install
  4. 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"]
  5. Build the Docker image: docker build -t k8s-demo-app:1.0 .
  6. Start Minikube: minikube start
  7. 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 using minikube 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
  8. 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
  9. 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
  10. Apply the deployment and service: kubectl apply -f deployment.yaml kubectl apply -f service.yaml
  11. Check if the pods are running: kubectl get pods
  12. Get the Minikube IP: minikube ip
  13. Check the services to get the NodePort: kubectl get services
  14. Access the service URL (replace the port with your NodePort): curl http://<minikube-ip>:<node-port>
  15. Access service using Minikube service command: minikube service k8s-demo-app --url
  16. Open the URL in the browser:
    • Go to http://127.0.0.1:<port> in your browser.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *