Pyramid Newbie
Pyramid Newbie

Reputation: 7355

How to expose a Kubernetes service externally using NodePort

I run the CoreOS k8s cluster on Mac OSX, which means it's running inside VirtualBox + Vagrant

I have in my service.yaml file:

spec:
  type: NodePort

When I type:

kubectl get services

I see:

NAME             CLUSTER_IP       EXTERNAL_IP   PORT(S)    SELECTOR                                
kubernetes       10.100.0.1       <none>        443/TCP    <none>                             
my-frontend      10.100.250.90    nodes         8000/TCP   name=my-app

What is the "nodes" external IP? How do I access my-frontend externally?

Upvotes: 17

Views: 41957

Answers (4)

Vyacheslav Enis
Vyacheslav Enis

Reputation: 1651

In addition to "NodePort" types of services there are some additional ways to be able to interact with kubernetes services from outside of cluster:

  • Use service type "LoadBalancer". It works only for some cloud providers and will not work for virtualbox, but I think it will be good to know about that feature. Link to the documentation
  • Use one of the latest features called "ingress". Here is description from manual "An Ingress is a collection of rules that allow inbound connections to reach the cluster services. It can be configured to give services externally-reachable urls, load balance traffic, terminate SSL, offer name based virtual hosting etc.". Link to the documentation
  • If kubernetes is not strict requirements and you can switch to latest openshift origin (which is "kubernetes on steroids") you can use origin feature called "router".

Upvotes: 14

Aprimit
Aprimit

Reputation: 171

I assume you are using MiniKube for Kubernetes. In such case, to identify your node ip address, use the following command:

.\minikube.exe ip

If the exposed service is of type=Nodeport, to check the exposed port use the following command:

.\kubectl.exe describe service <service-name>

Check for Node port in the result. Also, if you want to have all these details via nice UI, then you can launch the Kubernetes Dashboard present at the following address:

<Node-ip>:30000

Upvotes: 9

caesarxuchao
caesarxuchao

Reputation: 1109

Here is the doc on node addresses: http://kubernetes.io/docs/admin/node/#addresses

You can specify the port number of nodePort when you specify the service. If you didn't manually specify a port, system will allocate one for you. You can kubectl get services -o yaml and find the port at spec.ports[*].nodePort, as suggested in the doc here: https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/services.md#type-nodeport

And you can access your front-end at {nodes' external addresses}:{nodePort}

Hope this helps.

Upvotes: 1

ant31
ant31

Reputation: 4780

The easiest way to get the host ports is kubectl describe services my-frontend. The node port will be displayed.

Also you can check the api:

  • api/v1/namespaces/{namespace_name}/services/{service_name}

or list all:

  • api/v1/namespaces/default/services

Last, you can chose a fixed nodePort in the service.yml

Upvotes: 3

Related Questions