How to run MySQL on Kubernetes using Oracle's Operator
Kubernetes support for stateful applications has matured considerably over the past few years. It is now possible to co-locate your database within your cluster, allowing it to benefit from the same scalability as your other workloads.
MySQL, one of the most popular relational database engines, is now maintained by an official Kubernetes Operator. The Oracle-led open source project provides an easy way to create a managed MySQL cluster in Kubernetes.
In this article, you will learn how to install Operator
and start configuring the database. Operator
The service is automatically created so that applications in your other containers can connect to MySQL.
What are MySQL operators?
Oracle's MySQL operator is a component that runs inside the cluster to automate database initialization. You don't need an operations person to use MySQL in Kubernetes—you can StatefulSet
deploy the official container image yourself using . But this approach is cumbersome and requires you to write and maintain long manifest files to create a reliable environment.
The Operator provides a set of custom resources that can be used to create a database. InnoDBCluster
Adding the object to your Kubernetes cluster prompts the Operator to set up StatefulSets , storage , and networking for you . It also automates upgrades and backups, greatly reducing the burden on administrators.
Installing MySQL Operator
The included Helm chart is the easiest way to install in your cluster Operator
. If you don’t have Helm in your environment, you can use the manifest file as an alternative.
First add the operator to your list of Helm repositories:
$ helm repo add mysql-operator https://mysql.github.io/mysql-operator/
Next update Helm's repository database so that it can discover the available charts:
$ helm repo update
Now install the Operator into mysql-operator
a new namespace called with the following command:
$ helm install mysql-operator mysql-operator/mysql-operator \
--namespace mysql-operator \
--create-namespace
NAME: mysql-operator
LAST DEPLOYED: Sat Oct 29 15:00:26 2022
NAMESPACE: mysql-operator
STATUS: deployed
REVISION: 1
TEST SUITE: None
The process may take several seconds to complete.
Create Root User Credentials Key
Each database cluster you create must come with a Kubernetes secret that contains the credentials for the MySQL root user. The Operator will create a root privileged account using the username and password provided in the secret.
Copy the following YAML listing and rootPassword
change the values of the fields to secure values. You can also change the username and user hosts settings if applicable to your situation. Keep in mind that the account named here will have full control over MySQL; you should set up a separate user for your application later using the regular MySQL shell.
apiVersion: v1
kind: Secret
metadata:
name: mysql-root-user
stringData:
rootHost: "%"
rootUser: "root"
rootPassword: "P@$$w0rd"
Save the file as secret.yaml . Now add the secret to the cluster using Kubectl:
$ kubectl apply -f secret.yaml
secret/mysql-root-user created
Creating a Basic Cluster
Next create a new YAML file called mysql.yaml and copy the following content:
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
name: mysql-cluster
spec:
secretName: mysql-root-user
instances: 3
tlsUseSelfSigned: true
router:
instances: 1
This defines an object using a custom resource provided by the MySQL Operator InnoDBCluster
. Applying the manifest to your cluster will create a new MySQL database with replication automatically configured. Review and change the following properties in the manifest before continuing:
-
spec.secretName – This must match the name of the secret you created earlier
metadata.name
. The referenced secret will be read to set up the MySQL root user account. - spec.instances – This defines how many MySQL replicas (Pods) to run. Currently up to 10 replicas are supported.
-
spec.tlsUseSelfSigned – This field is set to true in this example to use the included self-signed TLS certificate. You can choose to provide your own certificate
spec.tlsSecretName/spec.tlsCASecretName
through a Kubernetes secret by setting the field. - spec.router.instances – The MySQL Router component is responsible for directing service traffic between available MySQL replicas. This field defines the number of router instances to run. Multiple instances will improve performance and resiliency in high traffic situations.
Use Kubectl to apply the manifest and create the database cluster:
$ kubectl apply -f mysql.yaml
innodbcluster.mysql.oracle.com/mysql-cluster created
The MySQL initialization process may take several minutes to complete. The Operator uses several init containers to set up user accounts and configure MySQL’s data directory. Wait a few moments, then kubectl get pods
check what’s running by running:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-cluster-0 2/2 Running 0 2m
mysql-cluster-1 2/2 Running 0 2m
mysql-cluster-2 2/2 Running 0 2m
mysql-cluster-router-6b68f9b5cb-wbqm5 1/1 Running 0 2m
The three MySQL replicas and the single router instance are in Running
the state. The database is now ready for use.
Connect to your cluster
The MySQL Operator creates a Kubernetes Service that routes traffic from your application Pods to your database. The Service is assigned a hostname in the following format:
<cluster-name>.<namespace-name>.svc.cluster.local
The correct hostname for the example cluster shown above is mysql-cluster.default.svc.cluster.local
. Configure your application to connect to MySQL at this address using port 3306 and the user credentials you defined in the secret.
Accessing your database from outside
You can use Kubectl's port forwarding feature to access MySQL from outside the cluster. Run the following command to open a new port forwarding session:
$ kubectl port-forward service/mysql-cluster 3306
Replace mysql-cluster
with InnoDBCluster
the name of the database you want to connect to. Now you can use local tools to interact with the database:
$ mysql -h127.0.0.1 -u root -p
kubectl port-forward
Pressing Ctrl+C in the terminal window
running the command will close the connection.
Customizing MySQL Server Settings
You can provide any MySQL configuration file options your application requires by InnoDBCluster
setting the fields in the manifest of the MySQL object :spec.mycnf
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
spec:
# ...
mycnf: |
[mysqld]
max_connections=500
innodb_ft_min_token_size=5
The Operator will use the value of this field to write the my.cnf file to the database Pod ’s file system.
Setting storage capacity
The Operator automatically creates a persistent volume (PV) and a persistent volume claim (PVC) to store database data. It provides 2Gi of storage space by default.
datadirVolumeClaimTemplate
This can be changed
using the manifest field, which allows you to override the properties of the PVC resource generated by the Operator. resources.requests.storage
Set the property to the capacity you require.
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
spec:
# ...
datadirVolumeClaimTemplate:
resources:
requests:
storage: 10Gi
You should set this value high enough so that your data has enough room to grow in the future. The Operator does not support in-place resizing of volumes, so you will see errors if you try to increase capacity in the future. Switching to a larger volume will require a manual migration step.
Fixed MySQL version
You can pin to a specific MySQL version using the spec.version
and spec.router.version
fields. This will prevent inadvertent automatic upgrades. Choose the same version for your MySQL Pod and router instance to ensure compatibility.
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
spec:
# ...
version: 8.0.31
router:
instances: 1
version: 8.0.31
Summarize
Oracle's MySQL Operator provides a convenient mechanism for running a MySQL database in a Kubernetes cluster. You can InnoDBCluster
provision a new replicated database by creating a MySQL object. This StatefulSet
is much simpler than manually creating a MySQL container and a service that exposes a regular MySQL container.
Using the Operator does not hinder your ability to control your MySQL or Kubernetes environment. You can spec.podSpec
customize the Pod by providing your own manifest attributes in the field and use your own MySQL configuration file by following the steps above. The Operator also provides integrated backup support , allowing you to periodically copy the database to external storage.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Two ways to install mysql-5.5.47 on Linux system and manage mysql
Publish Date:2025/04/26 Views:140 Category:MySQL
-
We know that there are generally two ways to install software on the Linux system. One is to use rpm or yum to install, which is convenient and fast; the other is to use the compiled source package. Although this method is more troublesome
Mysql master-slave replication simple configuration
Publish Date:2025/04/26 Views:120 Category:MySQL
-
I'm learning about MySQL master-slave replication recently and want to apply it to a project at work. Since I'm new to it, I don't understand it very well, so I can only share how to make a simple configuration. At the beginning, we configu
Implementation details of MySQL master-slave replication (I) Exploration of the m
Publish Date:2025/04/26 Views:56 Category:MySQL
-
This article mainly discusses the implementation mechanism of master-slave replication, which may not be directly helpful for our actual application, but understanding its principle can achieve twice the result with half the effort for futu
Implementation details of MySQL master-slave replication (II) Exploration from th
Publish Date:2025/04/26 Views:74 Category:MySQL
-
Previously we explored the master server in master-slave replication. Now let's look at how the slave server works in the entire system. In the article Master Server Exploration, we mentioned that three threads are needed in a master-slave
Mysql master-slave replication - what to do if the slave server stops
Publish Date:2025/04/26 Views:97 Category:MySQL
-
You may find this topic a little ridiculous. What can we do if the server stops? Of course, we should restart the service. Restarting the service is no problem. The problem is that a lot of data has been written to the master database durin
MySQL stored procedure details
Publish Date:2025/04/26 Views:163 Category:MySQL
-
A stored procedure can be thought of as encapsulating a SQL statement that we need to process specially into a function. When needed, we only need to call this function to achieve the desired operation. This process can be called a stored p
How many of these MySQL statement tags have you used?
Publish Date:2025/04/26 Views:122 Category:MySQL
-
In the article "A Peek into MySQL Stored Procedure Details" , we briefly introduced the use of stored procedures. The syntax for creating stored procedures includes BEGIN...END. In addition to BEGIN...END, the following statement tags can b
MySQL CURRENT_TIMESTAMP() Function Detailed Introduction (with Examples)
Publish Date:2025/04/26 Views:132 Category:MySQL
-
This article explains how to use MySQL functions with examples CURRENT_TIMESTAMP() . By using it, we can convert or display the current date and time. The output format is "YYYY-MM-DD HH:MM:SS" or "YYYYMMDDHHMMSS", depending on the context
Back up the MySQL database to a file
Publish Date:2025/04/26 Views:166 Category:MySQL
-
Backing up your database is a very important system administration task that should usually cron be run from a job at scheduled intervals. We will use mysqldump the dump utility that comes with mysql to dump the contents of the database to