|
| 1 | +--- |
| 2 | +slug: /architecture/horizontal-scaling |
| 3 | +sidebar_label: 'Scaling' |
| 4 | +sidebar_position: 10 |
| 5 | +title: 'Scaling' |
| 6 | +description: 'Page describing an example architecture designed to provide scalability' |
| 7 | +--- |
| 8 | + |
| 9 | +import Image from '@theme/IdealImage'; |
| 10 | +import ReplicationShardingTerminology from '@site/docs/_snippets/_replication-sharding-terminology.md'; |
| 11 | +import ConfigFileNote from '@site/docs/_snippets/_config-files.md'; |
| 12 | +import scalingOut1 from '@site/static/images/deployment-guides/scaling-out-1.png'; |
| 13 | +import DedicatedKeeperServers from '@site/docs/deployment-guides/replication-sharding-examples/_snippets/_dedicated_keeper_servers.mdx'; |
| 14 | + |
| 15 | +> In this example, you'll learn how to set up a simple ClickHouse cluster which |
| 16 | +scales. It includes three nodes: two combined ClickHouse plus coordination (ClickHouse Keeper) servers, |
| 17 | +and a third server with only ClickHouse Keeper to finish the quorum of three. |
| 18 | +With this example, we'll create a database, table, and a distributed table that |
| 19 | +will be able to query the data on both of the nodes. |
| 20 | + |
| 21 | +The architecture of the cluster you will be setting up is shown below: |
| 22 | + |
| 23 | +<Image img={scalingOut1} size='md' alt='Architecture diagram for 2 shards and 1 replica' /> |
| 24 | + |
| 25 | +<DedicatedKeeperServers/> |
| 26 | + |
| 27 | +## Prerequisites {#pre-requisites} |
| 28 | + |
| 29 | +- You've set up a [local ClickHouse server](../getting-started/install/install.mdx) before |
| 30 | +- You are familiar with basic configuration concepts of ClickHouse such as [configuration files](/operations/configuration-files) |
| 31 | +- You have docker installed on your machine |
| 32 | + |
| 33 | +<VerticalStepper level="h2"> |
| 34 | + |
| 35 | +## Set up directory structure and test environment {#set-up} |
| 36 | + |
| 37 | +In this tutorial, you will use [Docker compose](https://docs.docker.com/compose/) to |
| 38 | +set up the ClickHouse cluster. This setup could be modified to work |
| 39 | +for separate local machines, virtual machines or cloud instances as well. |
| 40 | + |
| 41 | +Run the following commands to set up the directory structure for this example: |
| 42 | + |
| 43 | +```bash |
| 44 | +mkdir cluster_2S_1R |
| 45 | +cd cluster_2S_1R |
| 46 | + |
| 47 | +# Create clickhouse-keeper directories |
| 48 | +for i in {01..03}; do |
| 49 | + mkdir -p fs/volumes/clickhouse-keeper-${i}/etc/clickhouse-keeper |
| 50 | +done |
| 51 | + |
| 52 | +# Create clickhouse-server directories |
| 53 | +for i in {01..02}; do |
| 54 | + mkdir -p fs/volumes/clickhouse-${i}/etc/clickhouse-server |
| 55 | +done |
| 56 | +``` |
| 57 | + |
| 58 | +Add the following `docker-compose.yml` file to the `clickhouse-cluster` directory: |
| 59 | + |
| 60 | +```yaml title="docker-compose.yml" |
| 61 | + |
| 62 | +``` |
| 63 | + |
| 64 | +Create the following sub-directories and files: |
| 65 | + |
| 66 | +```bash |
| 67 | +for i in {01..02}; do |
| 68 | + mkdir -p fs/volumes/clickhouse-${i}/etc/clickhouse-server/config.d |
| 69 | + mkdir -p fs/volumes/clickhouse-${i}/etc/clickhouse-server/users.d |
| 70 | + touch fs/volumes/clickhouse-${i}/etc/clickhouse-server/config.d/config.xml |
| 71 | + touch fs/volumes/clickhouse-${i}/etc/clickhouse-server/users.d/users.xml |
| 72 | +done |
| 73 | +``` |
| 74 | + |
| 75 | +<ConfigExplanation/> |
| 76 | + |
| 77 | +## Configure ClickHouse nodes {#configure-clickhouse-servers} |
| 78 | + |
| 79 | +### Server setup {#cluster-configuration} |
| 80 | + |
| 81 | +Now modify each empty configuration file `config.xml` located at |
| 82 | +`fs/volumes/clickhouse-{}/etc/clickhouse-server/config.d`. The lines which are |
| 83 | +highlighted below need to be changed to be specific to each node: |
| 84 | + |
| 85 | +```xml |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +| Directory | File | |
| 90 | +|-----------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 91 | +| `fs/volumes/clickhouse-01/etc/clickhouse-server/config.d` | [`config.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-01/etc/clickhouse-server/config.d/config.xml) | |
| 92 | +| `fs/volumes/clickhouse-02/etc/clickhouse-server/config.d` | [`config.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-02/etc/clickhouse-server/config.d/config.xml) | |
| 93 | + |
| 94 | +Each section of the above configuration file is explained in more detail below. |
| 95 | + |
| 96 | +#### Networking and logging {#networking} |
| 97 | + |
| 98 | +<ListenHost/> |
| 99 | + |
| 100 | +Logging is defined in the `<logger>` block. This example configuration gives |
| 101 | +you a debug log that will roll over at 1000M three times: |
| 102 | + |
| 103 | +```xml |
| 104 | + |
| 105 | +``` |
| 106 | + |
| 107 | +For more information on logging configuration, see the comments included in the |
| 108 | +default ClickHouse [configuration file](https://github.com/ClickHouse/ClickHouse/blob/master/programs/server/config.xml). |
| 109 | + |
| 110 | +#### Cluster configuration {#cluster-configuration} |
| 111 | + |
| 112 | +Configuration for the cluster is set up in the `<remote_servers>` block. |
| 113 | +Here the cluster name `cluster_1S_2R` is defined. |
| 114 | + |
| 115 | +The `<cluster_1S_2R></cluster_1S_2R>` block defines the layout of the cluster, |
| 116 | +using the `<shard></shard>` and `<replica></replica>` settings, and acts as a |
| 117 | +template for distributed DDL queries, which are queries that execute across the |
| 118 | +cluster using the `ON CLUSTER` clause. By default, distributed DDL queries |
| 119 | +are allowed, but can also be turned off with setting `allow_distributed_ddl_queries`. |
| 120 | + |
| 121 | +`internal_replication` is set to true so that data is written to just one of the replicas. |
| 122 | + |
| 123 | +```xml |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +<ServerParameterTable/> |
| 128 | + |
| 129 | +#### Keeper configuration {#keeper-config-explanation} |
| 130 | + |
| 131 | +The `<ZooKeeper>` section tells ClickHouse where ClickHouse Keeper (or ZooKeeper) is running. |
| 132 | +As we are using a ClickHouse Keeper cluster, each `<node>` of the cluster needs to be specified, |
| 133 | +along with its hostname and port number using the `<host>` and `<port>` tags respectively. |
| 134 | + |
| 135 | +Set up of ClickHouse Keeper is explained in the next step of the tutorial. |
| 136 | + |
| 137 | +```xml |
| 138 | + |
| 139 | +``` |
| 140 | + |
| 141 | +:::note |
| 142 | +Although it is possible to run ClickHouse Keeper on the same server as ClickHouse Server, |
| 143 | +in production environments we strongly recommend that ClickHouse Keeper runs on dedicated hosts. |
| 144 | +::: |
| 145 | + |
| 146 | +#### Macros configuration {#macros-config-explanation} |
| 147 | + |
| 148 | +Additionally, the `<macros>` section is used to define parameter substitutions for |
| 149 | +replicated tables. These are listed in `system.macros` and allow using substitutions |
| 150 | +like `{shard}` and `{replica}` in queries. |
| 151 | + |
| 152 | +```xml |
| 153 | + |
| 154 | +``` |
| 155 | + |
| 156 | +:::note |
| 157 | +These will be defined uniquely depending on the layout of the cluster. |
| 158 | +::: |
| 159 | + |
| 160 | +### User configuration {#user-config} |
| 161 | + |
| 162 | +Now modify each empty configuration file `users.xml` located at |
| 163 | +`fs/volumes/clickhouse-{}/etc/clickhouse-server/users.d` with the following: |
| 164 | + |
| 165 | +```xml title="/users.d/users.xml" |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +| Directory | File | |
| 170 | +|-----------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 171 | +| `fs/volumes/clickhouse-01/etc/clickhouse-server/users.d` | [`users.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-01/etc/clickhouse-server/users.d/users.xml) | |
| 172 | +| `fs/volumes/clickhouse-02/etc/clickhouse-server/users.d` | [`users.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-02/etc/clickhouse-server/users.d/users.xml) | |
| 173 | + |
| 174 | +In this example, the default user is configured without a password for simplicity. |
| 175 | +In practice, this is discouraged. |
| 176 | + |
| 177 | +:::note |
| 178 | +In this example, each `users.xml` file is identical for all nodes in the cluster. |
| 179 | +::: |
| 180 | + |
| 181 | +## Configure ClickHouse Keeper {#configure-clickhouse-keeper-nodes} |
| 182 | + |
| 183 | +### Keeper setup {#configuration-explanation} |
| 184 | + |
| 185 | +<KeeperConfig/> |
| 186 | + |
| 187 | +| Directory | File | |
| 188 | +|------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 189 | +| `fs/volumes/clickhouse-keeper-01/etc/clickhouse-server/config.d` | [`keeper_config.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-keeper-01/etc/clickhouse-keeper/keeper_config.xml) | |
| 190 | +| `fs/volumes/clickhouse-keeper-02/etc/clickhouse-server/config.d` | [`keeper_config.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-keeper-02/etc/clickhouse-keeper/keeper_config.xml) | |
| 191 | +| `fs/volumes/clickhouse-keeper-03/etc/clickhouse-server/config.d` | [`keeper_config.xml`](https://github.com/ClickHouse/examples/blob/main/docker-compose-recipes/recipes/cluster_1S_2R/fs/volumes/clickhouse-keeper-03/etc/clickhouse-keeper/keeper_config.xml) | |
| 192 | + |
| 193 | +<KeeperConfigExplanation/> |
| 194 | + |
| 195 | +## Test the setup {#test-the-setup} |
| 196 | + |
| 197 | + |
| 198 | +## Create a database {#creating-a-database} |
| 199 | + |
| 200 | + |
| 201 | +## Create a table on the cluster {#creating-a-table} |
| 202 | + |
| 203 | + |
| 204 | +## Insert data {#inserting-data} |
| 205 | + |
| 206 | + |
| 207 | +</VerticalStepper> |
| 208 | + |
| 209 | +## Conclusion {#conclusion} |
| 210 | + |
| 211 | + |
| 212 | + |
0 commit comments