Vttestserver Docker Image

This guide covers using the vttestserver docker image for testing purposes. This is also the docker image that we use for testing in Vitess Framework Testing.

Get the docker image

The first step is to get the docker image. There are two ways of doing this :

1. From the vitessio/vitess repository

Check out the vitessio/vitess repository

Clone the GitHub repository via:

  • SSH: git clone git@github.com:vitessio/vitess.git, or:
  • HTTP: git clone https://github.com/vitessio/vitess.git
  1. cd vitess

Build the docker image

In your shell, execute:

  1. make docker_vttestserver

This creates 2 docker images named vitess/vttestserver:mysql57 and vitess/vttestserver:mysql80

2. Pulling from docker hub

Alternately, you can get the latest docker images from the docker hub. In your shell, execute:

  1. docker pull vitess/vttestserver:mysql57
  2. docker pull vitess/vttestserver:mysql80

Run the docker image

At this point, you should have a docker image named vitess/vttestserver:mysql57 or vitess/vttestserver:mysql80.

Environment variables

The docker image expects some of the environment variables to be set to function properly. The following table lists all the environment variables available along with their usages.

Environment variableRequiredUse
KEYSPACESyesSpecifies the names of the keyspaces to be created as a comma separated value.
NUM_SHARDSyesSpecifies the number of shards in each keyspace. It is a comma separated value as well, read in conjunction with the KEYSPACES.
PORTyesThe starting of the port addresses that vitess will use to register its components like vtgate, etc.
MYSQL_MAX_CONNECTIONSnoMaximum number of connections that the MySQL instance will support. If unspecified, it defaults to 1000.
MYSQL_BIND_HOSTnoWhich host to bind the MySQL listener to. If unspecified, it defaults to 127.0.0.1.
MYSQL_SERVER_VERSIONnoMySQL server version to advertise. If unspecified, it defaults to 8.0.31-vitess or 5.7.9-vitess according to the version of vttestserver run.
CHARSETnoDefault charset to use. If unspecified, it defaults to utf8mb4.
FOREIGN_KEY_MODEnoThis is to provide how to handle foreign key constraint in create/alter table. Valid values are: allow (default), disallow.
ENABLE_ONLINE_DDLnoAllow users to submit, review and control Online DDL. Valid values are: true (default), false.
ENABLE_DIRECT_DDLnoAllow users to submit direct DDL statements. Valid values are: true (default), false.
PLANNER_VERSIONnoSets the default planner to use when the session has not changed it. Valid values are: Gen4 (default), v3, Gen4Greedy and Gen4Fallback. Gen4Fallback tries the new gen4 planner and falls back to the V3 planner if the gen4 fails.
TABLET_REFRESH_INTERVALnoInterval at which vtgate refreshes tablet information from topology server.

Environment variables in docker can be specified using the -e aka --env flag.

Sending queries to vttestserver container from outside

The vtgate listens for MySQL connections on 3 + the PORT environment variable specified. i.e. if you specify PORT to be 33574, then vtgate will be listening to connections on 33577, on the MYSQL_BIND_HOST which defaults to localhost. But this port will be on the docker container side. To connect to vtgate externally from a MySQL client, you will need to publish that port as well and specify the MYSQL_BIND_HOST to 0.0.0.0. This can be done via the -p aka --publish flag to docker. For eg: adding -p 33577:33577 to the docker run command will publish the container’s 33577 port to your local 33577 port, which can now be used to connect to the vtgate.

Persisting container data

If you wish to keep the state of the test container across reboots, such as when running the vttestserver container as a database container in local application development environments, you may optionally pass the --persistent_mode flag, along with a --data_dir directory which is bound to a docker volume. Due to a bug, the --port argument must also be present for correct operation.

When running in this mode, underlying MySQL table schemas, their data, and the Vitess VSchema objects are persisted under the provided --data_dir.

For example:

  1. docker run --name=vttestserver \
  2. -p 33577:33577 \
  3. --health-cmd="mysqladmin ping -h127.0.0.1 -P33577" \
  4. --health-interval=5s \
  5. --health-timeout=2s \
  6. --health-retries=5 \
  7. -v vttestserver_data:/vt/vtdataroot/vitess \
  8. vitess/vttestserver:mysql80
  9. /vt/bin/vttestserver \
  10. --alsologtostderr \
  11. --data_dir=/vt/vtdataroot/vitess \
  12. --persistent_mode \
  13. --port=33574 \
  14. --mysql_bind_host=0.0.0.0 \
  15. --keyspaces=test,unsharded \
  16. --num_shards=2,1

Example

An example command to run the docker image is as follows :

  1. docker run --name=vttestserver \
  2. -p 33577:33577 \
  3. -e PORT=33574 \
  4. -e KEYSPACES=test,unsharded \
  5. -e NUM_SHARDS=2,1 \
  6. -e MYSQL_MAX_CONNECTIONS=70000 \
  7. -e MYSQL_BIND_HOST=0.0.0.0 \
  8. --health-cmd="mysqladmin ping -h127.0.0.1 -P33577" \
  9. --health-interval=5s \
  10. --health-timeout=2s \
  11. --health-retries=5 \
  12. vitess/vttestserver:mysql80

Now, we can connect to the vtgate from a MySQL client as follows :

  1. mysql --host 127.0.0.1 --port 33577 --user "root"

We have 2 keyspaces which we can use, test which has 2 shards and unsharded which has a single shard.