You install ClickHouse on a single AlmaLinux node, point an ETL at it, dashboards load fast, the analytics team is happy. Six months later the SSD wears out, the node won’t boot, and the only copy of three years of event data is on that one disk. Someone asks where the replica was. There was no replica because there was no coordinator — ZooKeeper had been deprecated, ClickHouse Keeper was “on the roadmap”, nobody ever got around to wiring it. Then you discover that users.xml still has the default user, that the native protocol is on 0.0.0.0:9000 with no TLS, and that anyone inside the VPC can run DROP DATABASE. ClickHouse for production analytics isn’t “fast SQL on one box”; it’s a replicated table on at least two replicas, coordinated by Keeper, with TLS and a real ACL.
What’s happening
A production-grade ClickHouse deployment on your own infrastructure is at least one shard with two replicas — that’s the minimum that survives a single-node outage without data loss. Coordination uses ClickHouse Keeper (Raft, built in, ZooKeeper-compatible API) rather than ZooKeeper itself; on a small estate, Keeper runs co-located with the ClickHouse server on a 3-node Keeper quorum spread across both replicas plus a third tiny VM that acts as the tiebreaker. Tables that need replication use the ReplicatedMergeTree engine with a path in Keeper for the replica registry; non-replicated tables exist (lookup tables, staging) but the data that matters has Replicated* in its DDL. users.xml replaces the default user with a real one, password-hashed (SHA-256), constrained to specific networks. The native port (9440) and HTTPS port (8443) are TLS-only; 9000 and 8123 are either disabled or bound to localhost for backup tooling. LimitNOFILE=500000 (ClickHouse opens many merges), vm.overcommit_memory=1, and MAX_THREADS tuned to the box.
How to verify
Replica registration and lag from any node:
SELECT database, table, replica_name, is_leader, total_replicas, active_replicas,
absolute_delay, queue_size
FROM system.replicas
ORDER BY queue_size DESC;
active_replicas must equal total_replicas; queue_size > 0 for more than a few seconds is a replica that’s falling behind merges or stuck on a Keeper RPC. absolute_delay is what dashboards read against — a single replica that drifts above 30 seconds for an analytics workload is suspect.
Keeper quorum:
echo "ruok" | nc -q 1 127.0.0.1 9181
# imok
echo "stat" | nc -q 1 127.0.0.1 9181
# ClickHouse Keeper version: 24.x
# Mode: leader
# Followers: 2
If Followers is less than 2 on a 3-node quorum, the cluster is one failure away from losing coordination.
A real DDL round-trip across replicas:
CREATE TABLE events_test ON CLUSTER analytics (
ts DateTime, user_id UInt64, kind String
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/events_test', '{replica}')
ORDER BY (user_id, ts);
INSERT INTO events_test VALUES (now(), 42, 'click');
-- on the other replica, after a second
SELECT count() FROM events_test;
Should return 1 within a second. If it returns 0, the Keeper path is wrong or the replica’s interserver_http_host is unreachable.
The fix
Save the script as /root/setup-clickhouse-node.sh and run it on each of the two replicas after editing the per-node variables, plus on the tiebreaker VM. It performs the full installer: base hardening (SELinux enforcing, fail2ban, dnf-automatic, SSH key-only), sysctl tuning (vm.swappiness=1, vm.overcommit_memory=1, fs.file-max, net.core.somaxconn), the official ClickHouse RPM repo, an embedded ClickHouse Keeper on port 9181 in a 3-node quorum, a hardened config.xml with TLS-only listeners on 9440 (native) + 8443 (HTTP) and the legacy plaintext ports either disabled or localhost-only, a users.xml with the default user removed, an analytics user with SHA-256 hash and network ACL, an interserver_http_credentials block so replication itself is authenticated, LimitNOFILE=500000 via systemd override, and a listener-wait loop.
#!/usr/bin/env bash
set -euo pipefail
### AlmaLinux 9 ClickHouse + Keeper installer
### Run on each replica + on the Keeper tiebreaker node
### No local backups; assumes VM-level snapshots
### `clickhouse-backup` to object storage is the additional layer
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER NODE ===
NODE_NAME="ch01" # ch01 / ch02 / chk03 (tiebreaker)
NODE_IP="10.50.0.11" # this node's private IP
SHARD_NAME="shard1"
REPLICA_NAME="${NODE_NAME}"
KEEPER_IPS=("10.50.0.11" "10.50.0.12" "10.50.0.13")
KEEPER_NAMES=("ch01" "ch02" "chk03")
KEEPER_IDS=(1 2 3)
REPLICA_IPS=("10.50.0.11" "10.50.0.12")
ROLE="server" # "server" on ch01/ch02, "keeper-only" on chk03
ANALYTICS_USER="analytics"
ANALYTICS_PASSWORD_SHA256="" # printf 'CHANGE_ME' | sha256sum | awk '{print $1}'
INTERSERVER_USER="ch-interserver"
INTERSERVER_PASSWORD="CHANGE_ME_INTERSERVER"
UI_DOMAIN="ch.example.com"
# =====================
if [[ -z "${ANALYTICS_PASSWORD_SHA256}" ]]; then
echo "Set ANALYTICS_PASSWORD_SHA256 first:"
echo " printf 'YOUR_PASSWORD' | sha256sum | awk '{print \$1}'"
exit 1
fi
echo
echo "========================================"
echo "ClickHouse ${NODE_NAME} (${NODE_IP}) role=${ROLE}"
echo "========================================"
echo
echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y install vim curl wget chrony fail2ban dnf-automatic \
policycoreutils-python-utils lsof tcpdump bind-utils tar gzip jq \
iproute net-tools nc openssl
systemctl enable --now chronyd
echo "==> SELinux enforcing"
sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
setenforce 1 || true
echo "==> Kernel/sysctl tuning"
cat > /etc/sysctl.d/99-clickhouse.conf <<'SYSCTL'
vm.swappiness = 1
vm.overcommit_memory = 1
vm.max_map_count = 524288
fs.file-max = 4194304
fs.aio-max-nr = 1048576
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
SYSCTL
sysctl --system
cat > /etc/security/limits.d/99-clickhouse.conf <<'LIMITS'
clickhouse soft nofile 500000
clickhouse hard nofile 500000
clickhouse soft nproc 65535
clickhouse hard nproc 65535
LIMITS
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 sshd
echo "==> fail2ban + automatic security updates"
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban
cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
download_updates = yes
apply_updates = yes
[emitters]
emit_via = stdio
DNFAUTO
systemctl enable --now dnf-automatic.timer
echo "==> Adding ClickHouse RPM repo"
dnf -y install yum-utils
yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
if [[ "${ROLE}" == "server" ]]; then
dnf -y install clickhouse-server clickhouse-client
else
dnf -y install clickhouse-keeper
fi
echo "==> Generating self-signed TLS"
TLS_DIR="/etc/clickhouse-server/tls"
[[ "${ROLE}" != "server" ]] && TLS_DIR="/etc/clickhouse-keeper/tls"
mkdir -p "${TLS_DIR}"
cd "${TLS_DIR}"
if [[ ! -f node.crt ]]; then
openssl req -x509 -newkey rsa:4096 -nodes -days 1825 \
-subj "/CN=${UI_DOMAIN}" \
-addext "subjectAltName=DNS:${UI_DOMAIN},IP:${NODE_IP},IP:127.0.0.1" \
-keyout node.key -out node.crt
openssl dhparam -out dhparam.pem 2048
chmod 600 node.key
fi
cd /
if [[ "${ROLE}" == "server" ]]; then
chown -R clickhouse:clickhouse "${TLS_DIR}"
echo "==> Writing config.xml (server + embedded keeper)"
cat > /etc/clickhouse-server/config.d/01-listen.xml <<EOF
<clickhouse>
<listen_host>${NODE_IP}</listen_host>
<listen_host>127.0.0.1</listen_host>
<tcp_port remove="remove"/>
<http_port remove="remove"/>
<tcp_port_secure>9440</tcp_port_secure>
<https_port>8443</https_port>
<interserver_http_host>${NODE_IP}</interserver_http_host>
<interserver_https_port>9010</interserver_https_port>
<openSSL>
<server>
<certificateFile>${TLS_DIR}/node.crt</certificateFile>
<privateKeyFile>${TLS_DIR}/node.key</privateKeyFile>
<dhParamsFile>${TLS_DIR}/dhparam.pem</dhParamsFile>
<verificationMode>none</verificationMode>
<loadDefaultCAFile>true</loadDefaultCAFile>
<cacheSessions>true</cacheSessions>
<disableProtocols>sslv2,sslv3,tlsv1,tlsv1_1</disableProtocols>
<preferServerCiphers>true</preferServerCiphers>
</server>
</openSSL>
<interserver_http_credentials>
<user>${INTERSERVER_USER}</user>
<password>${INTERSERVER_PASSWORD}</password>
</interserver_http_credentials>
<max_open_files>500000</max_open_files>
</clickhouse>
EOF
cat > /etc/clickhouse-server/config.d/02-cluster.xml <<EOF
<clickhouse>
<remote_servers>
<analytics>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>${REPLICA_IPS[0]}</host>
<port>9440</port>
<secure>1</secure>
</replica>
<replica>
<host>${REPLICA_IPS[1]}</host>
<port>9440</port>
<secure>1</secure>
</replica>
</shard>
</analytics>
</remote_servers>
<macros>
<shard>${SHARD_NAME}</shard>
<replica>${REPLICA_NAME}</replica>
</macros>
<zookeeper>
<node><host>${KEEPER_IPS[0]}</host><port>9181</port><secure>0</secure></node>
<node><host>${KEEPER_IPS[1]}</host><port>9181</port><secure>0</secure></node>
<node><host>${KEEPER_IPS[2]}</host><port>9181</port><secure>0</secure></node>
</zookeeper>
<distributed_ddl>
<path>/clickhouse/task_queue/ddl</path>
</distributed_ddl>
</clickhouse>
EOF
echo "==> Embedded Keeper config (server nodes only)"
cat > /etc/clickhouse-server/config.d/03-keeper.xml <<EOF
<clickhouse>
<keeper_server>
<tcp_port>9181</tcp_port>
<server_id>$(for i in 0 1 2; do if [[ "${KEEPER_NAMES[$i]}" == "${NODE_NAME}" ]]; then echo ${KEEPER_IDS[$i]}; fi; done)</server_id>
<log_storage_path>/var/lib/clickhouse/coordination/log</log_storage_path>
<snapshot_storage_path>/var/lib/clickhouse/coordination/snapshots</snapshot_storage_path>
<coordination_settings>
<operation_timeout_ms>10000</operation_timeout_ms>
<session_timeout_ms>30000</session_timeout_ms>
<raft_logs_level>warning</raft_logs_level>
</coordination_settings>
<raft_configuration>
<server><id>1</id><hostname>${KEEPER_IPS[0]}</hostname><port>9234</port></server>
<server><id>2</id><hostname>${KEEPER_IPS[1]}</hostname><port>9234</port></server>
<server><id>3</id><hostname>${KEEPER_IPS[2]}</hostname><port>9234</port></server>
</raft_configuration>
</keeper_server>
</clickhouse>
EOF
echo "==> users.xml hardening"
cat > /etc/clickhouse-server/users.d/01-users.xml <<EOF
<clickhouse>
<users>
<default remove="remove"/>
<${ANALYTICS_USER}>
<password_sha256_hex>${ANALYTICS_PASSWORD_SHA256}</password_sha256_hex>
<networks>
<ip>10.50.0.0/16</ip>
<ip>127.0.0.1</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
<access_management>0</access_management>
</${ANALYTICS_USER}>
</users>
<profiles>
<default>
<max_memory_usage>10000000000</max_memory_usage>
<max_threads>8</max_threads>
<load_balancing>random</load_balancing>
<readonly>0</readonly>
</default>
</profiles>
<quotas>
<default>
<interval>
<duration>3600</duration>
<queries>0</queries>
<errors>0</errors>
<result_rows>0</result_rows>
<read_rows>0</read_rows>
<execution_time>0</execution_time>
</interval>
</default>
</quotas>
</clickhouse>
EOF
mkdir -p /etc/systemd/system/clickhouse-server.service.d
cat > /etc/systemd/system/clickhouse-server.service.d/override.conf <<'OV'
[Service]
LimitNOFILE=500000
OV
systemctl daemon-reload
systemctl enable --now clickhouse-server
echo "==> Waiting for native TLS listener"
for i in {1..30}; do
if ss -lntp | grep -q ":9440\b"; then break; fi
sleep 1
done
else
# Keeper-only tiebreaker node
chown -R clickhouse:clickhouse "${TLS_DIR}"
cat > /etc/clickhouse-keeper/keeper_config.xml <<EOF
<clickhouse>
<listen_host>${NODE_IP}</listen_host>
<listen_host>127.0.0.1</listen_host>
<keeper_server>
<tcp_port>9181</tcp_port>
<server_id>$(for i in 0 1 2; do if [[ "${KEEPER_NAMES[$i]}" == "${NODE_NAME}" ]]; then echo ${KEEPER_IDS[$i]}; fi; done)</server_id>
<log_storage_path>/var/lib/clickhouse-keeper/log</log_storage_path>
<snapshot_storage_path>/var/lib/clickhouse-keeper/snapshots</snapshot_storage_path>
<coordination_settings>
<operation_timeout_ms>10000</operation_timeout_ms>
<session_timeout_ms>30000</session_timeout_ms>
<raft_logs_level>warning</raft_logs_level>
</coordination_settings>
<raft_configuration>
<server><id>1</id><hostname>${KEEPER_IPS[0]}</hostname><port>9234</port></server>
<server><id>2</id><hostname>${KEEPER_IPS[1]}</hostname><port>9234</port></server>
<server><id>3</id><hostname>${KEEPER_IPS[2]}</hostname><port>9234</port></server>
</raft_configuration>
</keeper_server>
</clickhouse>
EOF
mkdir -p /var/lib/clickhouse-keeper /etc/systemd/system/clickhouse-keeper.service.d
chown -R clickhouse:clickhouse /var/lib/clickhouse-keeper
cat > /etc/systemd/system/clickhouse-keeper.service.d/override.conf <<'OV'
[Service]
LimitNOFILE=200000
OV
systemctl daemon-reload
systemctl enable --now clickhouse-keeper
fi
echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(8443|9010|9181|9234|9440)\b' || true
echo
echo "Keeper stat:"
echo "stat" | nc -q 1 127.0.0.1 9181 || true
echo
if [[ "${ROLE}" == "server" ]]; then
echo "Cluster members (run on a server node):"
clickhouse-client --secure --host ${NODE_IP} --port 9440 \
--user ${ANALYTICS_USER} --password "<PLAINTEXT>" \
--query "SELECT cluster, shard_num, replica_num, host_name FROM system.clusters WHERE cluster='analytics'" || true
fi
echo
echo "Repeat on the other replica (ch02) and the keeper tiebreaker (chk03 with ROLE=keeper-only)."
Make it executable and run on each of the three nodes (ch01, ch02, chk03):
chmod 700 /root/setup-clickhouse-node.sh
/root/setup-clickhouse-node.sh
After all three are up, create a real ReplicatedMergeTree table ON CLUSTER analytics and confirm it appears in system.replicas on both replicas with active_replicas = 2. Schedule clickhouse-backup create plus an s3 push nightly — Keeper coordination protects you from a node dying, not from an OPTIMIZE TABLE ... FINAL on a corrupted part that replicates the corruption. Wire a replication-lag alert into your monitoring on absolute_delay from system.replicas.
For a ClickHouse cluster you don’t have to operate yourself, our Cloud PaaS team runs it under Managed Cloud Ops with replication-lag alerting in our monitoring plane — start a scoping call.