Does a ingress in kubernetes occupy the node's port

Viewed 496

I am reading the Kubernetes document about ingress.
If I understand correctly, The Ingress will forward the traffic of the node to services in Kubernetes. For example, when a HTTP request to the node's port 80, then the ingress will forward that data to a service.
My Question is:

  1. Does this mean the ingress will occupy port 80 of the node? Then if I have another application want to use the port 80 will cause an error?
  2. If the answer to my first question is "yes", then is it possible to config an Ingress to listen to the other port on a node.
3 Answers

In k8s, there is no such thing like Port x80xx is already being used by another application.

Now let me explain why: As you know, you have to have a service to expose your application/pods running in k8s cluster.Here, Each service you create will have a unique new k8s cluster internal ip assigned to it.So if you have another service with same port, you will not get that error(Port x80xx is already being used by another application). As that particular error will be raised only when two application tried to get the same ip and port(socket).

Now come to the point of ingress: Basically ingress is implemented based on the ingress controller pods running in k8s cluster and yes it has a service.So you can assigned it to any service port(there is pre-defined port) as it will get assigned new cluster ip to it.

Now to expose your service outside of the cluster: To expose your service outside the cluster, you can use serviceType either load balancer and nodeport.

NodePort: A NodePort is an open port on every node of your cluster.As this a port of your node(host machine),you can not assigned one port to multiple service as this will not have different ip rather than a single localhost ip.

Answer of your questions:

  1. Does this mean the ingress will occupy port 80 of the node? Then if I have another application want to use the port 80 will cause an error?

yes, if you expose your ingress controller service with nodePort 80, then no other service can use this port(Port x80xx is already being used by another application. ).Actually you can not do that cause kubernetes only allow nodePort with range above than 30000.

  1. If the answer to my first question is "yes", then is it possible to config an Ingress to listen to the other port on a node.

Yes you can use any other port (typically 30000–32767) as NodePort to expose your ingress service and in that case you have to use url like http://host_machin_IP:NodePort.

Ingress is kind of a configuration or a route mapping, but you need a IngressController which does the actual routing part.

In the example that you have looked at, it has given a rule to map the http path /testpath to the service named test on port 80; so that the particular service can cater requests send to /testpath url.

If you're interested on ingress I assume you're interested in getting external traffic into your cluster. Hence it's necessary to provide a service of the type nodeport to allow your ingress to get access through the port on the node to your port on your pod /container. And yes that does mean it occupy's ports on the node. However this is not a part of your interest as the node's port are different than the one on your pod. This page offers a good example how nodeports work in general.

If you intend to use different applications in your cluster. You'd probably good to go defining two ingresses with two separate nodePort Services which automatically manages the port management on each of your k8s nodes. If you're really interested in manually setting it you're good to go with such a solution1:

apiVersion: v1
kind: Service
metadata:  
 name: my-nodeport-service
spec:
  selector:    
app: my-app
type: NodePort
ports:  
- name: http
  port: 80
  targetPort: 80
  nodePort: 30036
  protocol: TCP
Related