Enable TLS In EMQX
Task Target
Customize TLS certificates via the extraVolumes
and extraVolumeMounts
fields.
Create Secret Based On TLS Certificate
Secret is an object that contains a small amount of sensitive information such as passwords, tokens, or keys. For its documentation, please refer to: Secret. In this article, we use Secret to save TLS certificate information, so we need to create Secret based on TLS certificate before creating EMQX cluster.
Save the following as a YAML file and deploy it with the
kubectl apply
commandyamlapiVersion: v1 kind: Secret metadata: name: emqx-tls type: kubernetes.io/tls stringData: ca.crt: | -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- tls.crt: | -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- tls.key: | -----BEGIN RSA PRIVATE KEY----- ... -----END RSA PRIVATE KEY-----
ca.crt
indicates the content of the CA certificate,tls.crt
indicates the content of the server certificate, andtls.key
indicates the content of the server private key. In this example, the contents of the above three fields are omitted, please fill them with the contents of your own certificate.
Configure EMQX Cluster
The following is the relevant configuration of EMQX Custom Resource. You can choose the corresponding APIVersion according to the version of EMQX you want to deploy. For the specific compatibility relationship, please refer to EMQX Operator Compatibility:
apps.emqx.io/v2beta1 EMQX
supports .spec.coreTemplate.extraVolumes
and .spec.coreTemplate.extraVolumeMounts
and .spec.replicantTemplate.extraVolumes
and .spec.replicantTemplate.extraVolumeMounts
fields to EMQX The cluster configures additional volumes and mount points. In this article, we can use these two fields to configure TLS certificates for the EMQX cluster.
There are many types of Volumes. For the description of Volumes, please refer to the document: Volumes. In this article we are using the secret
type.
Save the following as a YAML file and deploy it with the
kubectl apply
commandyamlapiVersion: apps.emqx.io/v2beta1 kind: EMQX metadata: name: emqx spec: image: emqx:5 config: data: | listeners.ssl.default { bind = "0.0.0.0:8883" ssl_options { cacertfile = "/mounted/cert/ca.crt" certfile = "/mounted/cert/tls.crt" keyfile = "/mounted/cert/tls.key" gc_after_handshake = true handshake_timeout = 5s } } coreTemplate: spec: extraVolumes: - name: emqx-tls secret: secretName: emqx-tls extraVolumeMounts: - name: emqx-tls mountPath: /mounted/cert replicantTemplate: spec: extraVolumes: - name: emqx-tls secret: secretName: emqx-tls extraVolumeMounts: - name: emqx-tls mountPath: /mounted/cert dashboardServiceTemplate: spec: type: LoadBalancer listenersServiceTemplate: spec: type: LoadBalancer
The
.spec.coreTemplate.extraVolumes
field configures the volume type as: secret, and the name as: emqx-tls.The
.spec.coreTemplate.extraVolumeMounts
field configures the directory where the TLS certificate is mounted to EMQX:/mounted/cert
.The
.spec.config.data
field configures the TLS listener certificate path. For more TLS listener configurations, please refer to the document: Configuration Manual.Wait for EMQX cluster to be ready, you can check the status of EMQX cluster through the
kubectl get
command, please make sure thatSTATUS
isRunning
, this may take some timebash$ kubectl get emqx NAME IMAGE STATUS AGE emqx emqx:5.1 Running 10m
Obtain the External IP of EMQX cluster and access EMQX console
EMQX Operator will create two EMQX Service resources, one is emqx-dashboard and the other is emqx-listeners, corresponding to EMQX console and EMQX listening port respectively.
bash$ kubectl get svc emqx-dashboard -o json | jq '.status.loadBalancer.ingress[0].ip' 192.168.1.200
Access
http://192.168.1.200:18083
through a browser, and use the default username and passwordadmin/public
to login EMQX console.
Verify TLS Connection Using MQTT X CLI
MQTT X CLI is an open source MQTT 5.0 command line client tool, designed to help developers to more Quickly develop and debug MQTT services and applications.
Obtain the External IP of EMQX cluster
bashexternal_ip=$(kubectl get svc emqx-listeners -o json | jq '.status.loadBalancer.ingress[0].ip')
Subscribe to messages using MQTT X CLI
bashmqttx sub -h ${external_ip} -p 8883 -t "hello" -l mqtts --insecure [10:00:25] › … Connecting... [10:00:25] › ✔ Connected [10:00:25] › … Subscribing to hello... [10:00:25] › ✔ Subscribed to hello
Create a new terminal window and publish a message using the MQTT X CLI
bashmqttx pub -h ${external_ip} -p 8883 -t "hello" -m "hello world" -l mqtts --insecure [10:00:58] › … Connecting... [10:00:58] › ✔ Connected [10:00:58] › … Message Publishing... [10:00:58] › ✔ Message published
View messages received in the subscribed terminal window
bash[10:00:58] › payload: hello world