How can I use the port of a server running on localhost in kubernetes running spring boot app
We can get the vmware host ip using minikube with this command minikube ssh "route -n | grep ^0.0.0.0 | awk '{ print \$2 }'"
. For me it's 10.0.2.2
on Mac. If using Kubernetes on Docker for mac, it's host.docker.internal
.
By using these commands, I managed to connect to the services running on host machine from kubernetes.
1) Inside your application.properties define
server.port=8000
2) Create Dockerfile
# Start with a base image containing Java runtime (mine java 8)FROM openjdk:8u212-jdk-slim# Add Maintainer InfoLABEL maintainer="vaquar.khan@gmail.com"# Add a volume pointing to /tmpVOLUME /tmp# Make port 8080 available to the world outside this containerEXPOSE 8080# The application's jar file (when packaged)ARG JAR_FILE=target/codestatebkend-0.0.1-SNAPSHOT.jar# Add the application's jar to the containerADD ${JAR_FILE} codestatebkend.jar# Run the jar file ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/codestatebkend.jar"]
3) Make sure docker is working fine
docker run --rm -p 8080:8080
4)
- https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
use following command to find the pod name
kubectl get pods
then
kubectl port-forward <pod-name> 8080:8080
Useful links :