The Shift to Gateway API for Kubernetes Traffic Management
Internal Kubernetes services often require robust authentication, especially when exposed externally or accessed by multiple teams. Historically, tools like ingress-nginx have been used to route and protect these services. However, with Kubernetes steering towards the Gateway API for traffic management, a new strategy is needed. This guide outlines how to secure internal Kubernetes services by integrating OAuth2 Proxy, Traefik, and Pocket ID, leveraging the Gateway API for traffic routing.
The previous method relied on ingress-nginx annotations to configure OAuth2 Proxy. This approach is becoming obsolete as ingress-nginx faces retirement. The Gateway API offers a more standardized and extensible way to manage ingress traffic. It decouples the gateway infrastructure from the routing rules, allowing for greater flexibility and vendor neutrality. While Gateway API standardizes resources like Gateway and HTTPRoute, it doesn't inherently provide solutions for browser-based OIDC login flows or external authentication filters. This is where Traefik comes in. Traefik's Middleware Custom Resource Definition (CRD) is utilized to handle these specific authentication requirements, acting as the glue between the Gateway API's routing capabilities and OAuth2 Proxy's authentication logic.
This setup exposes three HTTPS hostnames under a single domain, each protected by OAuth2 Proxy and Pocket ID for authentication. The core components include:
- Gateway API: Provides the standardized ingress point and traffic routing rules.
- Traefik: Acts as the Gateway API implementation, specifically handling authentication via its
MiddlewareCRD. - OAuth2 Proxy: The primary authentication service that intercepts requests and redirects unauthenticated users to an OIDC provider.
- Pocket ID: Serves as the OpenID Connect (OIDC) provider, handling user authentication and issuing tokens.
The advantage of this architecture is that the core Gateway API resources (Gateway and HTTPRoute) remain consistent even if you switch to a different Gateway API implementation like Envoy Gateway, Kong, or Cilium. The authentication adapter (Traefik in this case) would be the component that needs to be swapped out.
Architectural Overview and Configuration
The proposed architecture involves deploying OAuth2 Proxy and Pocket ID within the Kubernetes cluster. Traefik, configured to implement the Gateway API, will be responsible for routing external traffic. When a request arrives, Traefik will apply a Middleware that directs users to OAuth2 Proxy for authentication. If authentication is successful, OAuth2 Proxy will allow the request to proceed to the target internal service.
The configuration for OAuth2 Proxy requires specific parameters to integrate with Pocket ID. This includes setting the client ID, client secret, cookie secret, and the redirect URL. The redirect URL must point to the OAuth2 Proxy service within the cluster. The OIDC discovery endpoint and token endpoint for Pocket ID need to be correctly specified. For Pocket ID, this typically involves endpoints like https://pocket.id/oauth2/auth and https://pocket.id/oauth2/token.
Traefik's Middleware CRD will be configured to leverage an external authentication service. This external service is essentially the OAuth2 Proxy instance. The HTTPRoute resources will then reference this Middleware, ensuring that all traffic matching the route is first subjected to the authentication process.

Implementing the Authentication Flow
The authentication flow begins when a user attempts to access a protected Kubernetes service via one of the three defined HTTPS hostnames. Traefik, acting as the gateway, intercepts the request.
- Request Interception: Traefik receives the incoming HTTPS request.
- Middleware Application: The associated
HTTPRoutereferences aMiddlewarethat is configured to use an external authentication service. - Redirection to OAuth2 Proxy: Traefik forwards the request to the OAuth2 Proxy service. If the user's session cookie is not present or invalid, OAuth2 Proxy redirects the user's browser to the Pocket ID authorization endpoint.
- OIDC Authentication: The user authenticates with Pocket ID.
- Callback to OAuth2 Proxy: Upon successful authentication, Pocket ID redirects the user back to the OAuth2 Proxy's callback URL with an authorization code.
- Token Exchange: OAuth2 Proxy exchanges the authorization code for an access token and ID token with Pocket ID.
- Session Creation: OAuth2 Proxy creates a secure session cookie for the user.
- Request Forwarding: OAuth2 Proxy adds relevant user information (like email or username) as headers to the original request and forwards it to the intended internal Kubernetes service.
- Service Response: The internal service receives the authenticated request and sends back its response, which Traefik then relays to the user.
This process ensures that only authenticated users can access the internal services. The use of Pocket ID simplifies the OIDC provider management, offering a secure and convenient solution for identity verification.
Benefits and Considerations
This setup provides several advantages. Firstly, it aligns with Kubernetes' recommended direction for traffic management by adopting the Gateway API. Secondly, it leverages the flexibility of Traefik's Middleware to integrate external authentication services, making the solution adaptable. Thirdly, using OAuth2 Proxy with a dedicated OIDC provider like Pocket ID offers a robust and standard-compliant authentication mechanism.
One of the key benefits is the clear separation of concerns. Gateway API handles routing, Traefik handles the gateway implementation and middleware application, and OAuth2 Proxy handles the authentication logic. This modularity makes the system easier to understand, maintain, and upgrade.
However, there are considerations. The complexity of managing multiple Kubernetes CRDs (Gateway, HTTPRoute, Middleware, and the OAuth2 Proxy deployment) requires a good understanding of Kubernetes networking and security. The choice of Gateway API implementation is critical, as the specific mechanism for applying external authentication might differ. For instance, while Traefik uses Middleware, other implementations might rely on different CRDs or annotations.
The surprising detail here is not the reliance on OAuth2 Proxy, which is a well-established tool, but the explicit move away from ingress-nginx-specific configurations towards a more generalized Gateway API approach. This signals a broader industry trend towards standardized ingress controllers and a desire for more declarative traffic management in Kubernetes environments.
The Future of Kubernetes Authentication
As Kubernetes continues to evolve, the Gateway API is poised to become the de facto standard for ingress traffic management. This shift necessitates that authentication solutions adapt accordingly. Architectures like the one described, which combine Gateway API with flexible authentication proxies and modern OIDC providers, represent the future. They offer a scalable, secure, and maintainable way to protect internal services.
What nobody has addressed yet is the long-term maintenance burden for teams managing these complex, multi-component authentication flows. While powerful, this setup requires ongoing vigilance to ensure all components remain updated and secure. The potential for misconfiguration across the various layers—Gateway API, Traefik, OAuth2 Proxy, and Pocket ID—is significant, and robust monitoring and testing strategies will be essential.
For developers and operations teams, understanding these new patterns is crucial. Embracing Gateway API and integrating it with robust authentication solutions like OAuth2 Proxy and dedicated identity providers will be key to building secure and resilient applications on Kubernetes.
