Deploy Kong AI Gateway with Kong Operator

TL;DR

With Konnect, create a KonnectAIGateway, store your provider API key in a Kubernetes Secret, add an AIGatewayModelProvider and AIGatewayModel, then deploy an AIGatewayDataPlane. Kong Operator provisions the mTLS certificate automatically. For on-prem (Tech Preview), create an OnPremAIGateway and an AIGatewayDataPlane that references it, then add the same provider and model resources with spec.aiGatewayRef.kind: OnPremAIGateway. Kong Operator pushes the configuration to the data plane directly.

Prerequisites

This page is part of the Deploy Kong AI Gateway on Kubernetes series.

Complete the previous page, Install Kong Operator for Kong AI Gateway before completing this page.

If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:
    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Set the personal access token as an environment variable:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'
kubectl create namespace kong --dry-run=client -o yaml | kubectl apply -f -
echo '
kind: KonnectAPIAuthConfiguration
apiVersion: konnect.konghq.com/v1alpha1
metadata:
  name: konnect-api-auth
  namespace: kong
spec:
  type: token
  token: "'$KONNECT_TOKEN'"
  serverURL: us.api.konghq.com
' | kubectl apply -f -

This tutorial uses OpenAI:

  1. Create an OpenAI account.
  2. Get an API key.
  3. Export your API key as an environment variable:

    export OPENAI_API_KEY='YOUR_OPENAI_API_KEY'

This guide deploys a full AI Gateway stack on Kubernetes using Kong Operator.

Deploying on-prem Kong AI Gateway 2.x with Kong Operator is in Tech Preview. Running Kong AI Gateway with an OnPremAIGateway control plane is not supported for production use, and its resources and behavior may change between releases. It requires Kong Operator 2.4.0-rapid.3.0 or later and a Kong Gateway Enterprise license.

Create the KonnectAIGateway

The KonnectAIGateway resource creates the AI Gateway control plane in Konnect and serves as the parent for all other resources in this guide.

  1. Create the KonnectAIGateway resource:

    echo '
    apiVersion: konnect.konghq.com/v1alpha1
    kind: KonnectAIGateway
    metadata:
      name: my-ai-gateway-cp
      namespace: kong
    spec:
      apiSpec:
        name: my-ai-gateway-cp
        displayName: My AI Gateway
        description: AI Gateway control plane managed by Kubernetes
      konnect:
        authRef:
          name: konnect-api-auth
    ' | kubectl apply -f -
  2. Wait for the resource to be ready:

    kubectl wait konnectaigateway/my-ai-gateway-cp -n kong \
      --for=condition=Programmed=True \
      --timeout=10m

Create the OnPremAIGateway

The OnPremAIGateway resource is the on-premises AI Gateway control plane, and serves as the parent for all other resources in this guide. It doesn’t create a Deployment or any Pods. Kong Operator runs a control plane instance for it inside the operator, which collects the configuration resources that reference the OnPremAIGateway.

  1. Create the OnPremAIGateway resource:

    echo '
    apiVersion: aigateway.konghq.com/v1alpha1
    kind: OnPremAIGateway
    metadata:
      name: my-ai-gateway-cp
      namespace: kong
    spec: {}
    ' | kubectl apply -f -

    By default, the OnPremAIGateway only watches its own namespace for configuration resources and the Secrets they reference. See On-premises control plane to watch other namespaces or tune how often configuration is pushed.

  2. Wait for the resource to be ready. Unlike KonnectAIGateway, an OnPremAIGateway reports a Ready condition rather than Programmed:

    kubectl wait onpremaigateway/my-ai-gateway-cp -n kong \
      --for=condition=Ready=True \
      --timeout=5m

Deploy the AIGatewayDataPlane

The AIGatewayDataPlane resource deploys the AI Gateway to your Kubernetes cluster. It exposes a LoadBalancer Service on port 8000 for inference requests.

  1. Deploy the AIGatewayDataPlane. Set spec.controlPlaneRef.type to onpremNamespacedRef to attach it to the OnPremAIGateway:

    echo '
    apiVersion: aigateway.konghq.com/v1alpha1
    kind: AIGatewayDataPlane
    metadata:
      name: my-ai-gateway-dp
      namespace: kong
    spec:
      controlPlaneRef:
        type: onpremNamespacedRef
        onpremNamespacedRef:
          name: my-ai-gateway-cp
      deployment:
        replicas: 1
      network:
        services:
          ingress:
            type: LoadBalancer
            ports:
              - name: http
                port: 8000
                targetPort: 8000
    ' | kubectl apply -f -
  2. Wait for the data plane to be ready:

    kubectl wait aigatewaydataplane/my-ai-gateway-dp -n kong \
      --for=condition=Ready=True \
      --timeout=10m
  3. List the Services Kong Operator created for the data plane:

    kubectl get service -n kong

Create an AI Model Provider

The AIGatewayModelProvider resource configures authentication and connection details for an upstream LLM provider. This example uses OpenAI.

  1. Store your OpenAI API key in a Kubernetes Secret. The value includes the Bearer prefix because it is used directly as an HTTP Authorization header:

    kubectl create secret generic openai-credentials \
      --from-literal=token="Bearer ${OPENAI_API_KEY}" \
      -n kong
    kubectl label secret openai-credentials konghq.com/secret=true -n kong

    Kong Operator only reads Secrets that carry the konghq.com/secret=true label. Without it, the operator can’t see the Secret and the AI Model Provider doesn’t become Programmed.

  2. Create the AIGatewayModelProvider resource, referencing the Secret:

    echo '
    apiVersion: aiconfiguration.konghq.com/v1alpha1
    kind: AIGatewayModelProvider
    metadata:
      name: openai-provider
      namespace: kong
    spec:
      aiGatewayRef:
        type: namespacedRef
        namespacedRef:
          name: my-ai-gateway-cp
      apiSpec:
        type: openai
        openai:
          name: openai-provider
          displayName: OpenAI
          config:
            auth:
              headers:
                - name: Authorization
                  value:
                    type: secretRef
                    secretRef:
                      name: openai-credentials
                      key: token
    ' | kubectl apply -f -
    echo '
    apiVersion: aiconfiguration.konghq.com/v1alpha1
    kind: AIGatewayModelProvider
    metadata:
      name: openai-provider
      namespace: kong
    spec:
      aiGatewayRef:
        group: aigateway.konghq.com
        kind: OnPremAIGateway
        namespacedRef:
          name: my-ai-gateway-cp
      apiSpec:
        type: openai
        openai:
          name: openai-provider
          displayName: OpenAI
          config:
            auth:
              headers:
                - name: Authorization
                  value:
                    type: secretRef
                    secretRef:
                      name: openai-credentials
                      key: token
    ' | kubectl apply -f -
  3. Wait for the resource to be ready:

    kubectl wait aigatewaymodelprovider/openai-provider -n kong \
      --for=condition=Programmed=True \
      --timeout=10m

Create an AI Model

The AIGatewayModel resource defines a Route and maps it to one or more provider targets. Clients send inference requests to the path configured here.

  1. Create the AIGatewayModel resource:

    echo '
    apiVersion: aiconfiguration.konghq.com/v1alpha1
    kind: AIGatewayModel
    metadata:
      name: gpt-4o-mini
      namespace: kong
    spec:
      aiGatewayRef:
        type: namespacedRef
        namespacedRef:
          name: my-ai-gateway-cp
      apiSpec:
        type: model
        model:
          name: gpt-4o-mini
          displayName: GPT-4o Mini
          enabled: Enabled
          formats:
            - type: openai
          capabilities:
            - generate
          config:
            route:
              paths:
                - /v1
          targets:
            - name: gpt-4o-mini
              provider:
                name: openai-provider
              config:
                type: openai
                openai:
                  upstreamURL: https://api.openai.com/v1/chat/completions
    ' | kubectl apply -f -
    echo '
    apiVersion: aiconfiguration.konghq.com/v1alpha1
    kind: AIGatewayModel
    metadata:
      name: gpt-4o-mini
      namespace: kong
    spec:
      aiGatewayRef:
        group: aigateway.konghq.com
        kind: OnPremAIGateway
        namespacedRef:
          name: my-ai-gateway-cp
      apiSpec:
        type: model
        model:
          name: gpt-4o-mini
          displayName: GPT-4o Mini
          enabled: Enabled
          formats:
            - type: openai
          capabilities:
            - generate
          config:
            route:
              paths:
                - /v1
          targets:
            - name: gpt-4o-mini
              provider:
                name: openai-provider
              config:
                type: openai
                openai:
                  upstreamURL: https://api.openai.com/v1/chat/completions
    ' | kubectl apply -f -
  2. Wait for the resource to be ready:

    kubectl wait aigatewaymodel/gpt-4o-mini -n kong \
      --for=condition=Programmed=True \
      --timeout=10m
  1. Confirm that Kong Operator pushed the configuration to the data plane:

    kubectl get onpremaigateway/my-ai-gateway-cp -n kong \
      -o jsonpath='{.status.conditions[?(@.type=="DataPlanesConfigured")].message}'

    The command prints Configuration pushed to 1 Admin API endpoints, one endpoint for each data plane replica.

Deploy the AIGatewayDataPlane

The AIGatewayDataPlane resource runs the AI Gateway binary inside your Kubernetes cluster. It exposes a LoadBalancer Service on port 8000 for inference requests.

  1. Deploy the AIGatewayDataPlane:

    echo '
    apiVersion: aigateway.konghq.com/v1alpha1
    kind: AIGatewayDataPlane
    metadata:
      name: my-ai-gateway-dp
      namespace: kong
    spec:
      controlPlaneRef:
        type: konnectNamespacedRef
        konnectNamespacedRef:
          name: my-ai-gateway-cp
      deployment:
        replicas: 1
      network:
        services:
          ingress:
            type: LoadBalancer
            ports:
              - name: http
                port: 8000
                targetPort: 8000
    ' | kubectl apply -f -
  2. Wait for the data plane to be ready:

    kubectl wait aigatewaydataplane/my-ai-gateway-dp -n kong \
      --for=condition=Ready=True \
      --timeout=10m

Validate

Export the LoadBalancer address:

export AIGW_HOST=$(kubectl get service my-ai-gateway-dp-ingress -n kong \
  -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo $AIGW_HOST

Send a request to the AI Model Route you configured:

curl -X POST "http://$AIGW_HOST:8000/v1/chat/completions" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json" \
     --json '{
       "model": "gpt-4o-mini",
       "messages": [
         {
           "role": "user",
           "content": "Hello from Kong AI Gateway!"
         }
       ]
     }'
curl -X POST "http://$AIGW_HOST:8000/v1/chat/completions" \
     --no-progress-meter --fail-with-body  \
     -H "Content-Type: application/json" \
     --json '{
       "model": "gpt-4o-mini",
       "messages": [
         {
           "role": "user",
           "content": "Hello from Kong AI Gateway!"
         }
       ]
     }'

You should receive a response from OpenAI routed through the AI Gateway data plane.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!