简体   繁体   中英

Why can't I attach a service type ClusterIP to Ingress on GKE?

Before I begin, I would like to mention that I am on the free trial version of GKE. I have a simple server running in a GKE cluster. I have a service that I use to expose the server. I am trying to configure an Ingress Controller and attach it to this service.

Everything works perfectly if my service is of type LoadBalancer, NodePort. However, if my service is of type ClusterIP, I get an error saying

error while evaluating the ingress spec: service "default/simple-server" is type "ClusterIP"
, expected "NodePort" or "LoadBalancer" 

GKE then stops trying to provision an IP for the ingress. Why can't I provision a service of type clusterIP and is there a work around?

I tried using annotations.kubernetes.io/ingress.class: "nginx" and it still didn't work.

The native GKE ingress controller does not support ClusterIP , but it works perfectly with LoadBalancer and NodePort type. Take a look at this issue

Non-native ingress controller Nginx works with ClusterIP .

That's normal and it's pretty simple why you have the error.

A ClusterIP service is an internal only service inside of your Kubernetes cluster and what you are trying to create (from what I undestood) is a publicly exposed service using ingress, meaning you are going to create a Google Load balancer.

Now the why it doesn't support ClusterIP is because when you create the ingress, the LB resource created inside google cloud needs a target port on your cluster to call on And for that you need to expose an ip/port for it.

I would not recommend combining LB service (wich by default create a LB on cloud provider) and ingress, but stay on a nodeport/ingress duo that is cleaner.

Bonus: The annotation you used is for internal services with ingress to be linked to your ingress controller. This will allow the ingress-controller to list the hosts and proxy traffic to the right service.

Use something like this.Use NodPort or LoadBalancer infront of type as per you convenience.

spec:
  ports:
    - protocol: TCP
      port: XX
      targetPort: XX
  type: NodePort

You can use Ingress to access a ClusterIP in GKE by setting the annotation cloud.google.com/neg: '{"ingress": true}' to the service.

apiVersion: v1
kind: Service
metadata:
  name: myapp
  annotations:
    cloud.google.com/neg: '{"ingress": true}'
...

See also answer https://stackoverflow.com/a/68271142 in the related question Why does Google Cloud show an error when using ClusterIP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM