MQTTnet
MQTTnet copied to clipboard
MqttCommunicationException when connecting to Mqtt broker running on Kubernetes
Hi,
I am running a Mqtt broker as a container on my local Kubernetes cluster. Right now I am using MqttNet as anonymous without username and password in my web api which is also running as container on the Kubernetes cluster and then trying to interact with the MQTT broker. Right now I am getting the below error:
Connection id "0HMEJVU79R6PI", Request id "0HMEJVU79R6PI:00000002": An unhandled exception was thrown by the application.
System.AggregateException: One or more errors occurred. (Error while connecting with host 'mqtt-service.default.svc.cluster.local:1883'.)
---> MQTTnet.Exceptions.MqttCommunicationException: Error while connecting with host 'mqtt-service.default.svc.cluster.local:1883'.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused [::ffff:10.103.10.12]:1883
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
at System.Net.Sockets.Socket.MultipleAddressConnectCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__277_0(IAsyncResult iar)
--- End of stack trace from previous location where exception was thrown ---
at MQTTnet.Implementations.CrossPlatformSocket.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at MQTTnet.Implementations.CrossPlatformSocket.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
at MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2 action, TimeSpan timeout, CancellationToken cancellationToken)
at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at DockerizedTestAPI.Controllers.WeatherForecastController.Get() in /src/Controllers/WeatherForecastController.cs:line 69
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
Below is my connection code:
IMqttClient _mqttClient = new MqttFactory().CreateMqttClient();
var clientOptionsBuilder = new MqttClientOptionsBuilder();
clientOptionsBuilder.WithTcpServer("mqtt-service.default.svc.cluster.local", 1883);
MqttClientConnectResult result = _mqttClient.ConnectAsync(clientOptionsBuilder.Build()).Result;
My mosquitto.conf file is:
listener 1883 127.0.0.1
allow_anonymous true
I am also using the below MQTTNet version:
<PackageReference Include="MQTTnet.AspNetCore" Version="3.1.1" />
Is it mandatory to configure username and password if using MqttNet or if we dont it still work as "Allow Anonymous"?
I dont know if this is the right place to ask the question since it also involves Kubernetes.
Please guide.
"Anonymous authentication" should work if you don't set username and password, I guess...
Connection refused
is a pretty good hint and indicates that it is not an authentication issue. The issue is listener 1883 127.0.0.1
in the mosquitto.conf
. Accessing a pod from outside (via service or directly) requires binding to 0.0.0.0
. With 127.0.0.1
, Mosquitto is only accessible from within the same pod (can be a different container though).
I assume this was a configuration issue with Kubernetes so that this ticket can be closed.