Configuring GKE Ingress: Traffic Routing, Security, and Load Balancing
.png)
GKE Ingress acts as a bridge between external users and your Kubernetes services. It allows you to define rules for routing traffic based on hostnames and URL paths, enabling you to direct requests to different backend services seamlessly.
A single GKE Ingress controller routes traffic to multiple services by identifying the target backend based on hostname and URL paths. It supports multiple certificates for different domains.
FrontendConfig enables automatic redirection from HTTP to HTTPS, ensuring encrypted communication between the web browser and the Ingress.
BackendConfig that allows you to configure advanced settings for backend services. It provides additional options beyond standard service configurations, enabling better control over traffic handling, security, and load balancing behavior.
Setup GKE ingress with application loadbalancer
To specify an Ingress class, you must use the kubernetes.io/ingress.class annotation.The “gce” class deploys an external Application Load Balancer
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: “gce”
Configure FrontendConfiguration:
apiVersion: networking.gke.io/v1beta1
kind: FrontendConfig
metadata:
name: my-frontend-config
spec:
redirectToHttps:
enabled: true
The FrontendConfig resource in GKE enables automatic redirection from HTTP to HTTPS, ensuring secure communication between clients and services.
Associating FrontendConfig with your Ingress
You can associate a FrontendConfig with an Ingress. Use the “networking.gke.io/v1beta1.FrontendConfig” to annotate with the ingress.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
networking.gke.io/v1beta1.FrontendConfig: “my-frontend-config”
Configure Backend Configuration:
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: my-backendconfig
spec:
timeoutSec: 40
BackendConfig to set a backend service timeout period in seconds.The following BackendConfig manifest specifies a timeout of 40 seconds.
Associate the backend configuration with service:
apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/backend-config: ‘{“ports”:{“my-backendconfig”}}’
cloud.google.com/neg: ‘{“ingress”: true}’
spec:
ports:
- name: app
port: 80
protocol: TCP
targetPort: 50000
We can specify a custom BackendConfig for one or more ports using a key that matches the port’s name or number. The Ingress controller uses the specific BackendConfig when it creates a load balancer backend service for a referenced Service port.
Creating an Ingress with a Google-Managed SSL Certificate
To set up a Google-managed SSL certificate and link it to an Ingress, follow these steps:
- Create a ManagedCertificate resource in the same namespace as the Ingress.
- Associate the ManagedCertificate with the Ingress by adding the annotation networking.gke.io/managed-certificates to the Ingress resource.
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
name: managed-cert
spec:
domains:
- hello.example.com
- world.example.com
Associate the SSL with Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
networking.gke.io/v1beta1.FrontendConfig: “my-frontend-config”
networking.gke.io/managed-certificates: managed-cert
kubernetes.io/ingress.class: “gce”
associate it with the managed-certificate by adding an annotation.
Assign Static IP to Ingress
When hosting a web server on a domain, the application’s external IP address should be static to ensure it remains unchanged.
By default, GKE assigns ephemeral external IP addresses for HTTP applications exposed via an Ingress. However, these addresses can change over time. If you intend to run your application long-term, it is essential to use a static external IP address for stability.
Create a global static ip from gcp console with specific name eg: web-static-ip and associate it with ingress by adding the global-static-ip-name annotation.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
networking.gke.io/v1beta1.FrontendConfig: “my-frontend-config”
networking.gke.io/managed-certificates: managed-cert
kubernetes.io/ingress.class: “gce”
kubernetes.io/ingress.global-static-ip-name: “web-static-ip”
Google Cloud Armor Ingress security policy
Google Cloud Armor security policies safeguard your load-balanced applications against web-based attacks. Once configured, a security policy can be referenced in a BackendConfig to apply protection to specific backends.
To enable a security policy, add its name to the BackendConfig. The following example configures a security policy named security-policy:
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
namespace: cloud-armor-how-to
name: my-backendconfig
spec:
securityPolicy:
name: “security-policy”
User-defined request/response headers
A BackendConfig can be used to define custom request headers that the load balancer appends to requests before forwarding them to the backend services.
These custom headers are only added to client requests and not to health check probes. If a backend requires a specific header for authorization and it is absent in the health check request, the health check may fail.
To configure user-defined request headers, specify them under the customRequestHeaders/customResponseHeaders property in the BackendConfig resource. Each header should be defined as a header-name:header-value string.
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: my-backendconfig
spec:
customRequestHeaders:
headers:
- “X-Client-Region:{client_region}”
- “X-Client-City:{client_city}”
- “X-Client-CityLatLong:{client_city_lat_long}”
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: my-backendconfig
spec:
customResponseHeaders:
headers:
- “Strict-Transport-Security: max-age=28800; includeSubDomains”