Skip to content

Deploying Vitess (sharded MySQL) on Ubuntu 22.04 — vtgate + vttablet topology

Stand up a Vitess cluster on Ubuntu 22.04 — topology service (etcd), vttablet wrapping MySQL on each shard, vtgate as the smart client-facing proxy.

You spent two years sharding MySQL by hand. Customer routing logic lives in three different microservices, half of them with their own version of “find the shard for this user_id,” and every schema migration is an all-hands operation that takes a Saturday. You evaluated Vitess because it does exactly the thing you’ve been doing by hand — except it does it as a proxy layer your application talks to like one big MySQL, and the sharding logic lives in one place. Now you have to deploy it, and the docs assume you already know what a vtgate, a vttablet, a keyspace, and a vschema are. This guide builds a real cluster on Ubuntu 22.04 and explains each piece as it goes in.

What’s happening

A Vitess deployment has five distinct moving parts, all on Ubuntu 22.04 VMs. The topology service is etcd — it’s the source of truth for which shards exist, who’s their primary, and how queries should be routed. mysqld is the actual storage engine — Vitess does not replace MySQL, it wraps it; each shard primary is a regular MySQL 8.0 server with its own replicas. vttablet sits next to each mysqld instance, one per MySQL — it owns that backend, executes queries against it, manages replication, and handles the operational primitives (PlannedReparentShard, EmergencyReparentShard, schema migrations) that you used to do by hand. vtgate is the front door — your application connects to vtgate on the MySQL wire protocol; vtgate parses queries, consults the topology, routes to the right shard’s vttablet, and returns results. vtctld is the admin UI/CLI plane — that’s where you create keyspaces, define shards, apply VSchemas, and trigger resharding.

The minimum production topology for a two-shard keyspace with HA is: 3 etcd nodes (topology quorum), 6 MySQL VMs (2 shards × 3 replicas each), 6 vttablets co-located with the MySQLs, 2 vtgate VMs behind your application’s load balancer, and 1 vtctld VM (or co-located on a vtgate). That’s the realistic floor. This installer covers the host prep for each role and the bring-up sequence from a control host.

How to verify

Topology summary from the control host (with vtctldclient pointing at vtctld):

vtctldclient --server=10.50.0.51:15999 GetKeyspaces
# commerce
vtctldclient --server=10.50.0.51:15999 GetShard commerce/0
vtctldclient --server=10.50.0.51:15999 GetTablets --keyspace commerce
# Cell  Keyspace  Shard  TabletType  State  Alias            Hostname
# zone1 commerce  0      PRIMARY     SERVING zone1-0000000100 10.50.0.21
# zone1 commerce  0      REPLICA     SERVING zone1-0000000101 10.50.0.22
# zone1 commerce  0      REPLICA     SERVING zone1-0000000102 10.50.0.23

Each vttablet’s local health (from the host running it):

curl -s http://10.50.0.21:15100/debug/health
# OK
curl -s http://10.50.0.21:15100/debug/vars | jq '.TabletState'

End-to-end query via vtgate using the MySQL wire protocol:

mysql -h 10.50.0.41 -P 15306 -u vt_app -p \
  -e "USE commerce; SHOW TABLES;"

MySQL replication on each shard primary:

SHOW REPLICA STATUS\G
-- Look for: Replica_IO_Running: Yes / Replica_SQL_Running: Yes / Seconds_Behind_Source: 0

The fix

You run host prep on every VM. Then you bootstrap etcd, then bring up MySQL + vttablet on each shard, then start vtgate, then create the keyspace via vtctldclient. The script below covers everything that’s the same across roles; the role-specific bring-up commands go in the verification steps after.

Save as /root/setup-vitess-host.sh on each VM:

#!/usr/bin/env bash
set -euo pipefail

### Ubuntu 22.04 Vitess host preparation
### Roles: etcd / mysql-vttablet / vtgate / vtctld
### Does NOT yet start the role-specific services; that follows in the bring-up steps

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# === EDIT PER NODE ===
NODE_ROLE="mysql-vttablet"             # etcd / mysql-vttablet / vtgate / vtctld
NODE_IP="10.50.0.21"
ETCD_PEERS=("10.50.0.11" "10.50.0.12" "10.50.0.13")
ETCD_NAMES=("etcd01" "etcd02" "etcd03")
NODE_NAME="vt-shard0-r0"
VITESS_VERSION="19.0.6"
MYSQL_VERSION="8.0"
DATA_DEVICE="/dev/sdb"
DATA_MOUNT="/var/lib/vitess"
# =====================

echo
echo "========================================"
echo "Vitess host prep: ${NODE_ROLE} (${NODE_IP})"
echo "========================================"
echo

echo "==> Updating base OS"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade
apt-get -y install curl wget gnupg ca-certificates chrony fail2ban \
  unattended-upgrades net-tools iproute2 jq lsof tcpdump xfsprogs \
  lvm2 numactl psmisc tar gzip

systemctl enable --now chrony

echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart ssh

echo "==> fail2ban + auto security updates"
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban
dpkg-reconfigure -f noninteractive unattended-upgrades

echo "==> sysctl tuning"
cat > /etc/sysctl.d/99-vitess.conf <<'SYSCTL'
vm.swappiness = 1
vm.max_map_count = 262144
fs.file-max = 2097152
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
kernel.dmesg_restrict = 1
SYSCTL
sysctl --system

cat > /etc/security/limits.d/99-vitess.conf <<'LIMITS'
mysql   soft nofile 200000
mysql   hard nofile 200000
vitess  soft nofile 200000
vitess  hard nofile 200000
mysql   soft nproc 65535
mysql   hard nproc 65535
vitess  soft nproc 65535
vitess  hard nproc 65535
LIMITS

echo "==> Creating vitess user"
id vitess >/dev/null 2>&1 || useradd --system --home /var/lib/vitess --shell /bin/bash --create-home vitess

echo "==> Preparing data mount"
mkdir -p "${DATA_MOUNT}"
if [[ -n "${DATA_DEVICE}" && -b "${DATA_DEVICE}" ]]; then
  if ! blkid "${DATA_DEVICE}" >/dev/null 2>&1; then
    echo "Formatting ${DATA_DEVICE} as xfs"
    mkfs.xfs -f -L vitess "${DATA_DEVICE}"
  fi
  if ! grep -q "${DATA_MOUNT}" /etc/fstab; then
    echo "LABEL=vitess ${DATA_MOUNT} xfs defaults,noatime,nofail 0 0" >> /etc/fstab
  fi
  mount -a
fi
chown -R vitess:vitess "${DATA_MOUNT}"

case "${NODE_ROLE}" in
  etcd)
    echo "==> Installing etcd"
    apt-get -y install etcd-server etcd-client || {
      ETCD_VERSION="v3.5.13"
      cd /tmp
      wget -q "https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz"
      tar -xzf "etcd-${ETCD_VERSION}-linux-amd64.tar.gz"
      install -m0755 "etcd-${ETCD_VERSION}-linux-amd64/etcd" /usr/local/bin/etcd
      install -m0755 "etcd-${ETCD_VERSION}-linux-amd64/etcdctl" /usr/local/bin/etcdctl
    }
    mkdir -p /var/lib/etcd /etc/etcd
    chown -R vitess:vitess /var/lib/etcd

    ETCD_INITIAL_CLUSTER="${ETCD_NAMES[0]}=http://${ETCD_PEERS[0]}:2380,${ETCD_NAMES[1]}=http://${ETCD_PEERS[1]}:2380,${ETCD_NAMES[2]}=http://${ETCD_PEERS[2]}:2380"
    cat > /etc/etcd/etcd.conf <<EOF
ETCD_NAME=${NODE_NAME}
ETCD_DATA_DIR=/var/lib/etcd
ETCD_LISTEN_PEER_URLS=http://${NODE_IP}:2380
ETCD_LISTEN_CLIENT_URLS=http://${NODE_IP}:2379,http://127.0.0.1:2379
ETCD_INITIAL_ADVERTISE_PEER_URLS=http://${NODE_IP}:2380
ETCD_ADVERTISE_CLIENT_URLS=http://${NODE_IP}:2379
ETCD_INITIAL_CLUSTER=${ETCD_INITIAL_CLUSTER}
ETCD_INITIAL_CLUSTER_TOKEN=vitess-topo
ETCD_INITIAL_CLUSTER_STATE=new
EOF

    cat > /etc/systemd/system/etcd.service <<'ETCDUNIT'
[Unit]
Description=etcd
After=network.target

[Service]
Type=notify
User=vitess
Group=vitess
EnvironmentFile=/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
LimitNOFILE=200000

[Install]
WantedBy=multi-user.target
ETCDUNIT
    systemctl daemon-reload
    systemctl enable --now etcd
    ;;

  mysql-vttablet)
    echo "==> Installing MySQL ${MYSQL_VERSION}"
    apt-get -y install mysql-server mysql-client

    cat > /etc/mysql/mysql.conf.d/vitess.cnf <<EOF
[mysqld]
server_id = ${NODE_IP##*.}
report_host = ${NODE_IP}
bind-address = ${NODE_IP}
datadir = ${DATA_MOUNT}/mysql/data
log_bin = ${DATA_MOUNT}/mysql/binlog/mysql-bin
log_bin_index = ${DATA_MOUNT}/mysql/binlog/mysql-bin.index
relay_log = ${DATA_MOUNT}/mysql/relay/relay-bin
relay_log_index = ${DATA_MOUNT}/mysql/relay/relay-bin.index
binlog_format = ROW
binlog_row_image = FULL
binlog_expire_logs_seconds = 86400
gtid_mode = ON
enforce_gtid_consistency = ON
log_replica_updates = ON
sync_binlog = 1
innodb_flush_log_at_trx_commit = 1
innodb_buffer_pool_size = 4G
max_connections = 1000
EOF

    mkdir -p "${DATA_MOUNT}"/mysql/{data,binlog,relay,tmp}
    chown -R mysql:mysql "${DATA_MOUNT}/mysql"

    sed -i 's|^datadir.*|datadir = '"${DATA_MOUNT}"'/mysql/data|' /etc/mysql/mysql.conf.d/mysqld.cnf || true
    systemctl restart mysql

    echo "==> Installing Vitess ${VITESS_VERSION}"
    cd /tmp
    wget -q "https://github.com/vitessio/vitess/releases/download/v${VITESS_VERSION}/vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    tar -xzf "vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    install -m0755 "vitess-${VITESS_VERSION}-linux-amd64/bin/"* /usr/local/bin/

    mkdir -p /etc/vitess "${DATA_MOUNT}/vttablet"
    chown -R vitess:vitess "${DATA_MOUNT}/vttablet"
    ;;

  vtgate)
    echo "==> Installing Vitess ${VITESS_VERSION} (vtgate only)"
    cd /tmp
    wget -q "https://github.com/vitessio/vitess/releases/download/v${VITESS_VERSION}/vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    tar -xzf "vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    install -m0755 "vitess-${VITESS_VERSION}-linux-amd64/bin/vtgate" /usr/local/bin/

    mkdir -p /etc/vitess
    cat > /etc/systemd/system/vtgate.service <<EOF
[Unit]
Description=Vitess vtgate
After=network.target

[Service]
User=vitess
Group=vitess
ExecStart=/usr/local/bin/vtgate \\
  --topo_implementation etcd2 \\
  --topo_global_server_address ${ETCD_PEERS[0]}:2379,${ETCD_PEERS[1]}:2379,${ETCD_PEERS[2]}:2379 \\
  --topo_global_root /vitess/global \\
  --cell zone1 \\
  --cells_to_watch zone1 \\
  --port 15001 \\
  --grpc_port 15991 \\
  --mysql_server_port 15306 \\
  --mysql_server_bind_address ${NODE_IP} \\
  --mysql_auth_server_impl none \\
  --tablet_types_to_wait PRIMARY,REPLICA \\
  --service_map 'grpc-vtgateservice'
Restart=on-failure
LimitNOFILE=200000

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable vtgate
    ;;

  vtctld)
    echo "==> Installing Vitess ${VITESS_VERSION} (vtctld)"
    cd /tmp
    wget -q "https://github.com/vitessio/vitess/releases/download/v${VITESS_VERSION}/vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    tar -xzf "vitess-${VITESS_VERSION}-linux-amd64.tar.gz"
    install -m0755 "vitess-${VITESS_VERSION}-linux-amd64/bin/vtctld" /usr/local/bin/
    install -m0755 "vitess-${VITESS_VERSION}-linux-amd64/bin/vtctldclient" /usr/local/bin/

    cat > /etc/systemd/system/vtctld.service <<EOF
[Unit]
Description=Vitess vtctld
After=network.target

[Service]
User=vitess
Group=vitess
ExecStart=/usr/local/bin/vtctld \\
  --topo_implementation etcd2 \\
  --topo_global_server_address ${ETCD_PEERS[0]}:2379,${ETCD_PEERS[1]}:2379,${ETCD_PEERS[2]}:2379 \\
  --topo_global_root /vitess/global \\
  --cell zone1 \\
  --port 15000 \\
  --grpc_port 15999 \\
  --service_map 'grpc-vtctl,grpc-vtctld'
Restart=on-failure
LimitNOFILE=200000

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable vtctld

    # vtctld first needs the cell registered in topo
    sleep 5
    vtctldclient \
      --server localhost:15999 \
      AddCellInfo --server-address "${ETCD_PEERS[0]}:2379,${ETCD_PEERS[1]}:2379,${ETCD_PEERS[2]}:2379" \
      --root /vitess/zone1 zone1 || true

    systemctl start vtctld
    ;;
esac

echo
echo "==> Host prep complete for role ${NODE_ROLE} on ${NODE_IP}"
ss -lntp | grep -E ':(2379|2380|3306|15000|15001|15100|15306|15991|15999)\b' || true

After every node has run the script, bring up vttablet on each mysql-vttablet VM:

sudo -u vitess /usr/local/bin/vttablet \
  --topo_implementation etcd2 \
  --topo_global_server_address 10.50.0.11:2379,10.50.0.12:2379,10.50.0.13:2379 \
  --topo_global_root /vitess/global \
  --tablet-path zone1-0000000100 \
  --init_keyspace commerce \
  --init_shard 0 \
  --init_tablet_type replica \
  --port 15100 \
  --grpc_port 16100 \
  --service_map 'grpc-queryservice,grpc-tabletmanager,grpc-updatestream' \
  --db_socket /var/run/mysqld/mysqld.sock \
  --db_app_user vt_app \
  --db_app_password CHANGE_ME \
  --health_check_interval 5s \
  --tablet_hostname 10.50.0.21 \
  --logtostderr &

Wrap that in a systemd unit per shard/replica (vary tablet-path, init_shard, tablet_hostname). Then from the control host (or any host with vtctldclient reachable):

vtctldclient --server 10.50.0.51:15999 CreateKeyspace commerce
vtctldclient --server 10.50.0.51:15999 PlannedReparentShard \
  --keyspace=commerce --shard=0 \
  --new-primary=zone1-0000000100

Start vtgate on each vtgate VM:

systemctl start vtgate

Connect through vtgate from your application or mysql -h 10.50.0.41 -P 15306 -u vt_app. For resharding, create the destination shards, run MoveTables or Reshard from vtctldclient, watch the workflow with Workflow show, switch reads, switch writes, drop the source. That sequence — never edit shards by hand — is the whole point of running Vitess.

For backups, point Vitess’s vtbackup at object storage and schedule a full backup from each shard’s replica nightly; pair with volume snapshots of ${DATA_MOUNT}/mysql on every MySQL host every six hours. The vtbackup archive lets you reseed a new replica without dumping the primary; the snapshot is your fast recovery. Restore drills quarterly, recorded.

Run Vitess on Stack Harbor’s managed dedicated cloud with environment management handling the operational primitives, or start a scoping call if you’d like us to plan the migration off your hand-sharded fleet.