レプリケーションとスケーリング
この例では、レプリケーションとスケールの両方に対応したシンプルな ClickHouse クラスターをセットアップする方法を学びます。クラスターは 2 つのシャードと 2 つのレプリカに加え、クラスター内の調整とクォーラムの維持を行う 3 ノード構成の ClickHouse Keeper クラスターで構成されています。
これから構成するクラスターのアーキテクチャは、次の図のとおりです。

Although it is possible to run ClickHouse Server and ClickHouse Keeper combined on the same server, we strongly recommend using dedicated hosts for ClickHouse keeper in production environments, which is the approach we will demonstrate in this example.
Keeper servers can be smaller, and 4GB RAM is generally enough for each Keeper server until your ClickHouse Servers grow large.
前提条件
- すでに ローカルの ClickHouse サーバー をセットアップしている
- 設定ファイル など、ClickHouse の基本的な設定の概念に慣れている
- 使用しているマシンに Docker がインストールされている
ディレクトリ構造とテスト環境のセットアップ
The following steps will walk you through setting up the cluster from scratch. If you prefer to skip these steps and jump straight to running the cluster, you can obtain the example files from the examples repository 'docker-compose-recipes' directory.
このチュートリアルでは、Docker composeを使用してClickHouseクラスタをセットアップします。このセットアップは、個別のローカルマシン、仮想マシン、クラウドインスタンスでも動作するように変更可能です。
この例のディレクトリ構造をセットアップするには、以下のコマンドを実行します:
以下の docker-compose.yml ファイルを clickhouse-cluster ディレクトリに追加します:
以下のサブディレクトリとファイルを作成します:
- The
config.ddirectory contains ClickHouse server configuration fileconfig.xml, in which custom configuration for each ClickHouse node is defined. This configuration gets combined with the defaultconfig.xmlClickHouse configuration file that comes with every ClickHouse installation. - The
users.ddirectory contains user configuration fileusers.xml, in which custom configuration for users is defined. This configuration gets combined with the default ClickHouseusers.xmlconfiguration file that comes with every ClickHouse installation.
It is a best practice to make use of the config.d and users.d directories when
writing your own configuration, rather than directly modifying the default configuration
in /etc/clickhouse-server/config.xml and etc/clickhouse-server/users.xml.
The line
Ensures that the configuration sections defined in the config.d and users.d
directories override the default configuration sections defined in the default
config.xml and users.xml files.
ClickHouseノードの設定
サーバーのセットアップ
次に、fs/volumes/clickhouse-{}/etc/clickhouse-server/config.dに配置されている各空の設定ファイルconfig.xmlを修正します。以下で強調表示されている行は、各ノードに固有の内容に変更する必要があります:
| ディレクトリ | ファイル |
|---|---|
fs/volumes/clickhouse-01/etc/clickhouse-server/config.d | config.xml |
fs/volumes/clickhouse-02/etc/clickhouse-server/config.d | config.xml |
fs/volumes/clickhouse-03/etc/clickhouse-server/config.d | config.xml |
fs/volumes/clickhouse-04/etc/clickhouse-server/config.d | config.xml |
上記の設定ファイルの各セクションについて、以下で詳細に説明します。
ネットワーキングとロギング
External communication to the network interface is enabled by activating the listen host setting. This ensures that the ClickHouse server host is reachable by other hosts:
The port for the HTTP API is set to 8123:
The TCP port for interaction by ClickHouse's native protocol between clickhouse-client
and other native ClickHouse tools, and clickhouse-server and other clickhouse-servers
is set to 9000:
ログ設定は <logger> ブロックで定義します。この設定例では、1000Mに達するごとに3回までローテーションするデバッグログを出力します:
ログ設定の詳細については、デフォルトのClickHouse設定ファイルに含まれているコメントを参照してください。
クラスター設定
クラスタの設定は <remote_servers> ブロックで設定します。
ここでクラスタ名 cluster_2S_2R を定義しています。
<cluster_2S_2R></cluster_2S_2R> ブロックは、<shard></shard> および <replica></replica> 設定を使用してクラスタのレイアウトを定義し、分散DDLクエリのテンプレートとして機能します。分散DDLクエリは、ON CLUSTER 句を使用してクラスタ全体で実行されるクエリです。デフォルトでは分散DDLクエリは許可されていますが、allow_distributed_ddl_queries 設定で無効化することもできます。
internal_replication を true に設定すると、データはレプリカの1つにのみ書き込まれます。
<cluster_2S_2R></cluster_2S_2R> セクションは、クラスタのレイアウトを定義し、
分散DDLクエリ(ON CLUSTER 句を使用してクラスタ全体で実行されるクエリ)のテンプレートとして機能します。
Keeper の設定
<ZooKeeper> セクションは、ClickHouse Keeper(または ZooKeeper)の実行場所を ClickHouse に指定します。
ClickHouse Keeper クラスタを使用する場合、クラスタの各 <node> を指定する必要があります。
ホスト名とポート番号はそれぞれ <host> タグと <port> タグを使用して指定します。
ClickHouse Keeperのセットアップは、チュートリアルの次のステップで説明されています。
ClickHouse KeeperをClickHouse Serverと同じサーバー上で実行することは可能ですが、本番環境では専用ホスト上で実行することを強く推奨します。
マクロの設定
また、<macros> セクションは、レプリケーテッドテーブルのパラメータ置換を定義するために使用されます。これらは system.macros に記載され、クエリ内で {shard} や {replica} などの置換を使用できます。
ユーザー設定
次に、fs/volumes/clickhouse-{}/etc/clickhouse-server/users.d に配置されている各空の設定ファイル users.xml を以下の内容で変更します:
この例では、簡略化のためデフォルトユーザーをパスワードなしで設定しています。 実際の運用環境では、この設定は推奨されません。
この例では、クラスター内のすべてのノードでusers.xmlファイルが同一です。
ClickHouse Keeperの設定
次に、コーディネーションに使用されるClickHouse Keeperを設定します。
Keeperのセットアップ
In order for replication to work, a ClickHouse keeper cluster needs to be set up and configured. ClickHouse Keeper provides the coordination system for data replication, acting as a stand in replacement for Zookeeper, which could also be used. ClickHouse Keeper is, however, recommended, as it provides better guarantees and reliability and uses fewer resources than ZooKeeper. For high availability and to keep quorum, it is recommended to run at least three ClickHouse Keeper nodes.
ClickHouse Keeper can run on any node of the cluster alongside ClickHouse, although it is recommended to have it run on a dedicated node which allows scaling and managing the ClickHouse Keeper cluster independently of the database cluster.
Create the keeper_config.xml files for each ClickHouse Keeper node
using the following command from the root of the example folder:
Modify the empty configuration files which were created in each
node directory fs/volumes/clickhouse-keeper-{}/etc/clickhouse-keeper. The
highlighted lines below need to be changed to be specific to each node:
| ディレクトリ | ファイル |
|---|---|
fs/volumes/clickhouse-keeper-01/etc/clickhouse-keeper | keeper_config.xml |
fs/volumes/clickhouse-keeper-02/etc/clickhouse-keeper | keeper_config.xml |
fs/volumes/clickhouse-keeper-03/etc/clickhouse-keeper | keeper_config.xml |
Each configuration file will contain the following unique configuration (shown below).
The server_id used should be unique for that particular ClickHouse Keeper node
in the cluster and match the server <id> defined in the <raft_configuration> section.
tcp_port is the port used by clients of ClickHouse Keeper.
The following section is used to configure the servers that participate in the quorum for the raft consensus algorithm:
ClickHouse Cloud removes the operational burden associated with managing shards and replicas. The platform automatically handles high availability, replication, and scaling decisions. Compute and storage are separate and scale based on demand without requiring manual configuration or ongoing maintenance.
セットアップのテスト
マシン上でDockerが実行されていることを確認してください。
cluster_2S_2Rディレクトリのルートからdocker-compose upコマンドを使用してクラスタを起動します:
dockerがClickHouseとKeeperのイメージをプルし始め、 その後コンテナが起動する様子が表示されます:
クラスタが稼働していることを確認するには、いずれかのノードに接続して以下のクエリを実行します。最初のノードへの接続コマンドは次のとおりです:
成功すると、ClickHouseクライアントのプロンプトが表示されます:
以下のクエリを実行して、各ホストに定義されているクラスタトポロジを確認します:
以下のクエリを実行して、ClickHouse Keeperクラスタのステータスを確認します:
The mntr command is also commonly used to verify that ClickHouse Keeper is
running and to get state information about the relationship of the three Keeper nodes.
In the configuration used in this example, there are three nodes working together.
The nodes will elect a leader, and the remaining nodes will be followers.
The mntr command gives information related to performance, and whether a particular
node is a follower or a leader.
You may need to install netcat in order to send the mntr command to Keeper.
Please see the nmap.org page for download information.
Run the command below from a shell on clickhouse-keeper-01, clickhouse-keeper-02, and
clickhouse-keeper-03 to check the status of each Keeper node. The command
for clickhouse-keeper-01 is shown below:
The response below shows an example response from a follower node:
The response below shows an example response from a leader node:
これで、2つのシャードと2つのレプリカを持つClickHouseクラスタのセットアップが完了しました。 次のステップでは、クラスタにテーブルを作成します。
データベースを作成する
クラスタが正しくセットアップされ、実行されていることを確認したので、UK property pricesサンプルデータセットチュートリアルで使用されているものと同じテーブルを再作成します。このデータセットは、1995年以降にイングランドとウェールズで取引された不動産物件の価格データ約3,000万行で構成されています。
各ホストのクライアントに接続するには、以下の各コマンドを別々のターミナルタブまたはウィンドウから実行します:
各ホストのclickhouse-clientから以下のクエリを実行して、デフォルトのデータベース以外にデータベースが作成されていないことを確認してください:
clickhouse-01 クライアントから、ON CLUSTER 句を使用して以下の分散型 DDL クエリを実行し、uk という名前の新しいデータベースを作成します:
各ホストのクライアントから先ほどと同じクエリを再度実行し、clickhouse-01からのみクエリを実行したにもかかわらず、クラスタ全体でデータベースが作成されていることを確認できます。
クラスタ上にテーブルを作成する
データベースが作成されたので、次はレプリケーション機能を持つテーブルを作成します。
いずれかのホストクライアントから以下のクエリを実行します:
これは、英国不動産価格サンプルデータセットチュートリアルの元のCREATE文で使用されたクエリと同一です。ただし、ON CLUSTER句とReplicatedMergeTreeエンジンの使用が異なる点に注意してください。
ON CLUSTER句は、CREATE、DROP、ALTER、RENAMEなどのDDL(Data Definition Language)クエリを分散実行するために設計されており、これらのスキーマ変更をクラスタ内のすべてのノードに適用します。
ReplicatedMergeTreeエンジンは、通常のMergeTreeテーブルエンジンと同様に動作しますが、データのレプリケーションも実行します。
2つのパラメータの指定が必要です:
zoo_path: テーブルのメタデータが格納されている Keeper/ZooKeeper のパス。replica_name: テーブルのレプリカ名。
zoo_pathパラメータは任意の値に設定できますが、プレフィックスを使用する慣例に従うことを推奨します
各項目の説明:
{database}と{table}は自動的に置き換えられます。{shard}と{replica}は、各 ClickHouse ノードのconfig.xmlファイル内であらかじめ定義されたマクロです。
各ホストのクライアントから以下のクエリを実行し、クラスタ全体でテーブルが作成されていることを確認してください:
分散テーブルへのデータ挿入
テーブルへのデータ挿入時にはON CLUSTERを使用できません。これはINSERT、UPDATE、DELETEなどのDML(Data Manipulation Language:データ操作言語)クエリには適用されないためです。 データを挿入するには、Distributedテーブルエンジンを利用する必要があります。
2シャード1レプリカ構成のクラスタをセットアップするガイドで学んだように、分散テーブルとは異なるホスト上に配置されたシャードにアクセス可能なテーブルであり、Distributedテーブルエンジンを使用して定義されます。
分散テーブルは、クラスタ内の全シャードに対するインターフェースとして機能します。
いずれかのホストクライアントから、以下のクエリを実行して、前のステップで作成した既存のレプリケートテーブルを使用する分散テーブルを作成します:
各ホストのukデータベースに、以下のテーブルが表示されるようになります:
データは、以下のクエリを使用して、いずれかのホストクライアントから uk_price_paid_distributed テーブルに挿入できます:
次のクエリを実行して、挿入されたデータがクラスタの各ノードに均等に分散されていることを確認します:
結論
2つのシャードと2つのレプリカを持つこのクラスター・トポロジーの利点は、スケーラビリティとフォールトトレランスの両方を提供できる点にあります。 データは個別のホスト間で分散されるため、ノードごとのストレージおよび I/O 要件が削減される一方で、クエリは両方のシャードにまたがって並列に処理されるため、パフォーマンスとメモリ効率が向上します。 重要な点として、各シャードには別ノード上にバックアップのレプリカが存在するため、クラスターはノードを1つ失ってもクエリを中断することなく処理し続けられます。
このクラスター・トポロジーの主な欠点は、ストレージのオーバーヘッドが増加することです。レプリカなしの構成と比較すると、各シャードが複製されるため、必要なストレージ容量は2倍になります。 さらに、クラスターは単一ノード障害には耐えられますが、どのノードが障害するかやシャードの配置によっては、2ノードが同時に失われるとクラスターが動作不能になる可能性があります。 このトポロジーは可用性とコストのバランスを取るものであり、より高いレプリケーション係数のコストをかけずに、一定レベルのフォールトトレランスが求められる本番環境に適しています。
スケーラビリティとフォールトトレランスの両方を提供する ClickHouse Cloud におけるクエリ処理の仕組みについては、"Parallel Replicas" セクションを参照してください。