Dependences
- Installed K3s on a Raspberry Pi[^1]
- Installed kubectl on your local computer.
- A MySQL client. (for testing)
Install MySQL on Kubernetes
We will create configures and apply them to create a MySQL service. All configure files below must be applied after created with command:
1 | kubectl apply -f <config-name>.yaml |
All commands below are omitted.
Prepare MySQL Data Storage
Let us create a volume for our MySQL service to store data.[^2]
At first, we need create a directory on node with command:
1 | mkdir /mnt/data |
Then, create a configure file for persistent volume.
1 | # pv.yaml |
After created, we can claim it on Kubernetes. Here is the config file:
1 | # pvc.yaml |
Adding A Secret with MySQL Password
The passsword of mysql we will use later should be injected into the pod environment by using the Secret.
Note that the value must be encoded using base64[^3].
Says that the password is “mypassword”.
1 | echo -n 'mypassword' | base64 |
The output bXlwYXNzd29yZA==
should be put into our secret configure file.
1 | # secret.yaml |
Creating Deployment with a MySQL Container
- Specifying image to create a container in pod.
- Using PersistentVolumeClaim as volume.
- Using Secret as environment variables.
1 | # deployment.yaml |
Expose the Service
After the deployment applied and the pod is running. The MySQL service is not exposed to the node. We need to create a Service to listen the port on node.
1 | # service.yaml |
Have a Try
Now, you can connet to your new database with a MySQL client.
1 | mysql -P 30006 -h <raspberry-pi-host> -u root -p |
All done!
[^1]: An article can help for your installation.
[^2]: You can reference here to configure persistent volume storage.
[^3]: Follow the example in the official document.