Quick Links
Kubernetes support forstateful applicationshas considerably matured over the past few years. Now it’s viable to locate your database inside your cluster, allowing it to benefit from the same scalability as your other workloads.
MySQL is one of the most popular relational database engines and it’s now augmented by an officialKubernetes operator. The Oracle-ledopen-source projectprovides a simple way to create managed MySQL clusters within Kubernetes.
In this article, you’ll learn how to install the operator and get started provisioning a database. The operator automatically creates services so applications in your other containers can connect to MySQL.
What Is The MySQL Operator?
Oracle’s MySQL operator is a component that runs inside your cluster to automate database initialization. You don’t need the operator to use MySQL in Kubernetes - you could deploy theofficial container imageyourself byusing a StatefulSet. This approach is cumbersome though, requiring you to author and maintain long manifest files to create a reliable environment.
Theoperator providesa set of custom resources that you can use to create your databases. Adding anInnoDBClusterobject to your Kubernetes cluster prompts the operator to set up StatefulSets, storage, and networking for you. It also automates upgradesand backups, greatly reducing the burden on administrators.
Installing the MySQL Operator
The includedHelmchart is the simplest way to install the operator in your cluster. Manifest filesare availableas an alternative if you don’t have Helm in your environment.
First add the operator to your Helm repository list:
Next update Helm’s repository database so it discovers the available charts:
Now use the following command to install the operator into a new namespace calledmysql-operator:
–namespace mysql-operator \
–create-namespace
NAME: mysql-operator
NAMESPACE: mysql-operator
STATUS: deployed
REVISION: 1
TEST SUITE: None
It might take a few seconds for the process to complete.
Creating a Root User Credentials Secret
Each database cluster you create must be accompanied by aKubernetes secretthat contains the credentials for the MySQL root user. The operator will create the root-privileged account with the username and password provided in the secret.
Copy the following YAML manifest and change the value of therootPasswordfield to something secure. Alter the username anduser host settingstoo if applicable to your situation. Remember that the account named here will have total control over MySQL; you should set up separate users for your applications later on, using the regular MySQL shell.
kind: Secret
name: mysql-root-user
rootHost: “%”
rootUser: “root”
rootPassword: “P@$$w0rd”
Save the file assecret.yaml. Now use Kubectl to add the secret to your cluster:
secret/mysql-root-user created
Creating a Basic Cluster
Next create a new YAML file calledmysql.yamland copy the following content:
kind: InnoDBCluster
name: mysql-cluster
secretName: mysql-root-user
instances: 3
tlsUseSelfSigned: true
router:
instances: 1
This defines an object using theInnoDBClustercustom resource offered by the MySQL operator. 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 you continue:
Use Kubectl to apply the manifest and create your database cluster:
innodbcluster.mysql.oracle.com/mysql-cluster created
The MySQL initialization process can take a couple of minutes to complete. The operator uses severalinit containersto set up user accounts and configure MySQL’s data directory. Wait a few moments before runningkubectl get podsto check what’s running:
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 single router instance are all in theRunningstate. The database is now ready to use.
Connecting to Your Cluster
The MySQL operator creates a Kubernetes service that routes traffic from your application Pods to your database. The service gets assigned a hostname with the following format:
The correct hostname for the example cluster shown above ismysql-cluster.default.svc.cluster.local. Configure your applications to connect to MySQL at this address using port 3306 and the user credentials you defined in your secret.
Accessing Your Database Externally
You can access MySQL from outside your cluster using Kubectl’s port forwarding capabilities. Run the following command to open a new port forwarding session:
Replacemysql-clusterwith the name of theInnoDBClusteryou want to connect to. Now you may use your local tools to interact with your database:
Pressing Ctrl+C in the terminal window running thekubectl port-forwardcommand will close the connection.
Customizing MySQL Server Settings
you may supply anyMySQL config fileoptions that your application requires by setting thespec.mycnffield in yourInnoDBClusterobject’s manifest:
mycnf: |
[mysqld]
max_connections=500
innodb_ft_min_token_size=5
The operator will use the value of this field to write themy.cnffile in the filesystem of your database Pods.
Setting the Storage Capacity
The operator automatically creates aPersistent Volume (PV) and Persistent Volume Claim (PVC)to store your database’s data. It defaults to providing 2Gi of storage.
This can be changed using thedatadirVolumeClaimTemplatemanifest field which allows you to override properties of the PVC resource produced by the operator. Set theresources.requests.storageproperty to the capacity you require.
requests:
storage: 10Gi
You should set this high enough that there’s plenty of room for your data to grow in the future. The operator doesn’t support in-place volume resizes so you’ll see an error if you try to increase the capacity in the future. Switching to a bigger volume would necessitate manual migration steps.
Pinning the MySQL Version
You can pin to a specific MySQL release with thespec.versionandspec.router.versionfields. This will prevent unintentional automatic upgrades. Select the same version for your MySQL Pods and router instances to guarantee compatibility.
version: 8.0.31
Summary
Oracle’s MySQL Operator provides a convenient mechanism for running MySQL databases within a Kubernetes cluster. You can provision new replicated databases by creatingInnoDBClusterobjects. This is much simpler than manually creating StatefulSets and services that expose regular MySQL containers.
Using the operator doesn’t impede your ability to control MySQL or your Kubernetes environment. You can customize your Pods by supplying your own manifest properties in thespec.podSpecfieldand use your own MySQL config file with the steps shown above. The operator also offersintegrated backup support, letting you copy your database to external storage on a recurring schedule.