Aller au contenu
Docs Try Aspire
Docs Try

Expose services with Ingress and Gateway API

Ce contenu n’est pas encore disponible dans votre langue.

When you deploy an Aspire application to Kubernetes, your services are accessible only within the cluster by default. To expose services to external traffic — such as a web frontend that users access from the internet — you need to configure an Ingress or Gateway resource that routes traffic from outside the cluster to your services.

Aspire provides first-class APIs for defining Ingress and Gateway API resources directly in your AppHost, generating the appropriate Kubernetes YAML as part of the Helm chart output.

Kubernetes offers two approaches for routing external traffic to your services:

IngressGateway API
MaturityLegacy (stable since K8S 1.19)Modern (GA since K8S 1.27)
Resource modelSingle Ingress resourceThree-tier: GatewayClassGatewayHTTPRoute
Protocol supportHTTP/HTTPS onlyHTTP, HTTPS, TCP, UDP, gRPC
Routing featuresHost and path matchingHost, path, header, query, method matching
ExtensibilityController-specific annotationsStandardized filters and policies
Traffic splittingNot standardizedFirst-class weighted backends
Aspire APIAddIngress()AddGateway()

Recommendation: Use Gateway API for new deployments. It’s the future direction of Kubernetes traffic management and offers richer routing capabilities with better portability across providers. Use Ingress when your cluster or ingress controller doesn’t support the Gateway API yet.

When you use AddIngress() or AddGateway(), Aspire generates additional Kubernetes resources in the Helm chart output:

For Ingress (AddIngress):

  • A networking.k8s.io/v1 Ingress resource with rules, TLS configuration, and controller annotations

For Gateway API (AddGateway):

  • A gateway.networking.k8s.io/v1 Gateway resource with listeners (HTTP on port 80, HTTPS on port 443 if TLS is configured)
  • One or more HTTPRoute resources attached to the Gateway, grouped by hostname

When you configure TLS with WithTls(), Aspire handles the initial bootstrapping:

  1. First deploy: Aspire generates a self-signed placeholder TLS certificate and creates the Kubernetes Secret. This makes the Ingress/Gateway valid immediately so the load balancer can start routing traffic.

  2. Certificate replacement: You then configure a certificate management solution (such as cert-manager) to replace the self-signed certificate with a real one from a trusted CA like Let’s Encrypt or ZeroSSL.

  3. Subsequent deploys: The existing Secret (with the real certificate) is preserved — the bootstrap step only creates the Secret if it doesn’t already exist.

Using parameters for deployment configuration

Section titled “Using parameters for deployment configuration”

Aspire’s Ingress and Gateway APIs accept parameters, allowing you to reuse the same AppHost across multiple deployment environments (development, staging, production) with different hostnames, TLS secrets, and controller configurations:

AppHost.cs
var hostname = builder.AddParameter("hostname");
var ingressClass = builder.AddParameter("ingressclass");
var ingress = k8s.AddIngress("ingress")
.WithIngressClass(ingressClass)
.WithHostname(hostname)
.WithTls();

Parameter values are provided at deploy time via the Aspire CLI or configuration files, keeping environment-specific details out of your source code.

You can omit WithTls() to deploy without TLS. The Ingress/Gateway will serve traffic over HTTP only. This is useful for quick testing but should never be used in production.

Choose the walkthrough that matches your deployment approach:

  • Configure Gateway API on AKS (Recommended) — Use the Kubernetes Gateway API with Application Gateway for Containers on AKS. Supports cert-manager HTTP-01 for automatic TLS with no DNS API or DNS zone required.

  • Configure Ingress on AKS — Use the Kubernetes Ingress API with Application Gateway for Containers on AKS. Requires cert-manager with DNS-01 for automatic TLS.