In this guide, we will deploy SurrealDB to KIND (Kubernetes in Docker) using TiKV as the storage engine. TiKV is a cloud-native transactional key/value store that integrates well with Kubernetes thanks to their TiDB-operator tool.
At the end, we will run a few experiments using SurrealQL to verify that we can interact with the new cluster and then delete some Kubernetes pods to demonstrate that the data remains available.
Requirements
For this guide, we need to install:
kubectl: To manage the Kubernetes clusterhelm: To install SurrealDB server and TiKVSurreal CLI: To interact with the SurrealDB server
Create KIND Cluster
First, we need to create a KIND cluster. KIND is a tool for running local Kubernetes clusters using Docker container “nodes”. It’s a great tool for experimenting with Kubernetes without spending a lot of time creating a full-featured cluster.
1. Create a new cluster
Run the following command to create a cluster:
2. Verify interaction with cluster
Run the following command to verify that we can interact with the created cluster:
The output of this command should be:
3. Verify that nodes are running
Run the following command to verify that the nodes are running:
The output of this command should be:
Deploy TiDB operator
Now that we have a Kubernetes cluster, we can deploy the TiDB operator. TiDB operator is a Kubernetes operator that manages the lifecycle of TiDB clusters deployed to Kubernetes.
You can deploy it following these steps:
Install CRDS:
Install TiDB Operator Helm chart:
Verify that the pods are running:
The output of this command should look like this:
Create TiDB cluster
Now that we have the TiDB Operator running, it’s time to define a TiDB Cluster and let the Operator do the rest. One of the TiDB Cluster components is the TiKV, which we are interested in. Given this is a demo, we will use a basic example cluster, but there are several examples in the official GitHub repo in case you need a more production-grade deployment
Run the following commands to deploy the TiKV cluster:
1. Create TiDB cluster namespace
Run the following command to create a namespace for the TiDB cluster:
2. Create TiDB cluster
Run the following command to create the TiDB cluster:
3. Check cluster status
Run the following command to check the cluster status and wait until it’s ready:
The output of this command should look like this:
Deploy SurrealDB
Now that we have a TiDB cluster running, we can deploy SurrealDB. For this guide, we will use the SurrealDB Helm chart. Run the following commands to deploy SurrealDB:
1. Add SurrealDB Charts repository
Run the following command to add the SurrealDB Charts repository:
2. Get TiKV PD service URL
Run the following command to get the TiKV PD service URL:
The output of this command should look like this:
Then set the TIKV_URL variable to the PD service URL:
3. Install SurrealDB Helm chart to create initial credentials
Now we can install the SurrealDB Helm chart with TIKV_URL defined above and auth disabled so that we can create the initial credentials:
4. Connect to cluster and define initial credentials
We can then connect to the cluster and define the initial credentials (see the section below to connect):
Verify you can connect to the database with the new credentials:
5. Enable authentication
Authentication can be enabled now that the initial credentials have been created:
Run SurrealDB experiments
Now that we have SurrealDB running, we can run some experiments to verify that everything is working as expected. For this guide, we will use the Surreal CLI. Run the following commands to run some experiments:
1. Start port forwarding to the SurrealDB service
2. Connect to the SurrealDB server
Use the following command to connect to the SurrealDB server using the CLI from another shell:
3. Create a SurrealDB database
Use the following to create a SurrealDB database and try out a few queries:
The data created above has been persisted to the TiKV cluster. Let’s verify it by deleting the SurrealDB server and let Kubernetes recreate it.
We will first use get pod to get the names of the existing pods:
The output of this command should look like this:
We can then grab the name and use delete pod to delete it.
A get pod command shows us that the pod has been recreated under a different name.
We can now connect again and verify the data is still there (you may need to re-run the port-forwarding command):
Note
Conclusion
This guide demonstrated how to deploy SurrealDB on Kubernetes using TiKV as a datastore. From here, you could try and deploy to EKS, GKE or AKS, and play with the different configurations for the TiKV cluster.