Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access kubernetes service on localhost with Docker for Windows

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>
like image 852
Liero Avatar asked Sep 26 '18 11:09

Liero


People also ask

Can you use localhost in Kubernetes?

Localhost (IP address 127.0.Kubernetes can take advantage of this structure and give the entire pod a shared network namespace. A network namespace is a Linux kernel feature that allows network resources to be put into groups.


1 Answers

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.

like image 75
Artem Golenyaev Avatar answered Sep 22 '22 03:09

Artem Golenyaev