Migrating your K3s cluster from SQLite to etcd involves shifting your data storage and cluster coordination from a local SQLite database to a distributed, consistent, and highly available key-value store. This transition offers improved scalability and reliability for your container orchestration platform. Let's explore the process in detail.

Before diving into the migration process, ensure you understand the differences between SQLite and etcd. SQLite is a file-based database designed for simplicity and ease of use, making it a great choice for small-scale setups and development environments. On the other hand, etcd is purpose-built for distributed systems, providing strong consistency and high availability features that make it the ideal choice for production Kubernetes and K3s clusters.

Preparation for Migration
Before migrating, assess your cluster's current state and plan your migration strategy. ensuring minimal downtime and data loss is crucial. Here are two key steps to prepare for the migration:

Evaluate Your Cluster's Health
Start by checking your cluster's overall health, including the status of your nodes, pods, and services. Tools like the Kubernetes Dashboard, Rancher, or Kubectl commands can help you monitor your cluster's state. Make sure your cluster is in a stable condition before proceeding with the migration.

Review your cluster's logs and events for any signs of instability or issues. Address any hauts or misconfigurations that might cause problems during the migration process.
Backup Your Data
Although both SQLite and etcd store data in key-value pairs, the data formats are different. To avoid data loss, it's essential to back up your existing SQLite data before migrating. Use the following command to backup your SQLite data:

kubectl exec -n kube-system k3s -c k3s -- backup sqlite
Install and Configure etcd
Before migrating your data, you need to install and configure etcd for your K3s cluster. Here's a step-by-step guide to help you through this process:
Install etcd

Start by installing etcd on your K3s master node. You can use a package manager like apt (for Debian/Ubuntu) or yum (for CentOS/RHEL) to install etcd. Alternatively, you can use the following commands to install etcd using snap (for Ubuntu):
sudo snap install etcd snap refresh etcd
Configure etcd




















After installing etcd, configure it by editing the systemd unit file located at /etc/systemd/system/etcd.service. Set the appropriate values for the following environment variables:
- ETCD_NAME: Set this value to the hostname of your K3s master node.
- ETCD_DATA_DIR: Specify the directory where etcd will store its data (e.g., /var/lib/etcd.default).
- ETCD_LISTEN_PEER_URLS: Set this value to the IP address and port number where etcd listens for peer traffic (e.g., http://localhost:2380).
- ETCD_INITIAL_ADVERTISE_PEER_URLS: Set this value to the same IP address and port number as above.
- ETCD_LISTEN_CLIENT_URLS: Set this value to the IP address and port number where etcd listens for client traffic (e.g., http://localhost:2379).
- ETCD_INITIAL_ADVERTISE_CLIENT_URLS: Set this value to the same IP address and port number as above.
After configuring etcd, save the file and restart the service using the following command:
sudo systemctl daemon-reload sudo systemctl restart etcd
Test etcd
Before proceeding with the migration, test your etcd installation to ensure it's working correctly. Use the following command to create a temporary key-value pair in etcd:
etcdctl set key 'value' etcdctl get key
If the test is successful, remove the temporary key using the following command:
etcdctl del key
Migrating Data from SQLite to etcd
Now that you have etcd up and running, it's time to migrate your data from SQLite. K3s provides a built-in tool called 'k3s migrate' for this purpose. Here's how to use it:
Run the Migration Tool
Execute the following command to run the 'k3s migrate' tool. This command will migrate your data from SQLite to etcd:
k3s migrate sqlite etcd
The migration process might take some time to complete, depending on the size of your data. Monitor the progress using the following command:
kubectl get cm -n kube-system k3s entrenv kubectl logs -n kube-system k3s
Verify the Migration
After the migration is complete, verify that your data has been successfully moved to etcd. You can use the 'k3s migrate status' command to check the migration status:
k3s migrate status
If the migration was successful, the output should indicate that both SQLite and etcd are enabled. If there are any errors or warnings, address them accordingly.
Congratulations! You have successfully migrated your K3s cluster from SQLite to etcd. This migration process ensures improved scalability and reliability for your container orchestration platform. Keep an eye on your cluster's performance and health, and make any necessary adjustments as needed.
With the migration complete, consider exploring other features and improvements offered by K3s, such as its simplified architecture, lightweight design, and easy-to-use k3sctl tool for cluster management. Happy orchestrating!