Skip to content
Docs Try Aspire
Docs Try

Set up Apache Kafka in the AppHost

Apache Kafka logo

This article is the reference for the Aspire Apache Kafka Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model a Kafka resource in your AppHost project.

If you’re new to the Apache Kafka integration, start with the Get started with Apache Kafka integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Apache Kafka.

To start building an Aspire app that uses Apache Kafka, install the 📦 Aspire.Hosting.Kafka NuGet package:

Terminal
aspire add kafka

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Kafka@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Kafka" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add a Kafka server resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the confluentinc/confluent-local image, it creates a new Kafka server instance on your local machine.

  2. The AppHost reference call configures a connection in the consuming project named after the referenced Kafka resource, such as kafka in the preceding example.

Add the Kafka UI sub-resource to the Kafka server resource to get a web interface for monitoring and managing your Kafka cluster during development:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithKafkaUI();
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...

The Kafka UI is a free, open-source web UI to monitor and manage Apache Kafka clusters. It’s automatically linked to your Kafka server and appears as a separate resource in the Aspire dashboard.

To change the host port the Kafka UI listens on, chain a call to WithHostPort (or withHostPort):

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithKafkaUI(kafkaUI => kafkaUI.WithHostPort(9100));
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...

Add a data volume to the Kafka server resource to persist data outside the lifecycle of its container:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithDataVolume(isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...

The data volume is used to persist Kafka broker data outside the lifecycle of its container. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add a data bind mount to the Kafka server resource as shown in the following examples:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithDataBindMount(
source: "/Kafka/Data",
isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...

The Aspire Kafka integration deploys a container from the confluentinc/confluent-local image, which provides a simple Apache Kafka cluster running in KRaft mode with no further configuration required. It’s ideal for local development and testing, but this image is for local experimentation only and isn’t supported by Confluent in production.

To use a full broker topology in non-development environments, switch to an externally managed Kafka cluster via a connection string:

C# — AppHost.cs
var kafka = builder.ExecutionContext.IsRunMode
? builder.AddKafka("kafka").WithKafkaUI()
: builder.AddConnectionString("kafka");

By default, Aspire injects the Kafka connection information using variable names derived from the resource name (for example, KAFKA_HOST, KAFKA_PORT). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka");
var app = builder.AddExecutable("my-app", "node", "app.js", ".")
.WithReference(kafka)
.WithEnvironment(context =>
{
context.EnvironmentVariables["BOOTSTRAP_SERVERS_HOST"] = kafka.Resource.PrimaryEndpoint.Property(EndpointProperty.Host);
context.EnvironmentVariables["BOOTSTRAP_SERVERS_PORT"] = kafka.Resource.PrimaryEndpoint.Property(EndpointProperty.Port);
});
builder.Build().Run();

To reference an externally managed Kafka instance rather than spinning up a local container:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddConnectionString("kafka");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...

For the full reference of Kafka connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Apache Kafka.

The Kafka hosting integration automatically adds a health check for the Kafka server resource. The health check verifies that a Kafka producer with the specified connection name can connect and persist a topic to the Kafka server.