How to Create a MySQL Kubernetes Service with a Locally Mounted Data Volume

How to Create a MySQL Kubernetes Service with a Locally Mounted Data Volume
Kubernetes has become a go-to platform for managing containerized applications at scale. One of the many benefits it offers is the ability to create and manage services like MySQL. In this tutorial, we’ll walk you through the process of creating a MySQL Kubernetes service with a locally mounted data volume. This setup allows you to persist data across pod restarts, ensuring your data’s durability and availability.
Prerequisites
Before we start, make sure you have the following:
- A running Kubernetes cluster
kubectl
installed and configured to interact with your cluster- A local directory to be used for data persistence
Step 1: Create a Persistent Volume
First, we need to create a Persistent Volume (PV) that points to the local directory. Here’s a sample YAML file for a PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local
local:
path: /path/to/your/local/directory
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- your-node-name
Replace /path/to/your/local/directory
with the path to your local directory and your-node-name
with the name of your node. Apply this configuration with kubectl apply -f pv.yaml
.
Step 2: Create a Persistent Volume Claim
Next, create a Persistent Volume Claim (PVC) that will request storage from the PV. Here’s a sample YAML file for a PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: local
resources:
requests:
storage: 1Gi
Apply this configuration with kubectl apply -f pvc.yaml
.
Step 3: Deploy MySQL
Now, let’s deploy MySQL using the PVC. Here’s a sample YAML file for a MySQL deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: yourpassword
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
Replace yourpassword
with your desired MySQL root password. Apply this configuration with kubectl apply -f mysql.yaml
.
Step 4: Expose MySQL Service
Finally, expose the MySQL service to access it from outside the cluster. Here’s a sample YAML file for a service:
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 30036
selector:
app: mysql
Apply this configuration with kubectl apply -f service.yaml
.
Conclusion
Congratulations! You’ve successfully created a MySQL Kubernetes service with a locally mounted data volume. This setup ensures that your data persists across pod restarts, providing a robust solution for data-intensive applications.
Remember, while this guide uses a local directory for data persistence, Kubernetes supports a variety of storage solutions. Always choose the one that best fits your project’s needs.
Stay tuned for more Kubernetes tutorials and tips. Happy coding!
Keywords: Kubernetes, MySQL, Persistent Volume, Persistent Volume Claim, Data Persistence, Kubernetes Service, Kubernetes Cluster, Local Directory, Data Volume, Containerized Applications, Data Durability, Data Availability, Storage Solutions, Kubernetes Tutorials, Kubernetes Tips, Kubernetes Node, MySQL Deployment, MySQL Service, MySQL Root Password, MySQL Kubernetes Service, Kubernetes Storage, Kubernetes Applications, Kubernetes Configuration, Kubernetes Setup, Kubernetes YAML, kubectl, MySQL Kubernetes Deployment, MySQL Kubernetes Configuration, MySQL Kubernetes Setup, MySQL Kubernetes YAML, MySQL kubectl, MySQL Data Persistence, MySQL Data Durability, MySQL Data Availability, MySQL Storage Solutions, MySQL Kubernetes Tutorials, MySQL Kubernetes Tips, MySQL Kubernetes Node.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.