You picked CockroachDB because the application needs the surface area of Postgres SQL and the fault tolerance of a Raft-replicated key-value store underneath. You read the docs, ran cockroach start-single-node on a laptop, joined three nodes on a staging VLAN, and it worked. Then you handed the cluster to the platform team and the first question they asked was, “where are the TLS certs, what’s listening on the public interface, and how does this back up.” That’s the question that turns a working prototype into a production deployment — and it’s the question this guide answers end-to-end on Ubuntu 22.04.
What’s happening
A CockroachDB cluster on three VMs is not “three databases that replicate.” It is one logical cluster, where every node is identical and any node can serve any range. The cluster talks to itself over a mutually-authenticated TLS mesh (every node carries its own node cert signed by a cluster CA, and every client carries a separately-signed client cert), and the safest production posture is to bind the SQL and inter-node listeners to private IPs only and treat the admin UI as an internal service behind a bastion or VPN. There is no leader for the cluster as a whole — each range elects its own Raft leader — so failover is not a thing that “happens”; the cluster is always doing it, all the time, for every range. What you need to get right at install time is the cert chain (because regenerating it on a running cluster is painful), the listener binding (because exposing 26257 to the internet has burned operators before), the snapshot policy on the underlying disks (because consistent volume snapshots are how you get your point-in-time-restore in less than an hour), and the systemd unit that restarts it cleanly after kernel patching.
This installer wires all of that on each of three Ubuntu 22.04 VMs. The cert directory is rebuilt from a shared CA pair, the listener binds to the private IP only, the data directory lives on a dedicated mount, the systemd unit handles LimitNOFILE and a graceful shutdown timeout, and the post-install hook runs cockroach init exactly once from node-1.
How to verify
Node status from any node:
cockroach node status \
--certs-dir=/var/lib/cockroach/certs \
--host=10.20.0.11:26257
# id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live
# 1 | 10.20.0.11:26257 | ... | v24.x | ... | ... | | true | true
# 2 | 10.20.0.12:26257 | ... | v24.x | ... | ... | | true | true
# 3 | 10.20.0.13:26257 | ... | v24.x | ... | ... | | true | true
Range replication and lease distribution:
SELECT range_id, replicas, lease_holder
FROM crdb_internal.ranges
ORDER BY range_id
LIMIT 20;
The SQL listener should never appear on a public interface:
ss -lntp | grep -E ':26257|:8080'
# tcp LISTEN 0 4096 10.20.0.11:26257 0.0.0.0:* users:(("cockroach",pid=...))
# tcp LISTEN 0 4096 10.20.0.11:8080 0.0.0.0:* users:(("cockroach",pid=...))
If you see 0.0.0.0:26257 or :::26257, the listener is wrong — stop the service before traffic finds it.
Cluster CA fingerprint should match on every node:
openssl x509 -in /var/lib/cockroach/certs/ca.crt -noout -fingerprint -sha256
The fix
Save the script below as /root/setup-cockroach-node.sh on each VM. The first node is the CA host — it generates the CA key/cert and all three node certs, then ships node-2 and node-3 their cert bundles. Edit the variables at the top, run it on node-1 first (it will pause to print the rsync commands for shipping certs to the other two nodes), then run it on node-2 and node-3, and finally run cockroach init from node-1.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 CockroachDB 24.x 3-node cluster installer with TLS
### Run on each of the 3 nodes after editing the variables below
### Node-1 generates the CA + all node certs; ships them via rsync to node-2/3
### Snapshots are taken at the volume level (LVM or cloud-provider snapshot)
### cockroach backup is the logical layer on top
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER NODE ===
NODE_NAME="crdb01" # crdb01 / crdb02 / crdb03
NODE_IP="10.20.0.11" # this node's private IP
PEER_IPS=("10.20.0.11" "10.20.0.12" "10.20.0.13")
PEER_NAMES=("crdb01" "crdb02" "crdb03")
CLUSTER_NAME="cockroachdb-prod"
IS_CA_HOST="yes" # "yes" only on node-1, "no" on node-2/3
COCKROACH_VERSION="24.3.4"
DATA_DEVICE="/dev/sdb" # dedicated data disk; set to "" to skip mount
DATA_MOUNT="/var/lib/cockroach"
# =====================
echo
echo "========================================"
echo "CockroachDB node ${NODE_NAME} (${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 ntp chrony \
fail2ban unattended-upgrades net-tools iproute2 jq lsof tcpdump \
rsync openssl xfsprogs lvm2
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 + automatic 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 "==> Kernel/sysctl tuning for CockroachDB"
cat > /etc/sysctl.d/99-cockroach.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-cockroach.conf <<'LIMITS'
cockroach soft nofile 200000
cockroach hard nofile 200000
cockroach soft nproc 65535
cockroach hard nproc 65535
LIMITS
echo "==> Creating cockroach system user"
id cockroach >/dev/null 2>&1 || useradd --system --home /var/lib/cockroach --shell /usr/sbin/nologin cockroach
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 cockroach "${DATA_DEVICE}"
fi
if ! grep -q "${DATA_MOUNT}" /etc/fstab; then
echo "LABEL=cockroach ${DATA_MOUNT} xfs defaults,noatime,nofail 0 0" >> /etc/fstab
fi
mount -a
fi
chown -R cockroach:cockroach "${DATA_MOUNT}"
echo "==> Installing CockroachDB ${COCKROACH_VERSION}"
cd /tmp
wget -q "https://binaries.cockroachdb.com/cockroach-v${COCKROACH_VERSION}.linux-amd64.tgz"
tar -xzf "cockroach-v${COCKROACH_VERSION}.linux-amd64.tgz"
install -m 0755 -o root -g root "cockroach-v${COCKROACH_VERSION}.linux-amd64/cockroach" /usr/local/bin/cockroach
mkdir -p /usr/local/lib/cockroach
cp -r "cockroach-v${COCKROACH_VERSION}.linux-amd64/lib/." /usr/local/lib/cockroach/
rm -rf "cockroach-v${COCKROACH_VERSION}.linux-amd64" "cockroach-v${COCKROACH_VERSION}.linux-amd64.tgz"
echo "==> Cert directory layout"
CERTS_DIR="${DATA_MOUNT}/certs"
CA_DIR="/root/cockroach-ca"
mkdir -p "${CERTS_DIR}"
chown cockroach:cockroach "${CERTS_DIR}"
chmod 700 "${CERTS_DIR}"
if [[ "${IS_CA_HOST}" == "yes" ]]; then
mkdir -p "${CA_DIR}"
chmod 700 "${CA_DIR}"
if [[ ! -f "${CA_DIR}/ca.key" ]]; then
echo "==> Generating cluster CA"
cockroach cert create-ca \
--certs-dir="${CERTS_DIR}" \
--ca-key="${CA_DIR}/ca.key"
fi
echo "==> Generating node cert for ${NODE_NAME} (${NODE_IP})"
cockroach cert create-node \
"${NODE_IP}" \
"${PEER_IPS[0]}" "${PEER_IPS[1]}" "${PEER_IPS[2]}" \
"${PEER_NAMES[0]}" "${PEER_NAMES[1]}" "${PEER_NAMES[2]}" \
127.0.0.1 localhost \
--certs-dir="${CERTS_DIR}" \
--ca-key="${CA_DIR}/ca.key" \
--overwrite
echo "==> Generating client root cert"
cockroach cert create-client root \
--certs-dir="${CERTS_DIR}" \
--ca-key="${CA_DIR}/ca.key" \
--overwrite
echo
echo "==> NODE-2 and NODE-3 cert bundles"
for i in 1 2; do
PEER="${PEER_NAMES[$i]}"
PEER_IP="${PEER_IPS[$i]}"
PEER_OUT="/tmp/${PEER}-certs"
rm -rf "${PEER_OUT}"
mkdir -p "${PEER_OUT}"
cp "${CERTS_DIR}/ca.crt" "${PEER_OUT}/"
cp "${CERTS_DIR}/client.root.crt" "${PEER_OUT}/"
cp "${CERTS_DIR}/client.root.key" "${PEER_OUT}/"
cockroach cert create-node \
"${PEER_IP}" \
"${PEER_IPS[0]}" "${PEER_IPS[1]}" "${PEER_IPS[2]}" \
"${PEER_NAMES[0]}" "${PEER_NAMES[1]}" "${PEER_NAMES[2]}" \
127.0.0.1 localhost \
--certs-dir="${PEER_OUT}" \
--ca-key="${CA_DIR}/ca.key" \
--overwrite
echo "Bundle ready: ${PEER_OUT}"
echo " rsync -aP ${PEER_OUT}/ root@${PEER_IP}:${CERTS_DIR}/"
done
echo
fi
echo "==> Installing CockroachDB systemd unit"
cat > /etc/systemd/system/cockroach.service <<EOF
[Unit]
Description=CockroachDB node ${NODE_NAME}
Requires=network.target
After=network.target
[Service]
Type=notify
User=cockroach
Group=cockroach
WorkingDirectory=${DATA_MOUNT}
ExecStart=/usr/local/bin/cockroach start \\
--certs-dir=${CERTS_DIR} \\
--store=${DATA_MOUNT}/data \\
--listen-addr=${NODE_IP}:26257 \\
--advertise-addr=${NODE_IP}:26257 \\
--http-addr=${NODE_IP}:8080 \\
--join=${PEER_IPS[0]}:26257,${PEER_IPS[1]}:26257,${PEER_IPS[2]}:26257 \\
--cache=.25 \\
--max-sql-memory=.25 \\
--background=false \\
--locality=region=local,zone=${NODE_NAME}
ExecStop=/usr/local/bin/cockroach quit --certs-dir=${CERTS_DIR} --host=${NODE_IP}:26257
Restart=on-failure
RestartSec=10
LimitNOFILE=200000
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
EOF
mkdir -p "${DATA_MOUNT}/data"
chown -R cockroach:cockroach "${DATA_MOUNT}"
if [[ "${IS_CA_HOST}" == "yes" ]]; then
echo
echo "==> On node-1: cert bundles for node-2 and node-3 are in /tmp/."
echo "==> Ship them to the other nodes BEFORE you systemctl start cockroach on them:"
for i in 1 2; do
echo " rsync -aP /tmp/${PEER_NAMES[$i]}-certs/ root@${PEER_IPS[$i]}:${CERTS_DIR}/"
done
echo
read -p "Press enter once cert bundles are on node-2 and node-3 (or skip on node-1)... " _ || true
fi
if [[ "${IS_CA_HOST}" != "yes" ]]; then
echo "==> On node-2/3: cert bundle must already be in ${CERTS_DIR}"
if [[ ! -f "${CERTS_DIR}/node.crt" ]]; then
echo "ERROR: ${CERTS_DIR}/node.crt missing — ship it from node-1 first."
exit 1
fi
chown -R cockroach:cockroach "${CERTS_DIR}"
chmod 700 "${CERTS_DIR}"
chmod 600 "${CERTS_DIR}/"*.key
fi
systemctl daemon-reload
systemctl enable cockroach
systemctl start cockroach
echo "==> Waiting for CockroachDB listener"
for i in {1..60}; do
if ss -lnt | awk '{print $4}' | grep -q "${NODE_IP}:26257"; then break; fi
sleep 2
done
echo
echo "========================================"
echo "DONE on ${NODE_NAME}"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(26257|8080)\b' || true
echo
if [[ "${IS_CA_HOST}" == "yes" ]]; then
echo "Once all 3 nodes are running, INIT the cluster from this node:"
echo " cockroach init --certs-dir=${CERTS_DIR} --host=${NODE_IP}:26257"
echo
echo "Then check status:"
echo " cockroach node status --certs-dir=${CERTS_DIR} --host=${NODE_IP}:26257"
fi
echo
echo "Admin UI: https://${NODE_IP}:8080/ (tunnel via bastion or VPN)"
Make it executable and run on node-1 first:
chmod 700 /root/setup-cockroach-node.sh
/root/setup-cockroach-node.sh
Ship the cert bundles printed by node-1 to the other two nodes, then run the same script on node-2 and node-3 with IS_CA_HOST="no". When all three listeners are up:
cockroach init \
--certs-dir=/var/lib/cockroach/certs \
--host=10.20.0.11:26257
Create your app database and a SQL user:
CREATE DATABASE app;
CREATE USER app WITH PASSWORD 'CHANGE_ME';
GRANT ALL ON DATABASE app TO app;
For backups, schedule volume-level snapshots of /var/lib/cockroach every six hours and pair them with a BACKUP to object storage. The volume snapshot is what gives you a fast point-in-time restore of a single node; the cluster-level BACKUP is what lets you rebuild from scratch on a new fleet. Pick a retention that survives a two-week silent corruption window, not just the last twenty-four hours. Do a restore drill before the cluster sees production traffic, document the elapsed time, and put a calendar reminder every quarter to redo it.
Run CockroachDB on Stack Harbor’s managed dedicated cloud with disk-level snapshots and patching included under environment management, or start a scoping call if you’d like us to plan the rollout end-to-end.