Setup TLS listener for Hivemq cluster using HiveMQ operator
In this article, we will walk you through the steps to enable TLS listener for the HiveMQ in the Kubernetes cluster using the HiveMQ Operator. By default, the HiveMQ operator will always enable TCP listener with port 1883.
Prerequisites:
The Kubernetes cluster is already set up and running.
k8s version 1.16+ is installed
Helm version 3 is installed
The Hivemq k8s operator repo is already added in the helm repo
Ready
keystore.jks
, (optional)truststore.jks
(You can find steps to create these JKS files here)
Instructions
Create a Secret from TLS certificate files. Use the following command:
kubectl create secret generic tls-certificates \ --from-file path/to/keystore.jks \ --from-file path/to/truststore.jks -n <namespace>
Add the Secret to your
values.yaml
file to mount to the HiveMQ pods:hivemq: ... secrets: - name: tls-certificates path: /opt/hivemq/conf
This will mount both JKS files at the specified Path.
Store the passwords in the Kubernetes secret using the following command:
kubectl create secret generic tls-passwords \ --from-literal=keystore_pass='password1' -n <namespace>
Create environment variables to access the passwords in the HiveMQ listener’s configurations. Update your
values.yaml
file with the following configuration:To enable the TLS listener, please add the following block to your
values.yaml
and add the correct JKS file name and the environment variables names for passwords used while creating the Keystore.In the
values.yaml
, edit themqtt
port so that it corresponds to the new listener. Update the MQTT port number from 1883 to 8883 in both thePorts
section of yourvalues.yaml
file and, in case you are exposing these ports via service, then update that file as well.Deploy the above changes to the Kubernetes cluster
Verify the logs to check if TLS is enabled or not
You will see the following logs if all changes are deployed correctly.
You can also test the connection via the MQTT CLI tool
Troubleshooting
Error: 'No subject alternative names present
Meaning: the server CA file, supplied by to the client, contains CN that is not the same as the--hostname
Example command:Reason: When the server.pem has CN that is not the IP address. For example, the server certificate has CN “example.domain.com”.
Workaround: On the client machine, edit the/etc/hosts
and append<ip-address> example.domain.com
. This way you can use the command successfully:Error: Unable to connect. Reason: 'No name matching localhost found'
Reason: the
--hostname
does not match the CN of the server.pem
Solution: Open the server.pem with a certificate viewer and find out the correct CN (hostname).