How to Configure Ingress in GKE on Google Cloud Platform
To configure
Ingress in GKE, first create a Kubernetes Ingress resource that defines rules for routing external HTTP(S) traffic to your services. Then, ensure your GKE cluster has an Ingress controller like the default GCP HTTP(S) Load Balancer enabled to manage the traffic flow.Syntax
An Ingress resource in GKE is a YAML file that defines how external traffic is routed to your Kubernetes services. It includes:
- apiVersion: The Kubernetes API version, usually
networking.k8s.io/v1. - kind: Always
Ingressfor this resource. - metadata: Name and annotations for the Ingress.
- spec: Rules that map hostnames and paths to backend services.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
kubernetes.io/ingress.class: "gce"
spec:
rules:
- host: example.com
http:
paths:
- path: /path
pathType: Prefix
backend:
service:
name: example-service
port:
number: 80Example
This example shows a complete Ingress configuration that routes traffic from myapp.example.com to a service named myapp-service on port 8080. It uses the GCP Ingress controller.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp-ingress
annotations:
kubernetes.io/ingress.class: "gce"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: myapp-service
port:
number: 8080Output
ingress.networking.k8s.io/myapp-ingress created
Common Pitfalls
Common mistakes when configuring Ingress in GKE include:
- Not specifying the correct
ingress.classannotation, which tells GKE which controller to use. - Using incorrect service names or ports in the backend, causing routing failures.
- Forgetting to expose the service with a
Serviceof typeNodePortorClusterIPthat matches the Ingress backend. - Not having the GKE Ingress controller enabled or misconfigured cluster permissions.
Example of a wrong annotation and the fix:
yaml
# Wrong: missing ingress class annotation apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: bad-ingress spec: rules: - http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80 # Right: specify GCE ingress class apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: good-ingress annotations: kubernetes.io/ingress.class: "gce" spec: rules: - http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80
Quick Reference
Tips for configuring Ingress in GKE:
- Always use
kubernetes.io/ingress.class: "gce"annotation for GCP Ingress controller. - Ensure your backend services are correctly exposed and reachable.
- Use
kubectl describe ingress [name]to debug issues. - Check GCP Console for Load Balancer status created by Ingress.
Key Takeaways
Create a Kubernetes Ingress resource with rules to route external traffic to services.
Use the annotation kubernetes.io/ingress.class: "gce" to enable GCP's Ingress controller.
Make sure backend services exist and expose the correct ports.
Check Ingress status and GCP Load Balancer to verify configuration.
Common errors include missing ingress class and incorrect service references.