# clickhouse-cluster

# Some Essential checks

## Check keeper :

echo ruok | nc 127.0.0.1 9181

sudo systemctl restart clickhouse-keeper

Check cluster

<span style="background-color: #ffffff; padding: 0px 0px 0px 2px;"><span style="color: #000000; background-color: #ffffff; font-family: 'Monospace'; font-size: 10pt; white-space: pre;"><span style="color: #800000; font-weight: bold;">SELECT</span> \* <span style="color: #800000; font-weight: bold;">FROM</span> <span style="color: #956037; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">system</span><span style="text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">.</span><span style="color: #6c5151; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">clusters</span> <span style="color: #800000; font-weight: bold;">WHERE</span> <span style="color: #800000; font-weight: bold;">cluster</span>=<span style="color: #008000;">'default'</span><span style="color: #ff0000;">;</span></span></span>

# Additional Keeper tests

### **Cluster monitoring**

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-echo-mntr-%7C-nc-127.0"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-built_in">echo</span> mntr | nc 127.0.0.1 9181`</div></div>Should show:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-zk_server_state-lead"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-attribute">zk_server_state</span>    leader   (<span class="hljs-literal">on</span> one node)zk_server_state    follower (<span class="hljs-literal">on</span> other two nodes)`</div></div>### **Server stats**

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-echo-stat-%7C-nc-127.0"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-built_in">echo</span> <span class="hljs-built_in">stat</span> | nc 127.0.0.1 9181`</div></div>### **List znodes**

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-echo-ls-%2F-%7C-nc-127.0"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-built_in">echo</span> <span class="hljs-built_in">ls</span> / | nc 127.0.0.1 9181`</div></div>

# Create db and replicated tables

# **Build Replicated ClickHouse Tables**

Keeper is fully working.  
Now you can create **replicated database + replicated tables** that sync across all 3 servers.

---

# 🚀 Step-by-step instructions (copy/paste)

## 1. Create database on all nodes:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-create-database-repl"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">CREATE</span> DATABASE replicated_db;`</div></div>## 2. Create table with replication

### Node1:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-create-table-replica"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">CREATE TABLE</span> replicated_db.events(    id UInt64,    msg String)ENGINE <span class="hljs-operator">=</span> ReplicatedMergeTree(<span class="hljs-string">'/clickhouse/tables/events'</span>, <span class="hljs-string">'replica1'</span>)<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> id;`</div></div>### Node2:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-create-table-replica-1"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">CREATE TABLE</span> replicated_db.events(    id UInt64,    msg String)ENGINE <span class="hljs-operator">=</span> ReplicatedMergeTree(<span class="hljs-string">'/clickhouse/tables/events'</span>, <span class="hljs-string">'replica2'</span>)<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> id;`</div></div>### Node3:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-create-table-replica-2"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">CREATE TABLE</span> replicated_db.events(    id UInt64,    msg String)ENGINE <span class="hljs-operator">=</span> ReplicatedMergeTree(<span class="hljs-string">'/clickhouse/tables/events'</span>, <span class="hljs-string">'replica3'</span>)<span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> id;`</div></div>---

# 🧪 Test replication:

Insert into Node1:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-insert-into-replicat"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">INSERT INTO</span> replicated_db.events <span class="hljs-keyword">VALUES</span> (<span class="hljs-number">1</span>, <span class="hljs-string">'hello'</span>);`</div></div>Check on Node2:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-select-%2A-from-replic"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> replicated_db.events;`</div></div>Check on Node3:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-select-%2A-from-replic-1"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> replicated_db.events;`</div></div>You will see:

<div class="contain-inline-size rounded-2xl corner-superellipse/1.1 relative bg-token-sidebar-surface-primary" id="bkmrk-1-%7C-hello"><div class="sticky top-9"><div class="absolute end-0 bottom-0 flex h-9 items-center pe-2"><div class="bg-token-bg-elevated-secondary text-token-text-secondary flex items-center gap-4 rounded-sm px-2 font-sans text-xs">  
</div></div></div><div class="overflow-y-auto p-4" dir="ltr">`1 | hello`</div></div>

# New Page

<span style="color: #800000; font-weight: bold;">CREATE</span> <span style="color: #800000; font-weight: bold;">TABLE</span> <span style="color: #8e00c6;">my\_table</span> <span style="color: #800000; font-weight: bold;">ON</span> <span style="color: #800000; font-weight: bold;">CLUSTER</span> <span style="color: #000000;">default</span>

(

<span style="color: #000080; font-weight: bold;">`id`</span> <span style="color: #000080; font-weight: bold;">UInt64</span>,

<span style="color: #000080; font-weight: bold;">`name`</span> <span style="color: #000080; font-weight: bold;">String</span>,

<span style="color: #000080; font-weight: bold;">`created\_at`</span> <span style="color: #000080; font-weight: bold;">String</span>

)

<span style="color: #800000; font-weight: bold;">ENGINE</span> = <span style="color: #000000;">MergeTree</span>

<span style="color: #800000; font-weight: bold;">ORDER</span> <span style="color: #800000; font-weight: bold;">BY</span> <span style="color: #000000;">id</span>

<span style="color: #800000; font-weight: bold;">INSERT</span> <span style="color: #800000; font-weight: bold;">INTO</span> <span style="color: #956037;">demo</span>.<span style="color: #8e00c6;">my\_table</span>

(<span style="color: #006464;">id</span>, <span style="color: #006464;">name</span>, <span style="color: #006464;">created\_at</span>)

<span style="color: #800000; font-weight: bold;">VALUES</span>(<span style="color: #0000ff;">0</span>, <span style="color: #008000;">'228'</span>, <span style="color: #008000;">''</span>)<span style="color: #ff0000;">;</span>

<span style="color: #800000; font-weight: bold;">select</span> \* <span style="color: #800000; font-weight: bold;">from</span> <span style="color: #6c5151; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">my\_table</span>

<span style="color: #800000; font-weight: bold;">INSERT</span> <span style="color: #800000; font-weight: bold;">INTO</span> <span style="color: #956037; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">demo</span><span style="text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">.</span><span style="color: #6c5151; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">my\_table\_all</span>

(<span style="color: #000000;">id</span>, <span style="color: #000000;">name</span>, <span style="color: #000000;">created\_at</span>)

<span style="color: #800000; font-weight: bold;">VALUES</span>(<span style="color: #0000ff;">0</span>, <span style="color: #008000;">'X'</span>, <span style="color: #008000;">''</span>)<span style="color: #ff0000;">;</span>

<span style="color: #800000; font-weight: bold;">CREATE</span> <span style="color: #800000; font-weight: bold;">TABLE</span> <span style="color: #8e00c6;">my\_table\_all</span> <span style="color: #800000; font-weight: bold;">ON</span> <span style="color: #800000; font-weight: bold;">CLUSTER</span> <span style="color: #000000;">default</span> <span style="color: #800000; font-weight: bold;">AS</span> <span style="color: #000000;">my\_table</span>

<span style="color: #800000; font-weight: bold;">ENGINE</span> = <span style="color: #000000;">Distributed</span>(<span style="color: #000000;">default</span>, <span style="color: #000000;">demo</span>, <span style="color: #000000;">my\_table</span>, <span style="color: #000000;">rand</span>())

<span style="color: #800000; font-weight: bold;">select</span> \* <span style="color: #800000; font-weight: bold;">from</span> <span style="color: #6c5151; text-decoration: underline; text-decoration-color: #ff0080; text-decoration-style: wavy;">my\_table\_all</span>