> ## Documentation Index
> Fetch the complete documentation index at: https://ngrok.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Pooling Endpoints in the ngrok Kubernetes Operator

> Learn how to load balance traffic between endpoints using the ngrok Kubernetes Operator.

Endpoint pooling is the concept of creating two or more endpoints that share the same URL and have pooling enabled (if you don't enable pooling for an endpoint it cannot have the same URL as another endpoint).
The [endpoint pooling page](/gateway/endpoint-pooling/) goes over concepts and details of pooling in more depth.
This guide focuses on how to use and configure endpoint pooling while using the ngrok Kubernetes Operator.

Note that while endpoint pooling is supported by the ngrok Kubernetes Operator, endpoint pools are not specific to the environment in which they were created.
This means that you can pool endpoints created with the Kubernetes Operator with endpoints started using the CLI, etc. This is a powerful and important concept to keep in mind to avoid unwanted pooling.

<Note>
  Pooling must be enabled by all endpoints that share the same URL. Any attempt to create a pooled endpoint for a URL already in-use by a non-pooled endpoint will fail.
  Similarly, any endpoint attempting to create an endpoint that does not enable pooling and uses a URL that is already in-use by one or more endpoints with pooling enabled will fail.
</Note>

## Endpoint Pooling with `AgentEndpoints`

Within the Kubernetes Operator, `AgentEndpoint` resources are currently **Always Pooled**.
This means that if you create the same `AgentEndpoint` in two different Kubernetes clusters, or with a URL shared by any other type of endpoint that has pooling enabled, then they will all be part of the same pool.
When the Kubernetes Operator creates `AgentEndpoint` resources for you by translating `Ingress` and Gateway API resources, it will prefix the generated `AgentEndpoint` URL with a special hash to avoid unintended pooling.

## Endpoint Pooling with `CloudEndpoints`

`CloudEndpoint` resources default to having pooling disabled, but you can enable it with the `spec.poolingEnabled: true` field such as with the following example.

```yaml theme={null}
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: NgrokTrafficPolicy
metadata:
  name: static-response
  namespace: default
spec:
  policy:
    on_http_request:
    - name: example-rule
      actions:
      - type: custom-response
        config:
          body: Example response from Traffic Policy
          headers:
            content-type: text/plain
          status_code: 200
---
apiVersion: ngrok.k8s.ngrok.com/v1alpha1
kind: CloudEndpoint
metadata:
  name: example-cloud-endpoint
spec:
  poolingEnabled: true
  url: https://example-cloud-endpoint.ngrok.io
  trafficPolicyName: static-response
```

## Endpoint Pooling with `Ingresses`

If you are using `Ingress` resources with the Operator, you can enable endpoint pooling for an `Ingress` using the `k8s.ngrok.com/pooling-enabled: "true"` annotation.
The following will create an `Ingress` that creates a `CloudEndpoint` with pooling enabled. Any other endpoint created with the same URL and pooling enabled will be pooled with the `CloudEndpoint` that this `Ingress` creates.

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    k8s.ngrok.com/pooling-enabled: "true"
  name: example-ingress
  namespace: default
spec:
  ingressClassName: ngrok
  rules:
    - host: example-ingresses.ngrok.io
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: example-service
                port:
                  number: 80
```

Since the Operator collapses `Ingress` resources that share the same hostname, you must make sure that all `Ingress` resources that use a given hostname use the same pooling enabled or disabled configuration.
See the [using Ingresses guide](/k8s/guides/using-ingresses) for more information.

## Endpoint Pooling with Gateway API

If you are using Gateway API for configuration, you can enable pooling using the using the `k8s.ngrok.com/pooling-enabled: "true"` annotation on `Gateway` resources.
The following will create a `CloudEndpoint` with pooling enabled. Any other endpoint created with the same URL and pooling enabled will be pooled with the `CloudEndpoint`.

```yaml theme={null}
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: example-gateway
  namespace: default
  annotations:
    k8s.ngrok.com/pooling-enabled: "true"
spec:
  gatewayClassName: ngrok
  listeners:
    - name: example-hostname
      hostname: "example-hostname.ngrok.io"
      port: 443
      protocol: HTTPS
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: default
spec:
  hostnames:
  - example-hostname.ngrok.io
  parentRefs:
  - group: gateway.networking.k8s.io
    kind: Gateway
    name: example-gateway
    namespace: default
  rules:
  - matches:
      - path:
          type: PathPrefix
          value: /
    backendRefs:
      - group: ""
        kind: Service
        name: example-service
        port: 80
        weight: 1
```
