You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Test connectivity to IoT MQ with MQTT clients
3
-
description: Learn how to use common and standard MQTT tools to test connectivity to Azure IoT MQ.
3
+
description: Learn how to use common and standard MQTT tools to test connectivity to Azure IoT MQ in a nonproduction environment.
4
4
author: PatAltimore
5
5
ms.author: patricka
6
6
ms.subservice: mq
7
7
ms.topic: how-to
8
8
ms.custom:
9
9
- ignite-2023
10
-
ms.date: 11/15/2023
10
+
ms.date: 03/18/2024
11
11
12
12
#CustomerIntent: As an operator or developer, I want to test MQTT connectivity with tools that I'm already familiar with to know that I set up my Azure IoT MQ broker correctly.
This article shows different ways to test connectivity to Azure IoT MQ Preview with MQTT clients.
19
+
This article shows different ways to test connectivity to Azure IoT MQ Preview with MQTT clients in a nonproduction environment.
20
20
21
-
By default:
21
+
By default, Azure IoT MQ Preview:
22
22
23
-
-IoT MQ deploys a [TLS-enabled listener](howto-configure-brokerlistener.md) on port 8883 with *ClusterIp* as the service type. *ClusterIp* means that the broker is accessible only from within the Kubernetes cluster. To access the broker from outside the cluster, you must configure a service of type *LoadBalancer* or *NodePort*.
23
+
-Deploys a [TLS-enabled listener](howto-configure-brokerlistener.md) on port 8883 with *ClusterIp* as the service type. *ClusterIp* means that the broker is accessible only from within the Kubernetes cluster. To access the broker from outside the cluster, you must configure a service of type *LoadBalancer* or *NodePort*.
24
24
25
-
-IoT MQ only accepts[Kubernetes service accounts for authentication](howto-configure-authentication.md) for connections from within the cluster. To connect from outside the cluster, you must configure a different authentication method.
25
+
-Accepts[Kubernetes service accounts for authentication](howto-configure-authentication.md) for connections from within the cluster. To connect from outside the cluster, you must configure a different authentication method.
26
26
27
-
Before you begin, [install or deploy IoT Operations](../get-started/quickstart-deploy.md). Use the following options to test connectivity to IoT MQ with MQTT clients.
27
+
> [!CAUTION]
28
+
> For production scenarios, you should use TLS and service accounts authentication to secure your IoT solution. For more information, see:
29
+
> -[Configure TLS with automatic certificate management to secure MQTT communication in Azure IoT MQ Preview](./howto-configure-tls-auto.md)
30
+
> -[Configure authentication in Azure IoT MQ Preview](./howto-configure-authentication.md)
31
+
> -[Expose Kubernetes services to external devices](/azure/aks/hybrid/aks-edge-howto-expose-service) using port forwarding or a virtual switch with Azure Kubernetes Services Edge Essentials.
32
+
33
+
Before you begin, [install or deploy IoT Operations](../get-started/quickstart-deploy.md). Use the following options to test connectivity to IoT MQ with MQTT clients in a nonproduction environment.
28
34
29
35
## Connect from a pod within the cluster with default configuration
30
36
31
37
The first option is to connect from within the cluster. This option uses the default configuration and requires no extra updates. The following examples show how to connect from within the cluster using plain Alpine Linux and a commonly used MQTT client, using the service account and default root CA cert.
32
38
33
-
```yaml
34
-
apiVersion: v1
35
-
kind: Pod
36
-
metadata:
37
-
name: mqtt-client
38
-
# Namespace must match IoT MQ BrokerListener's namespace
39
-
# Otherwise use the long hostname: aio-mq-dmqtt-frontend.azure-iot-operations.svc.cluster.local
40
-
namespace: azure-iot-operations
41
-
spec:
42
-
# Use the "mqtt-client" service account which comes with default deployment
43
-
# Otherwise create it with `kubectl create serviceaccount mqtt-client -n azure-iot-operations`
The mosquitto client uses the same service account token and root CA cert to authenticate with the broker and subscribe to the topic.
103
121
104
-
1. To use *mqttui*, the command is similar:
122
+
1. You can also use mqttui to connect to the broker using the service account token. The `--insecure` flag is required because mqttui doesn't support TLS certificate chain verification with a custom root CA cert.
123
+
124
+
> [!CAUTION]
125
+
> Using `--insecure` is not recommended for production scenarios. Only use it for testing or development purposes.
With the above command, mqttui connects to the broker using the service account token. The `--insecure` flag is required because mqttui doesn't support TLS certificate chain verification with a custom root CA cert.
111
-
112
131
1. To remove the pod, run `kubectl delete pod mqtt-client -n azure-iot-operations`.
113
132
114
133
## Connect clients from outside the cluster to default the TLS port
115
134
116
135
### TLS trust chain
117
136
118
-
Since the broker uses TLS, the client must trust the broker's TLS certificate chain. To do so, you must configure the client to trust the root CA cert used by the broker. To use the default root CA cert, download it from the `aio-ca-trust-bundle-test-only` ConfigMap:
137
+
Since the broker uses TLS, the client must trust the broker's TLS certificate chain. You need to configure the client to trust the root CA certificate used by the broker.
138
+
139
+
To use the default root CA certificate, download it from the `aio-ca-trust-bundle-test-only` ConfigMap:
119
140
120
141
```bash
121
142
kubectl get configmap aio-ca-trust-bundle-test-only -n azure-iot-operations -o jsonpath='{.data.ca\.crt}' > ca.crt
122
143
```
123
144
124
-
Then, use the `ca.crt` file to configure your client to trust the broker's TLS certificate chain.
145
+
Use the downloaded `ca.crt` file to configure your client to trust the broker's TLS certificate chain.
125
146
126
147
### Authenticate with the broker
127
148
@@ -131,57 +152,93 @@ By default, IoT MQ only accepts Kubernetes service accounts for authentication f
131
152
132
153
To turn off authentication for testing purposes, edit the `BrokerListener` resource and set the `authenticationEnabled` field to `false`:
133
154
155
+
> [!CAUTION]
156
+
> Turning off authentication should only be used for testing purposes with a test cluster that's not accessible from the internet.
> Turning off authentication should only be used for testing purposes with a test cluster that's not accessible from the internet.
140
-
141
162
### Port connectivity
142
163
143
164
Some Kubernetes distributions can [expose](https://k3d.io/v5.1.0/usage/exposing_services/) IoT MQ to a port on the host system (localhost). You should use this approach because it makes it easier for clients on the same host to access IoT MQ.
144
165
145
166
For example, to create a K3d cluster with mapping the IoT MQ's default MQTT port 8883 to localhost:8883:
But for this method to work with IoT MQ, you must configure it to use a load balancer instead of cluster IP.
172
+
But for this method to work with IoT MQ, you must configure it to use a load balancer instead of cluster IP. There are two ways to do this: create a load balancer or patch the existing default BrokerListener resource service type to load balancer.
152
173
153
-
To configure a load balancer:
174
+
#### Option 1: Create a load balancer
175
+
176
+
1. Create a file named `loadbalancer.yaml` with the following configuration:
177
+
178
+
```yaml
179
+
apiVersion: v1
180
+
kind: Service
181
+
metadata:
182
+
name: iotmq-public-svc
183
+
spec:
184
+
type: LoadBalancer
185
+
ports:
186
+
- name: mqtt1
187
+
port: 8883
188
+
targetPort: 8883
189
+
selector:
190
+
app: broker
191
+
app.kubernetes.io/instance: broker
192
+
app.kubernetes.io/managed-by: dmqtt-operator
193
+
app.kubernetes.io/name: dmqtt
194
+
tier: frontend
195
+
```
196
+
197
+
1. Apply the configuration to create a load balancer service:
198
+
199
+
```bash
200
+
kubectl apply -f loadbalancer.yaml
201
+
```
202
+
203
+
#### Option 2: Patch the default load balancer
154
204
155
205
1. Edit the `BrokerListener` resource and change the `serviceType` field to `loadBalancer`.
kubectl get service aio-mq-dmqtt-frontend --namespace azure-iot-operations
167
215
```
168
216
169
-
1. Use the external IP address to connect to IoT MQ from outside the cluster. If you used the K3d command with port forwarding, you can use `localhost` to connect to IoT MQ. For example, to connect with mosquitto client:
In this example, the mosquitto client uses username/password to authenticate with the broker along with the root CA cert to verify the broker's TLS certificate chain. Here, the `--insecure` flag is required because the default TLS certificate issued to the load balancer is only valid for the load balancer's default service name (aio-mq-dmqtt-frontend) and assigned IPs, not localhost.
176
-
177
-
1. If your cluster like Azure Kubernetes Service automatically assigns an external IP address to the load balancer, you can use the external IP address to connect to IoT MQ over the internet. Make sure to use the external IP address instead of `localhost` in the prior command, and remove the `--insecure` flag.
224
+
1. You can use the external IP address to connect to IoT MQ over the internet. Make sure to use the external IP address instead of `localhost`.
> Never expose IoT MQ port to the internet without authentication and TLS. Doing so is dangerous and can lead to unauthorized access to your IoT devices and bring unsolicited traffic to your cluster.
230
+
> [!TIP]
231
+
> You can use the external IP address to connect to IoT MQ from outside the cluster. If you used the K3d command with port forwarding option, you can use `localhost` to connect to IoT MQ. For example, to connect with mosquitto client:
> In this example, the mosquitto client uses username and password to authenticate with the broker along with the root CA cert to verify the broker's TLS certificate chain. Here, the `--insecure` flag is required because the default TLS certificate issued to the load balancer is only valid for the load balancer's default service name (aio-mq-dmqtt-frontend) and assigned IPs, not localhost.
238
+
>
239
+
> Never expose IoT MQ port to the internet without authentication and TLS. Doing so is dangerous and can lead to unauthorized access to your IoT devices and bring unsolicited traffic to your cluster.
240
+
>
241
+
> For information on how to add localhost to the certificate's subject alternative name (SAN) to avoid using the insecure flag, see [Configure server certificate parameters](howto-configure-tls-auto.md#optional-configure-server-certificate-parameters).
185
242
186
243
#### Use port forwarding
187
244
@@ -190,14 +247,14 @@ With [minikube](https://minikube.sigs.k8s.io/docs/), [kind](https://kind.sigs.k8
190
247
1. To access the broker, forward the broker listening port 8883 to the host.
1. Use 127.0.0.1 to connect to the broker at port 8883 with the same authentication and TLS configuration as the example without port forwarding.
197
254
198
255
Port forwarding is also useful for testing IoT MQ locally on your development machine without having to modify the broker's configuration.
199
256
200
-
To learn more, see [Use Port Forwarding to Access Applications in a Cluster](https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/) for minikube.
257
+
To learn more, see [Use Port Forwarding to Access Applications in a Cluster](https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/) for minikube and [Expose Kubernetes services to external devices](/azure/aks/hybrid/aks-edge-howto-expose-service) for Azure Kubernetes Services Edge Essentials.
201
258
202
259
## No TLS and no authentication
203
260
@@ -227,10 +284,15 @@ If you understand the risks and need to use an insecure port in a well-controlle
227
284
228
285
The `authenticationEnabled` and `authorizationEnabled` fields are set to `false` to turn off authentication and authorization. The `port` field is set to `1883` to use common MQTT port.
229
286
230
-
1. Wait for the service to be updated. You should see output similar to the following:
287
+
1. Wait for the service to be updated.
231
288
232
289
```console
233
-
$ kubectl get service my-unique-service-name -n azure-iot-operations
290
+
kubectl get service my-unique-service-name --namespace azure-iot-operations
0 commit comments