How can I use the port of a server running on localhost in kubernetes running spring boot app

9/27/2019

I am new to Kubernetes and kubectl. I am basically running a GRPC server in my localhost. I would like to use this endpoint in a spring boot app running on kubernetes using kubectl on my mac. If I set the following config in application.yml and run in kubernetes, it doesn't work. The same config works if I run in IDE.

grpc:
  client:
    local-server:
      address: static://localhost:6565
      negotiationType: PLAINTEXT

I see some people suggesting port-forward, but it's the other way round (It works when I want to use a port that is already in kubernetes from localhost just like the tomcat server running in kubernetes from a browser on localhost)

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: testspringconfigvol
  labels:
    app: testspring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: testspringconfigvol
  template:
    metadata:
      labels:
        app: testspringconfigvol
    spec:
      initContainers:
      # taken from https://gist.github.com/tallclair/849601a16cebeee581ef2be50c351841
      # This container clones the desired git repo to the EmptyDir volume.
      - name: git-config
        image: alpine/git # Any image with git will do
        args:
          - clone
          - --single-branch
          - --
          - https://github.com/username/fakeconfig
          - /repo # Put it in the volume
        securityContext:
          runAsUser: 1 # Any non-root user will do. Match to the workload.
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
        volumeMounts:
        - mountPath: /repo
          name: git-config
      containers:
      - name: testspringconfigvol-cont
        image: username/testspring
        ports:
        - containerPort: 8080
        volumeMounts:  
        - mountPath: /usr/local/lib/config/
          name: git-config
      volumes:
        - name: git-config
          emptyDir: {}

What I need in simple terms:

Ports having some server in my localhost: localhost:6565, localhost:6566, I need to access these ports some how in my kubernetes. Then what should I set it in application.yml config? Will it be the same localhost:6565, localhost:6566 or how-to-get-this-ip:6565, how-to-get-this-ip:6566.

-- Suveen Kumar Vundavalli
docker
kubernetes
localhost
portforwarding
spring-boot

2 Answers

10/17/2019

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.

-- Suveen Kumar Vundavalli
Source: StackOverflow

9/27/2019

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 Info
LABEL maintainer="vaquar.khan@gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8080 available to the world outside this container
EXPOSE 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 container
ADD ${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)

use following command to find the pod name

 kubectl get pods 

then

  kubectl port-forward <pod-name> 8080:8080

Useful links :

-- vaquar khan
Source: StackOverflow