Getting started with the MySQL Operator for Kubernetes

Hassan Ajan
Oracle Developers
Published in
4 min readApr 24, 2019

Kubernetes Operators are amazing and they are already playing an important role for those who are managing large scale applications. I personally think that we will manage all applications using Operators.

In this tutorial I will show you how to setup a MySQL cluster using the MySQL Operator on Kubernetes.

Prerequisites:

  1. Install kubectl
  2. Install Helm
  3. Kubernetes Cluster: Use minikube locally or check out Luca’s post on how to set up OKE.

Let’s start with cloning the MySQL repository which contains the Operator.

$ git clone git@github.com:oracle/mysql-operator.git

Now enter the directory.

$ cd mysql-operator

Make sure your helm repo’s are up to date.

$ helm repo update

Let’s create a namespace where the operator will be installed.

$ kubectl create ns mysql-operator

Install the mysql operator.

$ helm install --name mysql-operator mysql-operator

You should see an output similar to this:

NAME:   mysql-operator
LAST DEPLOYED: Tue Apr 23 15:48:53 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1beta1/CustomResourceDefinition
NAME AGE
mysqlbackupschedules.mysql.oracle.com 4s
mysqlclusters.mysql.oracle.com 4s
mysqlbackups.mysql.oracle.com 4s
mysqlrestores.mysql.oracle.com 4s
==> v1beta1/ClusterRole
mysql-operator 4s
mysql-agent 4s
==> v1beta1/ClusterRoleBinding
NAME AGE
mysql-operator 3s
mysql-agent 3s
==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
mysql-operator 1 1 1 0 3s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
mysql-operator-d99c84c9-sldb7 0/1 ContainerCreating 0 3s
==> v1/ServiceAccount
NAME SECRETS AGE
mysql-agent 1 5s
mysql-operator 1 4s
NOTES:
Thanks for installing the MySQL Operator.
Check if the operator is running withkubectl -n mysql-operator get po

As the document states we can check the status of the cluster. Notice because we installed it in the namespace called mysql-operator we are using the -n flag.

$ kubectl -n mysql-operator get poNAME                            READY     STATUS    RESTARTS   AGE
mysql-operator-d99c84c9-sldb7 1/1 Running 0 2m

Our Operator is up and running.

Let’s create a new namespace where we will install the MySQL Cluster:

$ kubectl create ns mysql-cluster

Since we are creating the MySQL Cluster in a new namespace, which we have named “mysql-cluster”, we need to create themysql-agent ServiceAccount and RoleBinding in that namespace.

If you decide to name your namespace something else make sure to change it below also.

Let’s create the mysql-agentServiceAccount and RoleBinding:

$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: mysql-agent
namespace: mysql-cluster
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: mysql-agent
namespace: mysql-cluster
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: mysql-agent
subjects:
- kind: ServiceAccount
name: mysql-agent
namespace: mysql-cluster
EOF

You should see an output similar to this:

serviceaccount "mysql-agent" created
rolebinding "mysql-agent" created

Create a cluster.yaml file with a database name and namespace. I have named mine “my-first-db” and I’m using the namespace “mysql-cluster”.

apiVersion: mysql.oracle.com/v1alpha1
kind: Cluster
metadata:
name: my-first-db
namespace: mysql-cluster

Now let’s create the MySQL Cluster.

$ kubectl apply -f cluster.yaml
mysqlcluster "my-first-db" created

Notice below, that because we have installed the mysql-operator, kubernetes now have a new resource called “mysqlclusters”. Similar to how we write kubectl get pods we can now write kubectl get mysqlclusters.

We are still using the -n flag because we installed the mysql cluster in the namespace called “mysql-cluster”. I should perhaps have picked a different name to avoid confusing it with the resource name.

Let’s check our mysql clusters:

$ kubectl -n mysql-cluster get mysqlclusters
NAME AGE
my-first-db 32s

We can see that we have one mysql-cluster running.

Let’s check how many pods are running:

$ kubectl -n mysql-cluster get pods 
NAME READY STATUS RESTARTS AGE
my-first-db-0 2/2 Running 0 5m
my-first-db-1 2/2 Running 0 5m
my-first-db-2 2/2 Running 0 5m

As you can see our cluster consists of three pods. By default the cluster has three members. You can specify otherwise by adding spec: members: 3 to the cluster.yaml file. There are also other options but I will cover those in a different blog post.

Let’s get the root password for our new database. Notice the secret and configs automatically created by the operator are prefixed with database name we entered above in the cluster.yaml file.

$ kubectl -n mysql-cluster get secret my-first-db-root-password -o jsonpath="{.data.password}" | base64 --decode#output
FDlQQxPCVdMZ6lAt

Let’s create a temporary container that contains the MySQL client to test that we can connect to the newly created database.

Notice we are creating the container in the same namespace. If we don’t we won’t be able to use the hostname to reach the database created.

$ kubectl -n mysql-cluster run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- /bin/bashIf you don't see a command prompt, try pressing enter.
root@mysql-client:/#

Replace the password below with the password generated above.

$ mysql -h my-first-db -uroot -pFDlQQxPCVdMZ6lAt -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+

Congratulations. You have installed the MySQL Operator, created a MySQL Cluster and connected to it.

If you want to give this a try on Oracle Cloud sign up for a free trial and launch Kubernetes in a few minutes.

In case you run into problems or need advice setting up a production grade system feel free to reach out to me on twitter or linkedin.

--

--

Hassan Ajan
Oracle Developers

In quest of perfect IT-Infrastructure | Currently exploring Kubernetes | Love building automated Infra when I’m not laying on the beach in Morocco