This deployment pattern uses a single TLS listener at the Kubernetes edge and routes Kafka traffic by SNI inside Kong Event Gateway. For when to use this pattern, see Deployment patterns.
Deploy Kong Event Gateway with TLSRoute and SNI
Front Event Gateway with a Gateway and TLSRoute, terminate TLS in Event Gateway, and route virtual clusters with SNI.
For on-prem, attach the Event Gateway resources to an OnPremEventGateway, and apply a KongLicense for the Kong Gateway data plane behind the Gateway.
Prerequisites
Series Prerequisites
This page is part of the Deploy Kong Event Gateway on Kubernetes series.
Complete the previous page, Deploy Kong Event Gateway with port mapping before completing this page.
Kong Konnect
If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.
- 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.
-
Set the personal access token as an environment variable:
export KONNECT_TOKEN='YOUR KONNECT TOKEN'
Kong Gateway Enterprise license
With an on-prem control plane, the Kong Gateway data plane behind the Gateway needs a Kong Gateway Enterprise license to accept configuration. Without one, it runs in read-only mode and the Gateway never becomes Programmed.
Save your license as license.json in your working directory. This is a different license from the Event Gateway license that the control plane uses.
Create a GatewayConfiguration and GatewayClass
Create a managed Gateway class for the TLSRoute example. Kong Operator provisions this Gateway through Konnect, so the GatewayConfiguration references the KonnectAPIAuthConfiguration that you created in Deploy Kong Event Gateway with port mapping.
-
Create the
GatewayConfigurationandGatewayClassresources:echo ' apiVersion: gateway-operator.konghq.com/v2beta1 kind: GatewayConfiguration metadata: name: kong-configuration namespace: kong spec: konnect: authRef: name: konnect-api-auth dataPlaneOptions: deployment: podTemplateSpec: spec: containers: - name: proxy image: kong/kong-gateway:3.16 --- apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: kong spec: controllerName: konghq.com/gateway-operator parametersRef: group: gateway-operator.konghq.com kind: GatewayConfiguration name: kong-configuration namespace: kong ' | kubectl apply -f - -
Wait for the
GatewayClassto be accepted:kubectl wait gatewayclass/kong \ --for=condition=Accepted=True \ --timeout=5m
Create a GatewayConfiguration and GatewayClass
Create a managed Gateway class for the TLSRoute example. Without spec.konnect, Kong Operator runs the control plane of the Gateway itself.
-
Apply a
KongLicensefor the Kong Gateway data plane, from thelicense.jsonfile in your working directory:echo " apiVersion: configuration.konghq.com/v1alpha1 kind: KongLicense metadata: name: kong-license rawLicenseString: '$(cat ./license.json)' " | kubectl apply -f - -
Create the
GatewayConfigurationandGatewayClassresources:echo ' apiVersion: gateway-operator.konghq.com/v2beta1 kind: GatewayConfiguration metadata: name: kong-configuration namespace: kong spec: dataPlaneOptions: deployment: podTemplateSpec: spec: containers: - name: proxy image: kong/kong-gateway:3.16 --- apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: kong spec: controllerName: konghq.com/gateway-operator parametersRef: group: gateway-operator.konghq.com kind: GatewayConfiguration name: kong-configuration namespace: kong ' | kubectl apply -f - -
Wait for the
GatewayClassto be accepted:kubectl wait gatewayclass/kong \ --for=condition=Accepted=True \ --timeout=5m
Create a second KonnectEventGateway
This guide uses a separate control plane so that its resources can run alongside the resources from the portMapping example.
-
Create the
KonnectEventGatewayresource:echo ' apiVersion: konnect.konghq.com/v1alpha1 kind: KonnectEventGateway metadata: name: cp-event-tls namespace: kong spec: apiSpec: name: cp-event-tls description: Event Gateway control plane for TLSRoute and SNI konnect: authRef: name: konnect-api-auth ' | kubectl apply -f - -
Wait for the resource to be ready:
kubectl wait konnecteventgateway/cp-event-tls -n kong \ --for=condition=Programmed=True \ --timeout=10m
Create a second OnPremEventGateway
This guide uses a separate OnPremEventGateway so that its resources can run alongside the resources from the portMapping example. The same control plane serves both OnPremEventGateway resources.
-
Create the
OnPremEventGatewayresource:echo ' apiVersion: eventgateway.konghq.com/v1alpha1 kind: OnPremEventGateway metadata: name: cp-event-tls namespace: kong spec: gwAdminRef: name: gw-admin ' | kubectl apply -f - -
Wait for the resource to be ready:
kubectl wait onpremeventgateway/cp-event-tls -n kong \ --for=condition=Programmed=True \ --timeout=5m
Create the backend cluster, virtual cluster, and listener
-
Create the
EventGatewayBackendCluster,EventGatewayVirtualCluster, andEventGatewayListenerthat define how Kafka traffic connects through thecp-event-tlscontrol plane:echo ' apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayBackendCluster metadata: name: default-backend-cluster-tls namespace: kong spec: parentRef: group: konnect.konghq.com kind: KonnectEventGateway name: cp-event-tls apiSpec: name: default_backend_cluster_tls bootstrapServers: - kafka-cluster.kafka.svc.cluster.local:9092 authentication: type: anonymous anonymous: {} insecureAllowAnonymousVirtualClusterAuth: Enabled tls: enabled: Disabled --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayVirtualCluster metadata: name: example-virtual-cluster-tls namespace: kong spec: eventGatewayBackendClusterRef: type: namespacedRef namespacedRef: name: default-backend-cluster-tls apiSpec: name: example_virtual_cluster_tls dnsLabel: vc-tls-1 aclMode: passthrough authentication: - type: anonymous namespace: prefix: "vctls_" mode: hide_prefix --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayListener metadata: name: sni-listener namespace: kong spec: apiSpec: name: sni_listener addresses: - 0.0.0.0 ports: - "9092" parentRef: group: konnect.konghq.com kind: KonnectEventGateway name: cp-event-tls ' | kubectl apply -f -echo ' apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayBackendCluster metadata: name: default-backend-cluster-tls namespace: kong spec: parentRef: group: eventgateway.konghq.com kind: OnPremEventGateway name: cp-event-tls apiSpec: name: default_backend_cluster_tls bootstrapServers: - kafka-cluster.kafka.svc.cluster.local:9092 authentication: type: anonymous anonymous: {} insecureAllowAnonymousVirtualClusterAuth: Enabled tls: enabled: Disabled --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayVirtualCluster metadata: name: example-virtual-cluster-tls namespace: kong spec: eventGatewayBackendClusterRef: type: namespacedRef namespacedRef: name: default-backend-cluster-tls apiSpec: name: example_virtual_cluster_tls dnsLabel: vc-tls-1 aclMode: passthrough authentication: - type: anonymous namespace: prefix: "vctls_" mode: hide_prefix --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayListener metadata: name: sni-listener namespace: kong spec: apiSpec: name: sni_listener addresses: - 0.0.0.0 ports: - "9092" parentRef: group: eventgateway.konghq.com kind: OnPremEventGateway name: cp-event-tls ' | kubectl apply -f - -
Wait for the resources to be ready:
kubectl wait eventgatewaybackendcluster/default-backend-cluster-tls -n kong \ --for=condition=Programmed=True \ --timeout=10m kubectl wait eventgatewayvirtualcluster/example-virtual-cluster-tls -n kong \ --for=condition=Programmed=True \ --timeout=10m kubectl wait eventgatewaylistener/sni-listener -n kong \ --for=condition=Programmed=True \ --timeout=10m
Create the TLS Secret and listener policies
-
Generate a self-signed TLS certificate for
*.keg.test.local. This wildcard covers all broker and bootstrap hostnames across every virtual cluster on this listener:openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ -keyout tls.key -out tls.crt \ -subj "/CN=keg.test.local" \ -addext "subjectAltName=DNS:*.keg.test.local" -
Export the certificate and key as base64-encoded environment variables:
export TLS_CRT=$(base64 < tls.crt | tr -d '\n') export TLS_KEY=$(base64 < tls.key | tr -d '\n') -
Create the TLS Secret:
echo ' apiVersion: v1 kind: Secret metadata: name: sni-listener-tls namespace: kong labels: konghq.com/secret: "true" type: kubernetes.io/tls data: tls.crt: '"$TLS_CRT"' tls.key: '"$TLS_KEY"' ' | kubectl apply -f - -
Apply the listener policies. The TLS server policy reads the certificate and key from the
tls.crtandtls.keykeys of the Secret. The SNI forwarding policy setsadvertisedPortto19092, the port of theGatewaylistener that you create later, because clients connect to the advertised brokers through theGateway:echo ' apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayListenerPolicy metadata: name: sni-tls-server namespace: kong spec: apiSpec: type: tlsServer tlsServer: name: tls_server enabled: Enabled config: allowPlaintext: Disabled versions: min: TLSv1.2 max: TLSv1.3 certificates: - certificate: type: secretRef secretRef: name: sni-listener-tls key: tls.crt key: type: secretRef secretRef: name: sni-listener-tls key: tls.key eventGatewayListenerRef: type: namespacedRef namespacedRef: name: sni-listener --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayListenerPolicy metadata: name: sni-forward namespace: kong spec: apiSpec: type: forwardToVirtualCluster forwardToVirtualCluster: name: forward_sni config: type: sni sni: sniSuffix: ".keg.test.local" brokerHostFormat: type: per_cluster_suffix advertisedPort: 19092 eventGatewayListenerRef: type: namespacedRef namespacedRef: name: sni-listener ' | kubectl apply -f - -
Wait for the listener policies to be ready:
kubectl wait eventgatewaylistenerpolicy/sni-tls-server -n kong \ --for=condition=Programmed=True \ --timeout=10m kubectl wait eventgatewaylistenerpolicy/sni-forward -n kong \ --for=condition=Programmed=True \ --timeout=10m
Create consume and produce policies
These policies add a marker header to consumed and produced records so you can verify they are attached.
-
Create the consume and produce policies:
echo ' apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayVirtualClusterConsumePolicy metadata: name: example-virtual-cluster-tls-consume-policy namespace: kong spec: eventGatewayVirtualClusterRef: type: namespacedRef namespacedRef: name: example-virtual-cluster-tls apiSpec: type: modifyHeaders modifyHeaders: name: example_tls_consume_policy description: Add a marker header to consumed records for the TLSRoute example config: actions: - op: set set: key: x-kong-consume-policy value: tlsroute --- apiVersion: configuration.konghq.com/v1alpha1 kind: EventGatewayVirtualClusterProducePolicy metadata: name: example-virtual-cluster-tls-produce-policy namespace: kong spec: eventGatewayVirtualClusterRef: type: namespacedRef namespacedRef: name: example-virtual-cluster-tls apiSpec: type: modifyHeaders modifyHeaders: name: example_tls_produce_policy description: Add a marker header to produced records for the TLSRoute example config: actions: - op: set set: key: x-kong-produce-policy value: tlsroute ' | kubectl apply -f - -
Wait for the resources to be ready:
kubectl wait eventgatewayvirtualclusterconsumepolicy/example-virtual-cluster-tls-consume-policy -n kong \ --for=condition=Programmed=True \ --timeout=10m kubectl wait eventgatewayvirtualclusterproducepolicy/example-virtual-cluster-tls-produce-policy -n kong \ --for=condition=Programmed=True \ --timeout=10m
Deploy the KegDataPlane
For the TLSRoute pattern, the KegDataPlane stays internal to the cluster. The Kafka listener is exposed as a ClusterIP Service because the Kubernetes Gateway will be the public entrypoint.
Deploy the KegDataPlane:
echo '
apiVersion: eventgateway.konghq.com/v1alpha1
kind: KegDataPlane
metadata:
name: keg-tls-dp
namespace: kong
spec:
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: cp-event-tls
network:
services:
kafka:
type: ClusterIP
ports:
- name: kafka
port: 9092
targetPort: 9092
' | kubectl apply -f --
Deploy the
KegDataPlane:echo ' apiVersion: eventgateway.konghq.com/v1alpha1 kind: KegDataPlane metadata: name: keg-tls-dp namespace: kong spec: controlPlaneRef: type: onpremNamespacedRef onpremNamespacedRef: name: cp-event-tls network: services: kafka: type: ClusterIP ports: - name: kafka port: 9092 targetPort: 9092 ' | kubectl apply -f - -
Wait for the data plane to be ready:
kubectl wait kegdataplane/keg-tls-dp -n kong \ --for=condition=Ready=True \ --timeout=5m
Create the Kubernetes Gateway
Create a Gateway that listens for TLS traffic on port 19092. It uses TLS passthrough so that Kong Event Gateway still terminates TLS with the certificate you configured earlier.
-
Create the
Gateway:echo ' apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: kong-keg namespace: kong spec: gatewayClassName: kong listeners: - name: kafka-tls protocol: TLS port: 19092 hostname: "*.keg.test.local" tls: mode: Passthrough certificateRefs: [] allowedRoutes: namespaces: from: Same kinds: - group: gateway.networking.k8s.io kind: TLSRoute ' | kubectl apply -f - -
Wait for the
Gatewayto be accepted:kubectl wait gateway/kong-keg -n kong --for=condition=Accepted=True --timeout=5m
Attach a TLSRoute to the Gateway
The TLSRoute binds the wildcard Kafka hostnames to the internal Kafka Service exposed by the KegDataPlane.
-
Create the
TLSRoute:echo ' apiVersion: gateway.networking.k8s.io/v1 kind: TLSRoute metadata: name: keg-tls namespace: kong spec: parentRefs: - group: gateway.networking.k8s.io kind: Gateway name: kong-keg namespace: kong sectionName: kafka-tls hostnames: - "*.vc-tls-1.keg.test.local" rules: - backendRefs: - group: "" kind: Service name: keg-tls-dp-kafka port: 9092 ' | kubectl apply -f - -
Wait for the
Gatewayto be programmed, then export its address:kubectl wait gateway/kong-keg -n kong --for=condition=Programmed=True --timeout=5m export GATEWAY_IP=$(kubectl get gateway kong-keg -n kong -o jsonpath='{.status.addresses[0].value}') echo $GATEWAY_IP -
Check the TLSRoute conditions:
kubectl get tlsroute keg-tls -n kong \ -o jsonpath='{range .status.parents[0].conditions[*]}{.type}={.status}{" "}{end}{"\n"}'
Smoke test with Kafka metadata
For local testing, create host aliases for the advertised broker hostnames and request metadata with kcat:
kubectl run kcat-tlsroute --rm -i --restart=Never --image=edenhill/kcat:1.7.1 -n kong \
--overrides='{"spec":{"hostAliases":[{"ip":"'"${GATEWAY_IP}"'","hostnames":["bootstrap.vc-tls-1.keg.test.local","broker-0.vc-tls-1.keg.test.local","broker-1.vc-tls-1.keg.test.local","broker-2.vc-tls-1.keg.test.local"]}]}}' \
--command -- kcat -b bootstrap.vc-tls-1.keg.test.local:19092 \
-X security.protocol=SSL \
-X enable.ssl.certificate.verification=false \
-LThe output lists all brokers on port 19092, with DNS names based on the virtual cluster dnsLabel, for example:
Metadata for all topics (from broker -1: ssl://bootstrap.vc-tls-1.keg.test.local:19092/bootstrap):
3 brokers:
broker 0 at broker-0.vc-tls-1.keg.test.local:19092
broker 1 at broker-1.vc-tls-1.keg.test.local:19092
broker 2 at broker-2.vc-tls-1.keg.test.local:19092 (controller)
0 topics:Produce and consume a record
Create a topic, produce a record, and consume it back through the Gateway with kafkactl:
kubectl run kafkactl-tlsroute --rm -i --restart=Never --image=deviceinsight/kafkactl:v5.21.0 -n kong \
--overrides='{"spec":{"hostAliases":[{"ip":"'"${GATEWAY_IP}"'","hostnames":["bootstrap.vc-tls-1.keg.test.local","broker-0.vc-tls-1.keg.test.local","broker-1.vc-tls-1.keg.test.local","broker-2.vc-tls-1.keg.test.local"]}]}}' \
--env=BROKERS=bootstrap.vc-tls-1.keg.test.local:19092 \
--env=TLS_ENABLED=true \
--env=TLS_INSECURE=true \
--command -- sh -c '
kafkactl create topic orders --partitions 1 --replication-factor 3 &&
kafkactl produce orders --value hello &&
sleep 3 &&
kafkactl consume orders --from-beginning --exit --print-headers'The sleep gives the new topic’s metadata time to propagate before the consumer reads it. The consumed record has the headers that both policies added:
x-kong-consume-policy:tlsroute,x-kong-produce-policy:tlsroute#helloCleanup
Delete the resources created in this guide
Delete the {{ site.event_gateway }} resources, the Gateway resources, and the {{ site.konnect_short_name }} authentication configuration:
kubectl delete -n kong --ignore-not-found \
tlsroute/keg-tls \
service/keg-tls-dp-kafka \
gateway/kong-keg \
gatewayconfiguration/kong-configuration \
eventgatewaylistenerpolicy/sni-tls-server \
eventgatewaylistenerpolicy/sni-forward \
eventgatewaylistenerpolicy/example-listener-policy \
eventgatewayvirtualclusterconsumepolicy/example-virtual-cluster-tls-consume-policy \
eventgatewayvirtualclusterconsumepolicy/example-virtual-cluster-consume-policy \
eventgatewayvirtualclusterproducepolicy/example-virtual-cluster-tls-produce-policy \
eventgatewayvirtualclusterproducepolicy/example-virtual-cluster-produce-policy \
eventgatewayvirtualcluster/example-virtual-cluster-tls \
eventgatewayvirtualcluster/example-virtual-cluster \
eventgatewaylistener/sni-listener \
eventgatewaylistener/example-listener \
eventgatewaybackendcluster/default-backend-cluster-tls \
eventgatewaybackendcluster/default-backend-cluster \
kegdataplane/keg-tls-dp \
kegdataplane/my-event-gateway-dp \
secret/sni-listener-tls \
konnecteventgateway/cp-event-tls \
konnecteventgateway/cp-event-1 \
konnectapiauthconfiguration/konnect-api-auth
kubectl delete --ignore-not-found gatewayclass/kongDelete the resources created in this guide
Delete the {{ site.event_gateway }} resources and the Gateway resources. Delete the OnPremEventGateway resources before the control plane, so that {{site.operator_product_name}} can first remove their configuration from the control plane:
kubectl delete -n kong --ignore-not-found \
tlsroute/keg-tls \
gateway/kong-keg \
gatewayconfiguration/kong-configuration \
eventgatewaylistenerpolicy/sni-tls-server \
eventgatewaylistenerpolicy/sni-forward \
eventgatewaylistenerpolicy/example-listener-policy \
eventgatewayvirtualclusterconsumepolicy/example-virtual-cluster-tls-consume-policy \
eventgatewayvirtualclusterconsumepolicy/example-virtual-cluster-consume-policy \
eventgatewayvirtualclusterproducepolicy/example-virtual-cluster-tls-produce-policy \
eventgatewayvirtualclusterproducepolicy/example-virtual-cluster-produce-policy \
eventgatewayvirtualcluster/example-virtual-cluster-tls \
eventgatewayvirtualcluster/example-virtual-cluster \
eventgatewaylistener/sni-listener \
eventgatewaylistener/example-listener \
eventgatewaybackendcluster/default-backend-cluster-tls \
eventgatewaybackendcluster/default-backend-cluster \
kegdataplane/keg-tls-dp \
kegdataplane/my-event-gateway-dp \
secret/sni-listener-tls \
onpremeventgateway/cp-event-tls \
onpremeventgateway/cp-event-1
kubectl delete -n kong --ignore-not-found gwadmin/gw-admin secret/gw-admin-license
kubectl delete --ignore-not-found gatewayclass/kong konglicense/kong-licenseDelete the namespaces
helm uninstall kong-operator -n kong-system
kubectl delete namespace kafka kong kong-system