Apache Kafka
To setup Apache Kafka pubsub create a component of type . See this guide on how to create and apply a pubsub configuration. For details on using secretKeyRef
, see the guide on .
The secretKeyRef
above is referencing a kubernetes secrets store to access the tls information. Visit to learn more about how to configure a secret store component.
Kafka supports a variety of authentication schemes and Dapr supports several: SASL password, mTLS, OIDC/OAuth2. With the added authentication methods, the authRequired
field has been deprecated from the v1.6 release and instead the authType
field should be used. If authRequired
is set to true
, Dapr will attempt to configure authType
correctly based on the value of saslPassword
. There are four valid values for authType
: none
, password
, mtls
, and oidc
. Note this is authentication only; authorization is still configured within Kafka.
None
Setting authType
to none
will disable any authentication. This is NOT recommended in production.
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: kafka-pubsub-noauth
namespace: default
spec:
type: pubsub.kafka
version: v1
metadata:
- name: brokers # Required. Kafka broker connection setting
value: "dapr-kafka.myapp.svc.cluster.local:9092"
- name: consumerGroup # Optional. Used for input bindings.
value: "group1"
- name: clientID # Optional. Used as client tracing ID by Kafka brokers.
value: "my-dapr-app-id"
- name: authType # Required.
- name: maxMessageBytes # Optional.
value: 1024
- name: consumeRetryInterval # Optional.
value: 200ms
- name: version # Optional.
value: 0.10.2.0
- name: disableTls
value: "true"
SASL Password
Mutual TLS
Setting authType
to mtls
uses a x509 client certificate (the clientCert
field) and key (the clientKey
field) to authenticate. Note that mTLS as an authentication mechanism is distinct from using TLS to secure the transport layer via encryption. mTLS requires TLS transport (meaning disableTls
must be false
), but securing the transport layer does not require using mTLS. See for configuring underlying TLS transport.
apiVersion: dapr.io/v1alpha1
metadata:
name: kafka-pubsub-mtls
namespace: default
spec:
type: pubsub.kafka
version: v1
metadata:
- name: brokers # Required. Kafka broker connection setting
value: "dapr-kafka.myapp.svc.cluster.local:9092"
- name: consumerGroup # Optional. Used for input bindings.
value: "group1"
- name: clientID # Optional. Used as client tracing ID by Kafka brokers.
value: "my-dapr-app-id"
- name: authType # Required.
value: "mtls"
- name: caCert
secretKeyRef:
name: kafka-tls
key: caCert
- name: clientCert
secretKeyRef:
name: kafka-tls
key: clientCert
- name: clientKey
secretKeyRef:
name: kafka-tls
key: clientKey
- name: maxMessageBytes # Optional.
value: 1024
- name: consumeRetryInterval # Optional.
value: 200ms
- name: version # Optional.
value: 0.10.2.0
OAuth2 or OpenID Connect
Setting authType
to oidc
enables SASL authentication via the OAUTHBEARER mechanism. This supports specifying a bearer token from an external OAuth2 or identity provider. Currenly only the client_credentials grant is supported. Configure oidcTokenEndpoint
to the full URL for the identity provider access token endpoint. Set and oidcClientSecret
to the client credentials provisioned in the identity provider. If caCert
is specified in the component configuration, the certificate is appended to the system CA trust for verifying the identity provider certificate. Similarly, if skipVerify
is specified in the component configuration, verification will also be skipped when accessing the identity provider. By default, the only scope requested for the token is openid
; it is highly recommended that additional scopes be specified via oidcScopes
in a comma-separated list and validated by the Kafka broker. If additional scopes are not used to narrow the validity of the access token, a compromised Kafka broker could replay the token to access other services as the Dapr clientID.
By default TLS is enabled to secure the transport layer to Kafka. To disable TLS, set disableTls
to true
. When TLS is enabled, you can control server certificate verification using skipVerify
to disable verificaiton (NOT recommended in production environments) and caCert
to specify a trusted TLS certificate authority (CA). If no caCert
is specified, the system CA trust will be used. To also configure mTLS authentication, see the section under Authentication. Below is an example of a Kafka pubsub component configured to use transport layer TLS:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: kafka-pubsub
namespace: default
spec:
version: v1
metadata:
- name: brokers # Required. Kafka broker connection setting
value: "dapr-kafka.myapp.svc.cluster.local:9092"
- name: consumerGroup # Optional. Used for input bindings.
value: "group1"
- name: clientID # Optional. Used as client tracing ID by Kafka brokers.
value: "my-dapr-app-id"
- name: authType # Required.
value: "password"
- name: saslUsername # Required if authType is `password`.
value: "adminuser"
- name: consumeRetryInterval # Optional.
value: 200ms
- name: version # Optional.
value: 0.10.2.0
- name: saslPassword # Required if authRequired is `true`.
secretKeyRef:
name: kafka-secrets
key: saslPasswordSecret
- name: maxMessageBytes # Optional.
value: 1024
- name: caCert # Certificate authority certificate.
secretKeyRef:
name: kafka-tls
key: caCert
auth:
secretStore: <SECRET_STORE_NAME>
When invoking the Kafka pub/sub, its possible to provide an optional partition key by using the metadata
query param in the request url.
Example:
All other metadata key/value pairs (that are not partitionKey
) are set as headers in the Kafka message. Here is an example setting a correlationId
for the message.
curl -X POST http://localhost:3500/v1.0/publish/myKafka/myTopic?metadata.correlationId=myCorrelationID&metadata.partitionKey=key1 \
-H "Content-Type: application/json" \
-d '{
"data": {
"message": "Hi"
}
}'
You can run Kafka locally using this Docker image. To run without Docker, see the getting started guide .
To run Kafka on Kubernetes, you can use any Kafka operator, such as Strimzi.
- Read this guide for instructions on configuring pub/sub components