Is there a default NodePort when you deploy a service?

8/2/2019

I have been following this guide: https://medium.com/javarevisited/kubernetes-step-by-step-with-spring-boot-docker-gke-35e9481f6d5f

Which has the following line:

   kubectl expose deployment userapi --type=LoadBalancer --port 80 --target-port 8080

I can access this service by just going to its external IP without specifying the nodeport. I am not sure why this is. I thought I would need to do externalip:nodeport.

Thank you

-- FranktheTank
google-cloud-platform
kubernetes

2 Answers

8/2/2019

Kubernetes services are hierarchical. A nodePort service will contain a clusterIP service. A loadBalancer service will contain a nodePort service.

If you execute your command kubectl expose deployment userapi --type=LoadBalancer --port 80 --target-port 8080 Kubernetes will create a service named userapi. Now in order to see the full (generated) yaml for this service simply do: kubectl get svc userapi -o yaml. You will see that Kubernetes automatically asigned a nodePort to your type:loadBalancer service.

NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You’ll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.

LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

-- Ph03n1x
Source: StackOverflow

8/2/2019

Quick answer:

If you create a service of type LoadBalancer it will create all the resources needed by the Nodeport as well. It does not create the actual service, it simply implement all the features needed.

You can think to the types of services as a hierarchy, each time it adds a new functionality, but you need the previous one.


Explanation:

Quoting from official documentation:

LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

If you create or expose a Noteport it will be assigned as well a ClusterIp.

NodePort: Exposes the Service on each Node’s IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You’ll be able to contact the NodePort Service, from outside the cluster, by requesting :

Just for the sake of completeness when creating a service a new resource of type endpoint is created as well that is totally managed by Kubernetes and should not be manually modified:

ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType

Endpoints:

Endpoints is a collection of endpoints that implement the actual service. Example: Name: "mysvc", Subsets: [ { Addresses: [{"ip": "10.10.1.1"}, {"ip": "10.10.2.2"}], Ports: [{"name": "a", "port": 8675}, {"name": "b", "port": 309}] }, { Addresses: [{"ip": "10.10.3.3"}], Ports: [{"name": "a", "port": 93}, {"name": "b", "port": 76}] }, ]

-- GalloCedrone
Source: StackOverflow