Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ingress expose the service with the type clusterIP

Is it possible to expose the service by ingress with the type of ClusterIP?

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-service
  ports:
  - name: my-service-port
    port: 4001
    targetPort: 4001
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: my.example.com
      http:
        paths:
        - path: /my-service
          backend:
            serviceName: my-service
            servicePort: 4001

I know the service can be exposed with the type of NodePort, but it may cost one more NAT connection, if someone could show me what's the fastest way to detect internal service from the world of internet in the cloud.

like image 559
ccd Avatar asked Sep 19 '25 00:09

ccd


2 Answers

EDIT: This answer is no longer correct (but I feel like leaving it here for historical reasons). The short of it is as long as your Ingress Controller is acting as a reverse proxy, ClusterIP will work. It depends on which ingress controller you are using.

No, clusterIP is only reachable from within the cluster. An Ingress is essentially just a set of layer 7 forwarding rules, it does not handle the layer 4 requirements of exposing the internals of your cluster to the outside world. At least 1 NAT step is required.

For Ingress to work, though, you need to have at least one service involved that exposes your workload externally, so nodePort or loadBalancer. Your ingress controller and the infrastructure of your cluster will determine which of the two services you will need to use.

In the case of Nginx ingress, you need to have a single LoadBalancer service which the ingress will use to bridge traffic from outside the cluster to inside it. After that, you can use clusterIP services for each of your workloads.

In your above example, as long as the nginx ingress controller is correctly configured (with a loadbalancer), then the config you are using should work fine.

like image 96
Patrick W Avatar answered Sep 20 '25 19:09

Patrick W


In short : YES

Now to the elaborate answer...

First thing first, let's have a look at what the official documentation says :

Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster.
[...]
An Ingress controller is responsible for fulfilling the Ingress, usually with a load balancer...

What's confusing here is the term Load balancer. In the definition above, we are talking about the classic and well known in the web load balancer.
This one has nothing to do with kubernetes !

So back to the definition, to use an Ingress and make it work, we need a kubernetes resource called IngressController. And this resource happen to be a load balancer ! That's it.

However, you have to keep in mind that there is a difference between a load balancer in the outside world and a kubernetes service of type type:LoadBalancer.

So in summary (and in order to redirect the traffic from the outside world to your k8s clusterIp service) :

  • Do you need a Load balancer to make your kind:Ingress works ? Yes, this is the kind:IngressController kubernetes resource.
  • Do you need a kubernetes service type:LoadBalancer or type:NodePort to make your kind:Ingress works ? Definitely no ! A service type:ClusterIP works just fine !
like image 39
Marc ABOUCHACRA Avatar answered Sep 20 '25 18:09

Marc ABOUCHACRA