Skip to content

Persistent Kafka getting restarted #6651

Open

Description

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A Kafka dependency is restarted each time I run the AppHost project.

Minimal reproduction code:


var kafka = builder
    .AddKafka("messaging").WithLifetime(ContainerLifetime.Persistent);

I am on MacOS, using Docker Desktop

Happy to provide further details as necessary

Thank you

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

AppHost CSProj

<Project Sdk="Microsoft.NET.Sdk">
  <Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsAspireHost>true</IsAspireHost>
    <UserSecretsId>*****</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    
    <PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
    <PackageReference Include="Aspire.Hosting.Kafka" Version="9.0.0" />
    <PackageReference Include="Aspire.Npgsql" Version="9.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Domain\Demo\DemoApp\DemoApp.csproj" />
  </ItemGroup>
</Project>

.NET Version info

dotnet --info
.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.4d7d414a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  14.4
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.100/

.NET workloads installed:
 Workload version: 8.0.100-manifests.4d7d414a
There are no installed workloads to display.

Host:
  Version:      8.0.0
  Architecture: arm64
  Commit:       5535e31a71

.NET SDKs installed:
  8.0.100 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  x64   [/usr/local/share/dotnet/x64]
    registered at [/etc/dotnet/install_location_x64]

Environment variables:
  Not set

global.json file:
  Not found

Anything else?

No response

Activity

  1. added
    area-integrations
    kafka
    on Nov 14, 2024
  2. amolenk commented on Nov 26, 2024

    @amolenk

    Seeing the same behavior

  3. duskembayev commented on Dec 18, 2024

    @duskembayev
    Contributor

    Defining the port solves the problem.

    var kafka = builder
        .AddKafka("messaging", 9092).WithLifetime(ContainerLifetime.Persistent);
    
  4. fredeil commented on Feb 19, 2025

    @fredeil

    Currently experiencing the same on .NET9

  5. davidfowl commented on Mar 3, 2025

    @davidfowl
    Collaborator

    David Negstad (@danegsta) there should be a log that explains why the container isn't being persistent right?

  6. danegsta commented on Mar 3, 2025

    @danegsta
    Member

    @danegsta there should be a log that explains why the container isn't being persistent right?

    Yeah, setting the Aspire.Hosting.Dcp LogLevel to information should show a log message when we recreate a persistent container. Log entry starts with “found existing Container, but the lifecycle key doesn't match, recreating container” and should then give a high level breakdown of what properties have changed (ports, environment variables, bind mounts, etc.).

  7. davidfowl commented on Mar 3, 2025

    @davidfowl
    Collaborator

    David Negstad (@danegsta) I think we should log that in warning by default but log the more verbose information as information.

  8. danegsta commented on Mar 3, 2025

    @danegsta
    Member

    @danegsta I think we should log that in warning by default but log the more verbose information as information.

    The logging library that we're basically forced to use (it's standard in all our dependencies) has explicitly chosen not to support warning level logging (apparently the author read a blog that said warnings are bad so now we're not allowed to use them). Practically speaking we're limited to error, info, and debug log levels.

  9. lwu-st commented on May 29, 2025

    @lwu-st

    This issue is still present on 9.3.0, and the workaround by specifying a port doesn't work for us either.
    Does anyone have any other workaround that may be worth trying?
    This is especially detrimental to integration test time consumption, as each run will reprovision Kafka.

    IResourceBuilder<KafkaServerResource> kafka = builder.AddKafka("kafka", 9092)
        .WithContainerName("kafka") // tried removing this
        .WithLifetime(ContainerLifetime.Persistent)
        .WithDataVolume("kafka-data"); // also tried removing this
    
  10. davidfowl commented on May 29, 2025

    @davidfowl
    Collaborator

    Did anyone turn on the logging that explains why the container is getting restarted?

  11. lwu-st commented on May 29, 2025

    @lwu-st

    David Fowler (@davidfowl) just retrieved it:

    info: Aspire.Hosting.Dcp.dcpctrl.ContainerReconciler[0]
          found existing Container, but the lifecycle key doesn't match, recreating container       {"Container": {"name":"gum-kafka"}, "Reconciliation": 17, "ContainerName": "gum-kafka", "ContainerID": "40169af84b0857706f499fc9186cce24fce3f996d7a0606c09ff50f6f7d73435", "OldLifecycleKey": "1cc4f78b7f5cb41a0fde197cdd8f98dbeee8b954cc53c6231a12035757ebc4ce", "NewLifecycleKey": "c6fbb0fc1bc229a6531d119f577df70f8f5a152f210b8f275253497495a8f394", "MountChanges": [], "PortChanges": [], "EnvChanges": ["KAFKA_ADVERTISED_LISTENERS"], "OtherChanges": []}
    
  12. davidfowl commented on May 29, 2025

    @davidfowl
    Collaborator
  13. danegsta commented on May 29, 2025

    @danegsta
    Member

    This is basically the predictable persistent container ports issue; by default Kafka is getting a random proxied host port every run which causes the ports reported in the KAFKA_ADVERTISED_LISTENERS environment variable to change despite being configured with what should be a fixed port of 9092. You should be able to opt the resource into running proxyless by adding the experimental .WithEndpointProxySupport(false) which would prevent the environment variable changing every run.

  14. artur-st commented on May 30, 2025

    @artur-st

    This is basically the predictable persistent container ports issue; by default Kafka is getting a random proxied host port every run which causes the ports reported in the KAFKA_ADVERTISED_LISTENERS environment variable to change despite being configured with what should be a fixed port of 9092. You should be able to opt the resource into running proxyless by adding the experimental .WithEndpointProxySupport(false) which would prevent the environment variable changing every run.

    Workaround worked for me 🚀

  15. Odonno commented on Aug 19, 2025

    @Odonno
    Contributor

    The same issue happens with Kafka and goff (from CommunityToolkit). However, the SQL Server hosting works as expected.

    I can also notice that the volume is not destroyed when the container is stopped/recreated, which causes issues as more volumes exist at the same time. Is it a side effect or could it be the cause?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-integrationskafka

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions