How to reach kubernete service running in minikube from the same computer

6/7/2018

I created a small java application launching manually a jetty server listening to the address 127.0.0.1 port 8081. The small server app listens for GET requests to the subaddress /dockerClient/ping, and answers "pong".

I test with SoapUI on http://127.0.0.1:8081/dockerClient/ping, and I get my pong.

I create a docker image, deploy the application on minikube and expose a service with the following configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
name: client
labels:
  tier: frontend
spec:
   replicas: 1
   selector:
      matchLabels:
      tier: frontend
   template:
      metadata:
       labels:
         tier: frontend
      spec:
        containers:
        - name: docker-client
          image: docker-client
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
  name: client-service
  labels:
    tier: frontend
spec:
  type: NodePort
  ports:
  - port: 8081
    protocol: TCP
    name: http
  selector:
    tier: frontend

Once I deploy and expose, I get the following information:

NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
client-service   NodePort    10.107.64.238   <none>        8081:31703/TCP   19m

I try to use SoapUI to reach the service : For this, I retrieve the IP of the minikube using :

echo $(minikupe ip)

Then, I try a GET request to http://$(minikube ip):31703/dockerClient/ping, but the request is refused.

I tried on http://$(minikube ip):8081/dockerClient/ping, same.

What do I do wrong? How can I reach the jetty server exposing my ping?

-- Joel
embedded-jetty
kubernetes
minikube

2 Answers

6/7/2018

I think the jetty server should listen to the address 0.0.0.0.

-- Kun Li
Source: StackOverflow

6/7/2018

In your service yml, you just tell the port (it can only be used within the cluster), it is not enough to expose your service outside your minikube, you also need to specify the targetPort (should match the container port e.g.8081) and nodePort (it is the port you can ping from localhost e.g. 31081).

---
apiVersion: v1
kind: Service
metadata:
  name: client-service
  labels:
    tier: frontend
spec:
  type: NodePort
  ports:
  - port: 8081 # access within the cluster
    targetPort: 8081 # should match to the container port
    nodePort: 31081 # expose outside the cluster and range from 30000 to 32767
    protocol: TCP
    name: http
  selector:
    tier: frontend

After you added the targetPort and nodePort, you can get the public endpoint by:

minikube service client-service --url

P.S. nodePort is optional, minikube would assign an random port from the range if nodePort has not been specified.

-- kitko112
Source: StackOverflow