---
title: 1. Создание кластера Clickhouse
description: 
published: true
date: 2025-05-15T20:11:12.215Z
tags: clickhouse, cluster
editor: markdown
dateCreated: 2025-05-15T18:04:57.423Z
---

https://clickhouse.com/docs/guides/sre/keeper/clickhouse-keeper
## Установка и настройка clickhouse, clickhouse-keeper
### Установка clickhouse-keeper
```bash
apt-get install -y apt-transport-https ca-certificates curl gnupg
curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' | sudo gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg
ARCH=$(dpkg --print-architecture)
echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg arch=${ARCH}] https://packages.clickhouse.com/deb stable main" | sudo tee /etc/apt/sources.list.d/clickhouse.list
apt-get update
apt-get install -y clickhouse-keeper
```

#### Готовим конфиг clickhouse-keeper
```
nano  /etc/clickhouse-keeper/keeper_config.xml
```

```xml
<clickhouse>
    <logger>
        <level>warning</level>
        <log>/var/log/clickhouse-keeper/clickhouse-keeper.log</log>
        <errorlog>/var/log/clickhouse-keeper/clickhouse-keeper.err.log</errorlog>
        <size>1000M</size>
        <count>10</count>
        <!-- <console>1</console> --> <!-- Default behavior is autodetection (log to console if not daemon mode and is tty) -->
    </logger>

    <max_connections>4096</max_connections>
    <listen_host>0.0.0.0</listen_host>
    <listen_host>::1</listen_host>

    <keeper_server>
            <tcp_port>9181</tcp_port>
            <server_id>1</server_id>

            <log_storage_path>/var/lib/clickhouse/coordination/logs</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>
                <compress_logs>true</compress_logs>
            </coordination_settings>

            <!-- enable sanity hostname checks for cluster configuration (e.g. if localhost is used with remote endpoints) -->
            <hostname_checks_enabled>false</hostname_checks_enabled>
            <raft_configuration>
                <server>
                    <id>1</id>
                    <hostname>localhost</hostname>
                    <port>9234</port>
                </server>
            </raft_configuration>
    </keeper_server>
</clickhouse>
```

#### Пароль пользователя
```
nano /etc/clickhouse-server/users.d/default-password.xml
```

```xml
<yandex>
    <users>
        <default>
            <password>SuperPass</password>
            <networks>
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>172.16.212.0/24</ip>
            </networks>
        </default>
    </users>
</yandex>
```

### Готовим конфиги clickhouse на серверах  SMVU2-DEV14 и SMVU2-Click-DEV

```bash
nano /etc/clickhouse-server/config.d/remote_servers.xml
```
##### server 1
```xml
<yandex>
    <display_name>smvu2_cluster node 1</display_name>
    <remote_servers replace="true">
        <smvu2_cluster>
            <!--<secret>mysecretphrase</secret> -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>172.16.212.14</host>
                    <port>9000</port>
                    <priority>1</priority>
                    <secure>0</secure>
                    <user>default</user>
                    <password>SuperPass</password>
                </replica>
                <replica>
                    <host>172.16.212.42</host>
                    <port>9000</port>
                    <priority>2</priority>
                    <secure>0</secure>
                    <user>default</user>
                    <password>SuperPass</password>
                </replica>
            </shard>
        </smvu2_cluster>
    </remote_servers>
</yandex>
```

```
nano /etc/clickhouse-server/config.d/shard.xml
```

```xml
<yandex>
    <macros>
        <shard>1</shard>
        <replica>replica1</replica>
    </macros>
</yandex>
```

##### server 2
```xml
<yandex>
    <display_name>smvu2_cluster node 2</display_name>
    <remote_servers replace="true">
        <smvu2_cluster>
            <!--<secret>mysecretphrase</secret> -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>172.16.212.42</host>
                    <port>9000</port>
                    <priority>2</priority>
                    <secure>0</secure>
                    <user>default</user>
                    <password>SuperPass</password>
                </replica>
                <replica>
                    <host>172.16.212.14</host>
                    <port>9000</port>
                    <priority>1</priority>
                    <secure>0</secure>
                    <user>default</user>
                    <password>SuperPass</password>
                </replica>
            </shard>
        </smvu2_cluster>
    </remote_servers>
</yandex>
```

```
nano /etc/clickhouse-server/config.d/shard.xml
```

```xml
<yandex>
    <macros>
        <shard>1</shard>
        <replica>replica2</replica>
    </macros>
</yandex>
```

```
nano /etc/clickhouse-server/config.d/keeper.xml
```

```xml
<yandex>
    <zookeeper>
        <node>
            <host>172.16.212.41</host>
            <port>9181</port>
        </node>
    </zookeeper>
</yandex>
```

```
nano /etc/clickhouse-server/config.d/distributed_ddl.xml
```

```xml
 <yandex>
    <distributed_ddl>
        <task_max_lifetime>86400</task_max_lifetime>
        <profile>default</profile>
        <cleanup_delay>60</cleanup_delay>
        <max_workers>16</max_workers>
    </distributed_ddl>
</yandex>
```

### Проверить сервера, что они в кластере

#### Показать кластер
```sql
SHOW clusters;
```

#### Вывести список серверов в кластере 
```sql
SELECT hostName() FROM clusterAllReplicas('smvu2_cluster', 'system', 'one')
```

#### Keeper status
```sql
SELECT * FROM system.zookeeper WHERE path = '/';
```