PHP Developer News

Getting started with the MySQL Operator for Kubernetes


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:

Install kubectl

Install Helm

Kubernetes Cluster: Use minikube locally or launch your own Kubernetes Cluster in 5–10 minutes on Oracle


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-operatorLAST DEPLOYED: Tue Apr 23 15:48:53 2019NAMESPACE: defaultSTATUS: DEPLOYED
RESOURCES:==> v1beta1/CustomResourceDefinitionNAME AGEmysqlbackupschedules.mysql.oracle.com 4smysqlclusters.mysql.oracle.com 4smysqlbackups.mysql.oracle.com 4smysqlrestores.mysql.oracle.com 4s
==> v1beta1/ClusterRolemysql-operator 4smysql-agent 4s
==> v1beta1/ClusterRoleBindingNAME AGEmysql-operator 3smysql-agent 3s
==> v1beta1/DeploymentNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEmysql-operator 1 1 1 0 3s
==> v1/Pod(related)NAME READY STATUS RESTARTS AGEmysql-operator-d99c84c9-sldb7 0/1 ContainerCreating 0 3s
==> v1/ServiceAccountNAME SECRETS AGEmysql-agent 1 5smysql-operator 1 4s
NOTES:Thanks for installing the MySQL Operator.
Check if the operator is running with
kubectl -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 po
NAME READY STATUS RESTARTS AGEmysql-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: v1kind: ServiceAccountmetadata: name: mysql-agent namespace: mysql-cluster---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1beta1metadata: name: mysql-agent namespace: mysql-clusterroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: mysql-agentsubjects:- kind: ServiceAccount name: mysql-agent namespace: mysql-clusterEOF
You should see an output similar to this:
serviceaccount "mysql-agent" createdrolebinding "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/v1alpha1kind: Clustermetadata: name: my-first-db namespace: mysql-cluster
Now let’s create the MySQL Cluster.
$ kubectl apply -f cluster.yamlmysqlcluster "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 mysqlclustersNAME AGEmy-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 AGEmy-first-db-0 2/2 Running 0 5mmy-first-db-1 2/2 Running 0 5mmy-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
#outputFDlQQxPCVdMZ6lAt
Let’s create a temporary container that contains the MySQL client to test that we can connect to the newly created database.
Notice we 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/bash
If 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.