Expose cluster in k8s on localhost

6/4/2019

Because docker supports out of the box kubernetes (on my Mac) I thought I try it out and see if I can load balance a simple webservice. For that, I created a simple image, which exposes port 3000 and only returns Hello World. And I created a k8s config yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-kubernetes
spec:
  type: NodePort
  externalIPs:
  - 192.168.2.85
ports:
  - port: 8080
    targetPort: 3000
selector:
  app: hello-kubernetes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-kubernetes
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
        - name: hello-kubernetes
          image: hello/world:latest
          ports:
            - containerPort: 3000

Apply it

gt; kubectl
apply -f ./example.yaml

I see 3 pods running, and a service

NAME               TYPE        CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
hello-kubernetes   NodePort    10.99.38.46     192.168.2.85   8080:30244/TCP   42m

I've used NodePort above, but I'm not sure if I can use Loadbalancer here as well.

Anyway, in the browser I get the message This site can’t be reached when I goto http://192.168.2.85:8080 or `http://192.168.2.85:30244 (I never know which port to use)

So, I think I'm close, but I still missed something :( Any help would be appreciated!

-- Jeanluca Scaljeri
kubernetes

2 Answers

6/4/2019
k explain service.spec.externalIPs

KIND: Service VERSION: v1

FIELD: externalIPs <[]string>

DESCRIPTION: externalIPs is a list of IP addresses for which nodes in the cluster will also accept traffic for this service. These IPs are not managed by Kubernetes. The user is responsible for ensuring that traffic arrives at a node with this IP. A common example is external load-balancers that are not part of the Kubernetes system.

Problem here is we don't know your network settings. IS this minikube for mac? Is the 192.168.2.x network reachable for you? In my case using minikube all I had to do was to edit the externalIP to be reachable from my network. So what I did to get this working was:

  • minikube IP in my case 192.168.99.100 (IP address of minikubeVM)
  • changed externalIP to 192.168.99.100

    k get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-kubernetes NodePort 10.105.212.118 192.168.99.100 8080:32298/TCP 46m

And I was able to reach the application using 192.168.99.100:8080.

Also note that in your case you have 8081 port (But I guess P Ekambaram already mentioned this).

-- aurelius
Source: StackOverflow

6/4/2019

the port number is wrong.

use http://NODEIP:NODEPORT

in your case, try

http://NODEIP:30244

-- P Ekambaram
Source: StackOverflow