Step-by-Step Guide to Deploying MySQL on GKE
To begin with, GKE provides a robust and managed environment for deploying and operating containerised applications, including databases like MySQL. In this guide, we’ll walk through the step-by-step process of setting up a MySQL database on GKE, covering everything from provisioning persistent storage to configuring MySQL settings and launching the database instance.
Setting Up the GKE Environment
Before we can deploy MySQL, we need a GKE cluster for it to run on. If you don’t have a GKE cluster already, you’ll need to create a new one.
1. Open the Google Cloud Console and go to the Kubernetes Engine section.
2. Click “Create Cluster” to start the cluster creation workflow.
3. Choose a name and configuration options for your cluster. Most defaults will work fine for running MySQL.
4. Once created, connect to the cluster using the Google Cloud SDK and configure kubectl to interact with the cluster.
Provisioning Persistent Storage
MySQL databases need persistent storage to reliably store their data. In GKE, we use PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs) to provision durable storage.
1. Decide what type of storage you want to use — a Google Cloud Filestore instance is a great option for MySQL.
2. Create a PersistentVolume that points to the desired storage.
3. Create a PersistentVolumeClaim that requests the desired storage capacity from the PersistentVolume.
Here’s an example PersistentVolumeClaim that requests a 10GB volume:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
— ReadWriteOnce
resources:
requests:
storage: 10Gi
Configuring MySQL Settings
Next, we need to configure the desired MySQL settings in a ConfigMap. This ConfigMap will be passed as an environment variable to the MySQL container.
Here’s an example ConfigMap with some common MySQL settings:
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
MYSQL_DATABASE: mydb
MYSQL_ROOT_PASSWORD: changeme
This ConfigMap creates a database named “mydb” and sets the root MySQL password to “changeme”. You’ll want to customize these for your use case.
Recommended by LinkedIn
Deploying the MySQL Database
With the persistent storage and configuration set, we’re finally ready to deploy the MySQL database itself using a Deployment.
Here’s an example Deployment that launches a MySQL 5.7 container with the persistent storage and configuration attached:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
— image: mysql:5.7
name: mysql
env:
— valueFrom:
configMapKeyRef:
name: mysql-config
key: MYSQL_DATABASE
— valueFrom:
configMapKeyRef:
name: mysql-config
key: MYSQL_ROOT_PASSWORD
ports:
— containerPort: 3306
name: mysql
volumeMounts:
— name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
— name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-data
This Deployment pulls the official MySQL 5.7 image, passes in the ConfigMap settings as environment variables, and mounts the persistent volume at /var/lib/mysql to store MySQL data.
To deploy this to the cluster, save the above YAML to a file (e.g. mysql.yaml) and run:
kubectl apply -f mysql.yaml
This will create the necessary Deployment, ConfigMap, and PersistentVolumeClaim resources in the cluster.
You can check that MySQL is running properly with:
kubectl get pods
Once the MySQL pod shows Running, the database is deployed!
Connecting to MySQL
To connect to the MySQL database from inside the cluster, you can use the kubectl exec command:
kubectl exec -it <mysql-pod-name> — /bin/bash
This will open a shell inside the running MySQL container. From there, you can use the mysql client to connect:
mysql -u root -p
And enter the root password you configured earlier.
To connect to MySQL from outside the GKE cluster, you’ll likely want to set up a Service of type LoadBalancer that exposes MySQL on an external IP address.
That’s it! We’ve covered the full end-to-end process for deploying a MySQL database on Google Kubernetes Engine. With MySQL running on the scalable and resilient GKE platform, you can reliably serve data to your applications.