Enable Persistence In EMQX Cluster
Task Target
Configure EMQX 5.x cluster Core node persistence through volumeClaimTemplates field.
Configure EMQX Cluster Persistence
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 configuration of EMQX cluster Core node persistence through .spec.coreTemplate.spec.volumeClaimTemplates field. The semantics and configuration of .spec.coreTemplate.spec.volumeClaimTemplates field are consistent with PersistentVolumeClaimSpec of Kubernetes, and its configuration can refer to the document: PersistentVolumeClaimSpec.
When the user configures the .spec.coreTemplate.spec.volumeClaimTemplates field, EMQX Operator will mount the /opt/emqx/data directory in the EMQX container to StorageClass in the PV and PVC created, when the EMQX Pod is deleted, the PV and PVC will not be deleted, so as to achieve the purpose of saving EMQX runtime data. For more information about PV and PVC, refer to the document Persistent Volumes.
Save the following content as a YAML file and deploy it via the
kubectl applycommandyamlapiVersion: apps.emqx.io/v2beta1 kind: EMQX metadata: name: emqx spec: image: emqx/emqx-enterprise:6.0.0 config: data: | license { key = "..." } coreTemplate: spec: volumeClaimTemplates: storageClassName: standard resources: requests: storage: 20Mi accessModes: - ReadWriteOnce replicas: 3 listenersServiceTemplate: spec: type: LoadBalancer dashboardServiceTemplate: spec: type: LoadBalancerstorageClassNamefield indicates the name of the StorageClass. You can use the commandkubectl get storageclassto get the StorageClass that already exists in the Kubernetes cluster, or you can create a StorageClass according to your own needs.Wait for EMQX cluster to be ready, you can check the status of the EMQX cluster through
kubectl getcommand, please make sureSTATUSisRunning, this may take some timebash$ kubectl get emqx emqx NAME IMAGE STATUS AGE emqx emqx/emqx-enterprise:6.0.0 Running 10mObtain the Dashboard External IP of the EMQX cluster and access the 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.200Access
http://192.168.1.200:18083through a browser, and use the default username and passwordadmin/publicto login EMQX console.
Verify EMQX Cluster Persistence
Verification scheme: 1) Passed in the old EMQX Dashboard creates a test rule; 2) Deletes the old cluster; 3) Recreates the EMQX cluster,and checks whether the previously created rule exists through the Dashboard.
Access EMQX Dashboard through browser to create test rules
bashexternal_ip=$(kubectl get svc emqx-listeners -o json | jq '.status.loadBalancer.ingress[0].ip')Login EMQX Dashboard by accessing
http://${external_ip}:18083, and click Data Integration → Rules to enter the page for creating rules. Let’s first click the button to add an action Add a response action for this rule, and then click Create to generate a rule, as shown in the following figure:
When our rule is successfully created, a rule record will appear on the page with the rule ID: emqx-persistent-test, as shown in the figure below:

delete old EMQX cluster
Execute the following command to delete the EMQX cluster:
bash$ kubectl delete -f emqx.yaml emqx.apps.emqx.io "emqx" deleted # emqxenterprise.apps.emqx.io "emqx" deletedemqx-persistent.yaml is the YAML file used to deploy the EMQX cluster for the first time in this article, and this file does not need to be changed.
Recreate the EMQX cluster
Execute the following command to recreate the EMQX cluster:
bash$ kubectl apply -f emqx.yaml emqx.apps.emqx.io/emqx created # emqxenterprise.apps.emqx.io/emqx createdWait for the EMQX cluster to be ready, and then access the EMQX Dashboard through the browser to check whether the previously created rules exist, as shown in the following figure:

It can be seen from the figure that the rule emqx-persistent-test created in the old cluster still exists in the new cluster, which means that the persistence we configured is in effect.