Vitess Operator for Kubernetes

PlanetScale provides a Vitess Operator for Kubernetes, released under the Apache 2.0 license. The following steps show how to get started using Minikube:

Prerequisites

Before we get started, let’s get a few things out of the way:

  1. Install Minikube and start a Minikube engine. We recommend using Kubernetes 1.14, as this is a common denominator across public clouds:

    1. minikube start --kubernetes-version=v1.14.10 --cpus=8 --memory=11000 --disk-size=50g

    If you do not have a machine with 11GB of memory to spare, you could also consider using GKE instead. An equivalent setup can be deployed from the Cloud Shell with:

    1. gcloud container clusters create vitess --cluster-version 1.14 --zone us-east1-b --num-nodes 5
  2. Install kubectl and ensure it is in your PATH. For example, on Linux:

    1. curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.14.9/bin/linux/amd64/kubectl
  3. Install the MySQL client locally. For example, on Ubuntu:

    1. apt install mysql-client
  4. Install vtctlclient locally:

    If you are familiar with Go development, the easiest way to do this is:

    1. go get vitess.io/vitess/go/cmd/vtctlclient

    If not, you can also download the latest Vitess release and extract vtctlclient from it.

Install the Operator

Change to the operator example directory:

  1. git clone git@github.com:vitessio/vitess.git
  2. cd vitess/examples/operator

Install the operator:

  1. kubectl apply -f operator.yaml

Bring up an initial cluster

In this directory, you will see a group of yaml files. The first digit of each file name indicates the phase of example. The next two digits indicate the order in which to execute them. For example, 101_initial_cluster.yaml is the first file of the first phase. We shall execute that now:

  1. kubectl apply -f 101_initial_cluster.yaml

We have supplied an example yaml for bringing up Vitess with the experimental vtorc component. You can try this out by using the following command: kubectl apply -f vtorc_example.yaml. Once vtorc is officially released, the examples will be updated accordingly.

Verify cluster

You can check the state of your cluster with kubectl get pods. After a few minutes, it should show that all pods are in the status of running:

  1. $ kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. example-etcd-faf13de3-1 1/1 Running 0 78s
  4. example-etcd-faf13de3-2 1/1 Running 0 78s
  5. example-etcd-faf13de3-3 1/1 Running 0 78s
  6. example-vttablet-zone1-2469782763-bfadd780 3/3 Running 1 78s
  7. example-vttablet-zone1-2548885007-46a852d0 3/3 Running 1 78s
  8. example-zone1-vtctld-1d4dcad0-59d8498459-kwz6b 1/1 Running 2 78s
  9. example-zone1-vtgate-bc6cde92-6bd99c6888-vwcj5 1/1 Running 2 78s
  10. vitess-operator-8454d86687-4wfnc 1/1 Running 0 2m29s

Setup Port-forward

The port-forward will only forward to a specific pod. Currently, kubectl does not automatically terminate a port-forward as the pod disappears due to apply/upgrade operations. You will need to manually restart the port-forward.

For ease-of-use, Vitess provides a script to port-forward from Kubernetes to your local machine. This script also recommends setting up aliases for mysql and vtctlclient:

  1. ./pf.sh &
  2. alias vtctlclient="vtctlclient -server=localhost:15999"
  3. alias mysql="mysql -h 127.0.0.1 -P 15306 -u user"

Setting up aliases changes mysql to always connect to Vitess for your current session. To revert this, type unalias mysql && unalias vtctlclient or close your session.

Create Schema

Load our initial schema:

  1. vtctlclient ApplySchema -sql="$(cat create_commerce_schema.sql)" commerce
  2. vtctlclient ApplyVSchema -vschema="$(cat vschema_commerce_initial.json)" commerce

Connect to your cluster

You should now be able to connect to the VTGate Server in your cluster with the MySQL client:

  1. ~/vitess/examples/operator$ mysql
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 3
  4. Server version: 5.7.9-Vitess MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show databases;
  11. +-----------+
  12. | Databases |
  13. +-----------+
  14. | commerce |
  15. +-----------+
  16. 1 row in set (0.00 sec)

Summary

In this example, we deployed a single unsharded keyspace named commerce. Unsharded keyspaces have a single shard named 0. The following schema reflects a common ecommerce scenario that was created by the script:

  1. create table product(
  2. sku varbinary(128),
  3. description varbinary(128),
  4. price bigint,
  5. primary key(sku)
  6. );
  7. create table customer(
  8. customer_id bigint not null auto_increment,
  9. email varbinary(128),
  10. primary key(customer_id)
  11. );
  12. create table corder(
  13. order_id bigint not null auto_increment,
  14. customer_id bigint,
  15. sku varbinary(128),
  16. price bigint,
  17. primary key(order_id)
  18. );

The schema has been simplified to include only those fields that are significant to the example:

  • The product table contains the product information for all of the products.
  • The customer table has a customer_id that has an auto_increment. A typical customer table would have a lot more columns, and sometimes additional detail tables.
  • The corder table (named so because order is an SQL reserved word) has an order_id auto-increment column. It also has foreign keys into customer(customer_id) and product(sku).

Next Steps

You can now proceed with MoveTables.

Or alternatively, if you would like to teardown your example:

  1. kubectl delete -f 101_initial_cluster.yaml

Congratulations on completing this exercise!