Deploying MySQL on Kubernetes


Deploying MySQL on Kubernetes

In the dynamic landscape of modern application development, deploying and managing databases efficiently is crucial. Kubernetes, with its container orchestration capabilities, has become a go-to solution for deploying and scaling applications. In this article, we'll explore the process of deploying MySQL, a popular open-source relational database, on Kubernetes. Follow along for a step-by-step guide to seamlessly integrate MySQL into your Kubernetes cluster.

Setting up your Kubernetes Cluster:
Before diving into MySQL deployment, ensure you have a functional Kubernetes cluster. If you don't have one yet, you can use a tool like Minikube for local development or set up a cluster on cloud providers like Google Kubernetes Engine (GKE) or Amazon EKS. Once your cluster is up and running, let's proceed to the next steps.

Creating a Persistent Volume:
MySQL, being a database, requires persistent storage to store its data. In Kubernetes, this is achieved through Persistent Volumes (PV) and Persistent Volume Claims (PVC). Create a YAML file for your PV and PVC, specifying the storage capacity and access modes. Apply these configurations to your cluster:

# persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"

---

# persistent-volume-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

Apply the configurations:

kubectl apply -f persistent-volume.yaml
kubectl apply -f persistent-volume-claim.yaml

Deploying MySQL:
With the storage in place, let's proceed to deploy MySQL. We'll use a Kubernetes Deployment to manage the MySQL instance. Create a MySQL Deployment YAML file with the following content:

# mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: "your-root-password"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc

Apply the MySQL Deployment:

kubectl apply -f mysql-deployment.yaml

This YAML configuration defines a MySQL Deployment with a single replica, sets the root password, and mounts the persistent storage.

Accessing MySQL from Outside the Cluster:
To access MySQL from outside the Kubernetes cluster, you'll need to create a Kubernetes Service. Create a file named mysql-service.yaml:

# mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306
type: LoadBalancer

Apply the service configuration:

kubectl apply -f mysql-service.yaml

This service exposes MySQL on a specified port and makes it accessible outside the cluster.

Connecting to MySQL:
Retrieve the external IP of the MySQL service:

kubectl get services mysql-service

Use a MySQL client to connect to MySQL using the external IP:

mysql -h <external-ip> -u root -p

Enter the root password when prompted.

Congratulations! You have successfully deployed MySQL on Kubernetes, allowing seamless database management within your containerized environment. This setup provides scalability, resilience, and ease of management, making it an ideal solution for modern applications.

Related Searches and Questions asked:

  • Install Elasticsearch on Kubernetes Using Helm Chart
  • Istio Tutorial: Getting Started with Istio Basics
  • How to Set Up and Run Kafka on Kubernetes
  • How to Fix Helm "Has No Deployed Releases" Error
  • That's it for this topic, Hope this article is useful. Thanks for Visiting us.