feat: initialize

This commit is contained in:
Reinaldy Rafli 2022-10-18 16:58:07 +07:00
commit 1cc35d5ce2
Signed by: aldy505
GPG Key ID: A3F8A7E23DA2AD94
7 changed files with 223 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Reinaldy Rafli <aldy505@proton.me>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

54
README.md Normal file
View File

@ -0,0 +1,54 @@
# Local Clickhouse Cluster with Docker Compose
Sets up a local Clickhouse cluster with 3 nodes (and Zookeeper as a bonus).
To run it:
```bash
docker compose up -d
```
There is no authentication, just log in with the `default` user with no password at all.
There is a cluster already set up called `localcluster`.
Prometheus metrics is available at `/metrics` endpoint on port `9363` of each node.
To connect with Go:
```go
package main
import (
"context"
"github.com/ClickHouse/clickhouse-go/v2"
)
func main() {
clickhouseOptions, err := clickhouse.ParseDSN("clickhouse://default:@127.0.0.1:19000,127.0.0.1:29000,127.0.0.1:39000/default?dial_timeout=200ms&max_execution_time=60&debug=true")
if err != nil {
// handle your error
}
clickhouseConnection, err := clickhouse.Open(clickhouseOptions)
if err != nil {
// handle your error
}
defer func () {
err := clickhouseConnection.Close()
if err != nil {
// handle your error
}
}()
// To query with to the "localcluster"
err = clickhouseConnection.Exec(
context.Background(),
`CREATE DATABASE IF NOT EXISTS "sampledatabase" ON CLUSTER "localcluster"`,
)
if err != nil {
// handle your error
}
}
```

40
config.xml Normal file
View File

@ -0,0 +1,40 @@
<clickhouse>
<listen_host>::1</listen_host>
<listen_host>0.0.0.0</listen_host>
<remote_servers>
<localcluster>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>ch1</host>
<port>9000</port>
</replica>
<replica>
<host>ch2</host>
<port>9000</port>
</replica>
<replica>
<host>ch3</host>
<port>9000</port>
</replica>
</shard>
</localcluster>
</remote_servers>
<zookeeper>
<node>
<host>zookeeper</host>
<port>2181</port>
</node>
</zookeeper>
<prometheus>
<endpoint>/metrics</endpoint>
<port>9363</port>
<metrics>true</metrics>
<events>true</events>
<asynchronous_metrics>true</asynchronous_metrics>
<status_info>true</status_info>
</prometheus>
</clickhouse>

90
docker-compose.yml Normal file
View File

@ -0,0 +1,90 @@
services:
ch1:
image: clickhouse/clickhouse-server:22.8.1
restart: on-failure:10
volumes:
- ./config.xml:/etc/clickhouse-server/config.d/local.xml
- ./macro1.xml:/etc/clickhouse-server/config.d/macros.xml
- ch1data:/var/lib/clickhouse
ports:
- '18123:8123'
- '19000:9000'
ulimits:
nproc: 65536
nofile:
soft: 252144
hard: 252144
healthcheck:
test: wget --spider -q localhost:8123/ping
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
depends_on:
zookeeper:
condition: service_healthy
ch2:
image: clickhouse/clickhouse-server:22.8.1
restart: on-failure:10
volumes:
- ./config.xml:/etc/clickhouse-server/config.d/local.xml
- ./macro2.xml:/etc/clickhouse-server/config.d/macros.xml
- ch2data:/var/lib/clickhouse
ports:
- '28123:8123'
- '29000:9000'
ulimits:
nproc: 65536
nofile:
soft: 252144
hard: 252144
healthcheck:
test: wget --spider -q localhost:8123/ping
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
depends_on:
zookeeper:
condition: service_healthy
ch1:
condition: service_started
ch3:
image: clickhouse/clickhouse-server:22.8.1
restart: on-failure:10
volumes:
- ./config.xml:/etc/clickhouse-server/config.d/local.xml
- ./macro3.xml:/etc/clickhouse-server/config.d/macros.xml
- ch3data:/var/lib/clickhouse
ports:
- '38123:8123'
- '39000:9000'
ulimits:
nproc: 65536
nofile:
soft: 252144
hard: 252144
healthcheck:
test: wget --spider -q localhost:8123/ping
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
depends_on:
zookeeper:
condition: service_healthy
ch1:
condition: service_started
zookeeper:
image: zookeeper:3.8.0
restart: on-failure:10
healthcheck:
test: [ "CMD", "sh", "-c", "nc -nz 127.0.0.1 2181" ]
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
volumes:
ch1data:
ch2data:
ch3data:

6
macro1.xml Normal file
View File

@ -0,0 +1,6 @@
<clickhouse>
<macros replace="replace">
<cluster>localcluster</cluster>
<replica>ch1</replica>
</macros>
</clickhouse>

6
macro2.xml Normal file
View File

@ -0,0 +1,6 @@
<clickhouse>
<macros replace="replace">
<cluster>localcluster</cluster>
<replica>ch2</replica>
</macros>
</clickhouse>

6
macro3.xml Normal file
View File

@ -0,0 +1,6 @@
<clickhouse>
<macros replace="replace">
<cluster>localcluster</cluster>
<replica>ch3</replica>
</macros>
</clickhouse>