3.2 Deploy using kubectl

To create an InnoDB Cluster with kubectl, first create a secret containing credentials for a new MySQL root user, a secret named 'mypwds' in this example:

$> kubectl create secret generic mypwds \
        --from-literal=rootUser=root \
        --from-literal=rootHost=% \
        --from-literal=rootPassword="sakila"

Use that newly created user to configure a new MySQL InnoDB Cluster. This example's InnoDBCluster definition creates three MySQL server instances and one MySQL Router instance:

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: mycluster
spec:
  secretName: mypwds
  tlsUseSelfSigned: true
  instances: 3
  router:
    instances: 1

Assuming a file named mycluster.yaml contains this definition, install this simple cluster:

$> kubectl apply -f mycluster.yaml

Optionally observe the process by watching the innodbcluster type for the default namespace:

$> kubectl get innodbcluster --watch

Output looks similar to this:

NAME          STATUS    ONLINE   INSTANCES   ROUTERS   AGE
mycluster     PENDING   0        3           1         10s

Until reaching ONLINE status:

NAME        STATUS   ONLINE   INSTANCES   ROUTERS   AGE
mycluster   ONLINE   3        3           1         2m6s

To demonstrate, this example connects with MySQL Shell to show the host name:

$> kubectl run --rm -it myshell --image=container-registry.oracle.com/mysql/community-operator -- mysqlsh root@mycluster --sql
If you don't see a command prompt, try pressing enter.
******

MySQL mycluster SQL> SELECT @@hostname

+-------------+
| @@hostname  |
+-------------+
| mycluster-0 |
+-------------+

This shows a successful connection that was routed to the mycluster-0 pod in the MySQL InnoDB Cluster. For additional information about connecting, see Chapter 5, Connecting to MySQL InnoDB Cluster.