How to access kubernetes service on localhost with Docker for Windows

9/26/2018

I have Windows 10 Pro with Docker for Windows v18.06.1-ce with kubernetes enabled.

Using kubectl create -f, I've created rc.yml:

apiVersion: v1
kind: ReplicationController
metadata:
  name: hello-rc
spec:
  replicas: 9
  selector:
    app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-ctr
        image: nigelpoulton/pluralsight-docker-ci:latest
        ports:
        - containerPort: 8080

svc.yml

apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels:
    app: hello-world
spec:
  type: NodePort
  ports:
  - port: 8080
    nodePort: 30001
    protocol: TCP
  selector:
    app: hello-world

How do I access the website behind the service?

I would expect localhost:8080 to be working, but it isn't, nor is 10.108.96.27:8080

> kubectl describe service/hello-svc
Name:                     hello-svc
Namespace:                default
Labels:                   app=hello-world
Annotations:              <none>
Selector:                 app=hello-world
Type:                     NodePort
IP:                       10.108.96.27
LoadBalancer Ingress:     localhost
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30001/TCP
Endpoints:                10.1.0.10:8080,10.1.0.11:8080,10.1.0.12:8080 + 6 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
-- Liero
docker
docker-for-windows
kubectl
kubernetes

1 Answer

9/26/2018

There are two ways to expose a service to the outer world from a Kubernetes cluster:

  1. type: LoadBalancer. However, it works only with cloud providers.

  2. type: NodePort. As you used in this case. Now, to access service inside Kubernetes cluster, you need to use the IP address of one of your Nodes and the port from the field nodePort For example, 12.34.56.78:30001

For more information, look through the official documentation.

-- Artem Golenyaev
Source: StackOverflow