Skip to main content

Kafka Proxy

The kafka-proxy plugin supplies SASL/PLAIN credentials when the gateway reads records from Apache Kafka through its publish-subscribe WebSocket API. Clients connect with WebSocket and send binary Protocol Buffers commands to list offsets and fetch records from a topic partition.

This integration is a Kafka consumer bridge. It does not proxy native Kafka clients, produce records, manage consumer groups, or commit offsets.

Examples​

The examples start a SASL-enabled Kafka broker, configure the gateway, and demonstrate the two supported Kafka operations: listing the latest offset and fetching records.

Set Up a SASL-Protected Kafka Route​

The setup requires Docker, protoc, websocat, and a running APISIX or API7 Gateway container that can join a Docker network.

Create and enter a working directory:

mkdir kafka-proxy-example
cd kafka-proxy-example

Create the broker login configuration:

kafka-jaas.conf
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret"
user_admin="admin-secret";
};

Create the client configuration used to prepare the topic:

kafka-client.properties
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";

Set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container. Create a dedicated network and connect the gateway to it:

export GATEWAY_CONTAINER=replace-with-gateway-container-name

docker network create gateway-kafka-net
docker network connect gateway-kafka-net "$GATEWAY_CONTAINER"

Start a single-node Kafka broker with SASL/PLAIN enabled:

docker run -d \
--name kafka-proxy-broker \
--network gateway-kafka-net \
-v "$PWD/kafka-jaas.conf:/etc/kafka/kafka_server_jaas.conf:ro" \
-v "$PWD/kafka-client.properties:/tmp/client.properties:ro" \
-e KAFKA_NODE_ID=1 \
-e KAFKA_PROCESS_ROLES=broker,controller \
-e KAFKA_LISTENERS="CONTROLLER://:9093,SASL_PLAINTEXT://:9092" \
-e KAFKA_ADVERTISED_LISTENERS="SASL_PLAINTEXT://kafka-proxy-broker:9092" \
-e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \
-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP="CONTROLLER:PLAINTEXT,SASL_PLAINTEXT:SASL_PLAINTEXT" \
-e KAFKA_INTER_BROKER_LISTENER_NAME=SASL_PLAINTEXT \
-e KAFKA_SASL_ENABLED_MECHANISMS=PLAIN \
-e KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL=PLAIN \
-e KAFKA_CONTROLLER_QUORUM_VOTERS="1@kafka-proxy-broker:9093" \
-e KAFKA_OPTS="-Djava.security.auth.login.config=/etc/kafka/kafka_server_jaas.conf" \
apache/kafka:3.9.1

The example uses SASL over plaintext on an isolated Docker network. Encrypt broker connections with TLS in production. The upstream nodes are bootstrap brokers, so every broker address returned in Kafka metadata must be reachable from each gateway instance.

Wait for the broker, create a topic, and produce two records:

until docker exec kafka-proxy-broker \
/opt/kafka/bin/kafka-topics.sh \
--bootstrap-server kafka-proxy-broker:9092 \
--command-config /tmp/client.properties \
--list >/dev/null 2>&1; do
sleep 2
done

docker exec kafka-proxy-broker \
/opt/kafka/bin/kafka-topics.sh \
--bootstrap-server kafka-proxy-broker:9092 \
--command-config /tmp/client.properties \
--create \
--if-not-exists \
--topic docs-events \
--partitions 1 \
--replication-factor 1

printf "first event\nsecond event\n" | \
docker exec -i kafka-proxy-broker \
/opt/kafka/bin/kafka-console-producer.sh \
--bootstrap-server kafka-proxy-broker:9092 \
--producer.config /tmp/client.properties \
--topic docs-events

Create the Protocol Buffers definitions needed by the example client:

pubsub.proto
syntax = "proto3";

message CmdKafkaListOffset {
string topic = 1;
int32 partition = 2;
int64 timestamp = 3;
}

message CmdKafkaFetch {
string topic = 1;
int32 partition = 2;
int64 offset = 3;
}

message PubSubReq {
int64 sequence = 1;
oneof req {
CmdKafkaFetch cmd_kafka_fetch = 33;
CmdKafkaListOffset cmd_kafka_list_offset = 34;
}
}

message ErrorResp {
int32 code = 1;
string message = 2;
}

message KafkaMessage {
int64 offset = 1;
int64 timestamp = 2;
bytes key = 3;
bytes value = 4;
}

message KafkaFetchResp {
repeated KafkaMessage messages = 1;
}

message KafkaListOffsetResp {
int64 offset = 1;
}

message PubSubResp {
int64 sequence = 1;
oneof resp {
ErrorResp error_resp = 31;
KafkaFetchResp kafka_fetch_resp = 33;
KafkaListOffsetResp kafka_list_offset_resp = 34;
}
}

Create a route with a Kafka upstream and the broker credentials:

curl "http://127.0.0.1:9180/apisix/admin/routes/kafka-proxy-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"uri": "/kafka",
"plugins": {
"kafka-proxy": {
"sasl": {
"username": "admin",
"password": "admin-secret"
}
}
},
"upstream": {
"nodes": {
"kafka-proxy-broker:9092": 1
},
"type": "none",
"scheme": "kafka"
}
}'

List the Latest Offset​

Each command addresses one topic partition, and clients are responsible for selecting and persisting offsets. Create a ListOffsets command with a timestamp of -1 to request the offset after the newest record:

list-offset.textproto
sequence: 1
cmd_kafka_list_offset {
topic: "docs-events"
partition: 0
timestamp: -1
}

Encode and send the ListOffsets command:

protoc --encode=PubSubReq pubsub.proto \
< list-offset.textproto \
> list-offset.bin

websocat -1 -b "ws://127.0.0.1:9080/kafka" \
< list-offset.bin \
> list-offset-response.bin

protoc --decode=PubSubResp pubsub.proto \
< list-offset-response.bin

The response reports the next offset after the two records:

sequence: 1
kafka_list_offset_resp {
offset: 2
}

Fetch Records​

Create a Fetch command that starts from the first offset:

fetch.textproto
sequence: 2
cmd_kafka_fetch {
topic: "docs-events"
partition: 0
offset: 0
}

Encode and send the Fetch command:

protoc --encode=PubSubReq pubsub.proto \
< fetch.textproto \
> fetch.bin

websocat -1 -b "ws://127.0.0.1:9080/kafka" \
< fetch.bin \
> fetch-response.bin

protoc --decode=PubSubResp pubsub.proto \
< fetch-response.bin | \
sed '/timestamp:/d'

The response contains both records. The first offset is omitted because zero is the Protocol Buffers default value:

sequence: 2
kafka_fetch_resp {
messages {
value: "first event"
}
messages {
offset: 1
value: "second event"
}
}