# Integrate with Kubernetes Service Discovery

Kubernetes tracks the ready pod addresses behind each Service through Endpoints or EndpointSlices. APISIX can watch these resources and route traffic directly to changing pod addresses instead of using a Service virtual IP or cluster DNS name.

The integration supports one Kubernetes cluster or multiple named clusters. APISIX authenticates to each Kubernetes API with a service-account bearer token and makes the discovered addresses available to upstreams.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* An APISIX deployment that can connect to the Kubernetes API server and discovered pod addresses.
* A Kubernetes ServiceAccount token, supplied directly with `client.token` or through `client.token_file`.
* `get`, `list`, and `watch` permissions for `endpoints`. Add the same permissions for `endpointslices` in the `discovery.k8s.io` API group when `watch_endpoint_slices` is `true`.
* The Kubernetes API server CA certificate in a PEM bundle available to APISIX when TLS verification is enabled.
* If you use the Admin API examples and authentication is enabled, export an admin-role key as `ADMIN_API_KEY`. Configure the key under `deployment.admin.admin_key`. The Docker quickstart disables authentication for local evaluation; keep it enabled in production.

## Grant Kubernetes API Access[​](#grant-kubernetes-api-access "Direct link to Grant Kubernetes API Access")

The following manifest creates the `apisix` namespace and grants the required discovery permissions. If APISIX runs in another namespace, replace `apisix` in the Namespace, ServiceAccount, and ClusterRoleBinding subject.

kubernetes-discovery-rbac.yaml

```
apiVersion: v1

kind: Namespace

metadata:

  name: apisix

---

apiVersion: v1

kind: ServiceAccount

metadata:

  name: apisix-discovery

  namespace: apisix

---

apiVersion: rbac.authorization.k8s.io/v1

kind: ClusterRole

metadata:

  name: apisix-discovery

rules:

  - apiGroups: [""]

    resources: ["endpoints"]

    verbs: ["get", "list", "watch"]

  - apiGroups: ["discovery.k8s.io"]

    resources: ["endpointslices"]

    verbs: ["get", "list", "watch"]

---

apiVersion: rbac.authorization.k8s.io/v1

kind: ClusterRoleBinding

metadata:

  name: apisix-discovery

roleRef:

  apiGroup: rbac.authorization.k8s.io

  kind: ClusterRole

  name: apisix-discovery

subjects:

  - kind: ServiceAccount

    name: apisix-discovery

    namespace: apisix
```

Apply the RBAC resources:

```
kubectl apply -f kubernetes-discovery-rbac.yaml
```

When APISIX runs in Kubernetes and reads the mounted service-account token, set `spec.template.spec.serviceAccountName` in the APISIX Deployment or StatefulSet to `apisix-discovery`. Applying the RBAC manifest does not change the identity of existing pods. Roll out the workload after updating its pod specification.

## Configure a Single Kubernetes Cluster[​](#configure-a-single-kubernetes-cluster "Direct link to Configure a Single Kubernetes Cluster")

When APISIX runs in a pod, the default configuration reads the Kubernetes API address and mounted service-account token from the pod environment:

config.yaml

```
discovery:

  kubernetes: {}
```

For production, explicitly enable certificate verification. Create and mount a PEM bundle that contains the Kubernetes API CA together with every system or custom CA that APISIX already trusts. The `apisix.ssl.ssl_trusted_certificate` setting is global, so replacing an existing bundle with the Kubernetes CA alone can break other TLS connections. Keep the API server hostname consistent with its certificate.

config.yaml

```
apisix:

  ssl:

    ssl_trusted_certificate: /usr/local/apisix/conf/trusted-ca-bundle.pem



discovery:

  kubernetes:

    shared_size: 64m

    service:

      schema: https

      host: ${KUBERNETES_SERVICE_HOST}

      port: ${KUBERNETES_SERVICE_PORT}

      ssl_verify: true

    client:

      token_file: /var/run/secrets/kubernetes.io/serviceaccount/token

    namespace_selector:

      equal: prod-apps

    watch_endpoint_slices: true
```

`service.ssl_verify` defaults to `false` for compatibility, including when `service.schema` is `https`. This accepts an untrusted API server certificate and is not recommended for production. Set it to `true` and configure `apisix.ssl.ssl_trusted_certificate` with the combined trust bundle.

`shared_size` controls the shared memory available to store discovered endpoints and defaults to `64m`. The zone is shared across workers. Increase it when endpoint inventory exceeds the available zone, and include the configured size in the gateway instance's memory budget.

When APISIX runs outside Kubernetes, set `service.host` and `service.port` explicitly. Supply either `client.token` or a readable `client.token_file`; HTTPS API servers require a nonempty token.

## Configure Multiple Kubernetes Clusters[​](#configure-multiple-kubernetes-clusters "Direct link to Configure Multiple Kubernetes Clusters")

Use an array to configure multiple clusters. Each item requires a unique `id` containing 1–64 lowercase letters or digits, an explicit API server address, and client credentials. Defaults from the single-cluster service and client fields are not applied to array items.

config.yaml

```
apisix:

  ssl:

    ssl_trusted_certificate: /usr/local/apisix/conf/trusted-ca-bundle.pem



discovery:

  kubernetes:

    - id: prod

      shared_size: 64m

      service:

        schema: https

        host: prod-api.example.com

        port: "6443"

        ssl_verify: true

      client:

        token_file: /usr/local/apisix/conf/prod-service-account.token

      namespace_selector:

        match:

          - ^prod-.*

      watch_endpoint_slices: true

    - id: staging

      shared_size: 64m

      service:

        schema: https

        host: staging-api.example.com

        port: "6443"

        ssl_verify: true

      client:

        token_file: /usr/local/apisix/conf/staging-service-account.token

      namespace_selector:

        equal: prod-apps

      watch_endpoint_slices: true
```

The CA bundle must trust every configured API server and retain the roots required by other HTTPS connections from APISIX. After changing discovery or trust settings, [reload APISIX](https://docs.api7.ai/apisix/reference/apisix-cli.md#apisix-reload).

## Deploy a Sample Service[​](#deploy-a-sample-service "Direct link to Deploy a Sample Service")

Deploy an HTTPBin workload and a Service with a named `http` port in the `prod-apps` namespace:

httpbin.yaml

```
apiVersion: v1

kind: Namespace

metadata:

  name: prod-apps

---

apiVersion: apps/v1

kind: Deployment

metadata:

  name: httpbin

  namespace: prod-apps

spec:

  replicas: 2

  selector:

    matchLabels:

      app: httpbin

  template:

    metadata:

      labels:

        app: httpbin

    spec:

      containers:

        - name: httpbin

          image: mccutchen/go-httpbin:v2.15.0

          ports:

            - name: http

              containerPort: 8080

---

apiVersion: v1

kind: Service

metadata:

  name: httpbin

  namespace: prod-apps

spec:

  selector:

    app: httpbin

  ports:

    - name: http

      port: 80

      targetPort: http
```

Apply the resources and wait for both pods to become ready:

```
kubectl apply -f httpbin.yaml

kubectl rollout status deployment/httpbin -n prod-apps
```

## Route to a Discovered Service[​](#route-to-a-discovered-service "Direct link to Route to a Discovered Service")

For a single cluster, use `namespace/service:port-name` as `service_name`. In multiple-cluster mode, you can either target one cluster with `cluster-id/namespace/service:port-name` or select several clusters through `discovery_args.cluster_ids` while keeping the unprefixed `namespace/service:port-name` format. If the Kubernetes resource defines a named port, use the port name rather than its number.

Create a route that sends requests to the `httpbin` Service in the `prod-apps` namespace through its `http` port:

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "kubernetes-discovery-route",

    "uri": "/anything",

    "upstream": {

      "type": "roundrobin",

      "discovery_type": "kubernetes",

      "service_name": "prod-apps/httpbin:http"

    }

  }'
```

If you configured multiple clusters, change `service_name` to `prod/prod-apps/httpbin:http` to target the `prod` cluster.

Send a request to verify that APISIX resolves a ready endpoint and proxies the request:

```
curl -i "http://127.0.0.1:9080/anything"
```

An `HTTP/1.1 200 OK` response confirms that the route reached a discovered endpoint.

### Select Kubernetes Clusters[​](#select-kubernetes-clusters "Direct link to Select Kubernetes Clusters")

To combine endpoints from selected clusters, apply the existing `httpbin.yaml` with the `kubectl` context for both the `prod` and `staging` clusters. Both discovery configurations above admit the `prod-apps` namespace. The following route selects `prod-apps/httpbin:http` endpoints from those clusters:

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "kubernetes-selected-clusters-route",

    "uri": "/anything-multi",

    "upstream": {

      "type": "roundrobin",

      "discovery_type": "kubernetes",

      "service_name": "prod-apps/httpbin:http",

      "discovery_args": {

        "cluster_ids": ["prod", "staging"]

      }

    }

  }'
```

Send a request through the route that selects both clusters:

```
curl -i "http://127.0.0.1:9080/anything-multi"
```

An `HTTP/1.1 200 OK` response confirms that APISIX resolved a ready endpoint from the selected clusters. Use the discovery dump in the next section to confirm that both clusters contributed the expected endpoints.

## Verify Discovery and TLS Trust[​](#verify-discovery-and-tls-trust "Direct link to Verify Discovery and TLS Trust")

If the [Control API](https://docs.api7.ai/apisix/reference/control-api.md) is enabled, inspect the discovery cache:

```
curl "http://127.0.0.1:9090/v1/discovery/kubernetes/dump"
```

Confirm that the output contains the expected namespace, service, port name, and ready endpoint addresses. With multiple clusters, also confirm the expected cluster `id`.

Each worker caches a resolved service or missing-service result for one second after reading it. The next lookup after expiry reads the latest shared discovery state, so allow for this worker-local cache when verifying endpoint changes.

To verify trusted-certificate behavior:

1. Configure `service.ssl_verify: true` and the correct CA bundle, reload APISIX, and confirm the expected endpoints appear in the discovery dump.
2. In an isolated test deployment, point `apisix.ssl.ssl_trusted_certificate` to a bundle that does not trust the API server, or use a host that does not match the certificate. Reload APISIX and confirm the error log reports certificate verification failure and no fresh endpoint updates are received. Previously discovered entries can remain in shared memory during a reload, so do not use the presence of an old entry as proof that the new TLS connection succeeded.
3. Restore the correct CA bundle and host, reload APISIX, and confirm discovery recovers.

Do not use `ssl_verify: false` as the fix for a production trust failure. Correct the CA bundle, certificate chain, API server hostname, or system time instead.

## Next Steps[​](#next-steps "Direct link to Next Steps")

Kubernetes discovery supports namespace selectors, Kubernetes label-selector expressions, configurable endpoint weights, shared-memory sizing, and either Endpoints or EndpointSlices. For the complete discovery state and troubleshooting endpoints, see the [Control API reference](https://docs.api7.ai/apisix/reference/control-api.md#tag/Service-Discovery).
