commit 1cc35d5ce267e91d028d3a670fd27f9954ff8c9a Author: Reinaldy Rafli Date: Tue Oct 18 16:58:07 2022 +0700 feat: initialize diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..32adbd1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Reinaldy Rafli + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0b2a55 --- /dev/null +++ b/README.md @@ -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 + } +} +``` \ No newline at end of file diff --git a/config.xml b/config.xml new file mode 100644 index 0000000..db2d249 --- /dev/null +++ b/config.xml @@ -0,0 +1,40 @@ + + ::1 + 0.0.0.0 + + + + true + + ch1 + 9000 + + + ch2 + 9000 + + + ch3 + 9000 + + + + + + + + zookeeper + 2181 + + + + + /metrics + 9363 + + true + true + true + true + + \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..853ce61 --- /dev/null +++ b/docker-compose.yml @@ -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: diff --git a/macro1.xml b/macro1.xml new file mode 100644 index 0000000..988bb11 --- /dev/null +++ b/macro1.xml @@ -0,0 +1,6 @@ + + + localcluster + ch1 + + \ No newline at end of file diff --git a/macro2.xml b/macro2.xml new file mode 100644 index 0000000..2ab1735 --- /dev/null +++ b/macro2.xml @@ -0,0 +1,6 @@ + + + localcluster + ch2 + + \ No newline at end of file diff --git a/macro3.xml b/macro3.xml new file mode 100644 index 0000000..304fc65 --- /dev/null +++ b/macro3.xml @@ -0,0 +1,6 @@ + + + localcluster + ch3 + + \ No newline at end of file