Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1alpha1/clickhousecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (s *ClickHouseNetworkPolicySpec) Enabled() bool {
// +kubebuilder:validation:XValidation:rule="has(self.keeperClusterRef) != has(self.externalKeeper)",message="exactly one of keeperClusterRef or externalKeeper must be set"
type ClickHouseClusterSpec struct {
// Number of replicas in the single shard.
// Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
// +optional
// +kubebuilder:default:=3
// +kubebuilder:validation:Minimum=0
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ const (
ConditionReasonVersionOutdated ConditionReason = "VersionOutdated"
ConditionReasonUpgradeCheckFailed ConditionReason = "UpgradeCheckFailed"

// ConditionTypeReady indicates that cluster is ready to serve client requests.
// ConditionTypeReady indicates that cluster is ready to serve client requests or is fully scaled to zero.
ConditionTypeReady ConditionType = "Ready"
ConditionReasonScaledToZero ConditionReason = "ScaledToZero"
ClickHouseConditionAllShardsReady ConditionReason = "AllShardsReady"
ClickHouseConditionSomeShardsNotReady ConditionReason = "SomeShardsNotReady"
KeeperConditionReasonStandaloneReady ConditionReason = "StandaloneReady"
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const (
EventReasonClickHouseWarning EventReason = "ClickHouseWarning"
)

// Event reason for a ClickHouseCluster running without Keeper nodes.
const (
EventReasonNoKeeperNodes EventReason = "NoKeeperNodes"
)

// EventAction represents the action associated with an event.
type EventAction = string

Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/keepercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (s *KeeperNetworkPolicySpec) Enabled() bool {

// KeeperClusterSpec defines the desired state of KeeperCluster.
type KeeperClusterSpec struct {
// Number of replicas in the cluster
// Number of replicas in the cluster.
// Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
// +optional
// +kubebuilder:default:=3
// +kubebuilder:validation:Enum=0;1;3;5;7;9;11;13;15
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/clickhouse.com_clickhouseclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3487,7 +3487,9 @@ spec:
rule: '!has(self.topologyMinDomains) || has(self.topologyZoneKey)'
replicas:
default: 3
description: Number of replicas in the single shard.
description: |-
Number of replicas in the single shard.
Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
format: int32
minimum: 0
type: integer
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/clickhouse.com_keeperclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3189,7 +3189,9 @@ spec:
rule: '!has(self.topologyMinDomains) || has(self.topologyZoneKey)'
replicas:
default: 3
description: Number of replicas in the cluster
description: |-
Number of replicas in the cluster.
Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
enum:
- 0
- 1
Expand Down
4 changes: 3 additions & 1 deletion dist/chart-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ clickhouse:
# podTemplate: {}

# Number of replicas in the single shard.
# Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
replicas: 3

# Configuration parameters for ClickHouse server.
Expand Down Expand Up @@ -171,7 +172,8 @@ keeper:
# Parameters passed to the Keeper pod spec.
# podTemplate: {}

# Number of replicas in the cluster
# Number of replicas in the cluster.
# Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
replicas: 3

# Configuration parameters for ClickHouse Keeper server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3490,7 +3490,9 @@ spec:
rule: '!has(self.topologyMinDomains) || has(self.topologyZoneKey)'
replicas:
default: 3
description: Number of replicas in the single shard.
description: |-
Number of replicas in the single shard.
Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
format: int32
minimum: 0
type: integer
Expand Down
4 changes: 3 additions & 1 deletion dist/chart/templates/crd/keeperclusters.clickhouse.com.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,9 @@ spec:
rule: '!has(self.topologyMinDomains) || has(self.topologyZoneKey)'
replicas:
default: 3
description: Number of replicas in the cluster
description: |-
Number of replicas in the cluster.
Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept.
enum:
- 0
- 1
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ spec:
| `Disabled` | The operator does **not** create PDBs and **deletes** any existing ones with matching labels. Useful for development clusters where every voluntary disruption should be allowed. |
| `Ignored` | The operator neither creates nor deletes PDBs. Existing PDBs are left alone. Use this when another system (e.g. policy admission, GitOps tool) owns PDB management for you. |

With `Enabled`, the operator deletes the PDBs while the cluster is [scaled to zero](/products/kubernetes-operator/guides/scaling#scaling-to-zero), since no Pods are left to protect.

Example — disable PDB management completely on a development cluster:

```yaml
Expand Down Expand Up @@ -982,7 +984,7 @@ Every replica Pod carries the `clickhouse.com/ReplicaInitialized` readiness gate

The operator never drops a populated non-Replicated `default` database. Such a replica still starts serving client traffic after schema preparation, but the cluster reports `SchemaInSync=False` with reason `DefaultDatabaseNotReplicated` until you resolve that database yourself.

Before a replica is removed on scale-down, the operator unloads client traffic from it, replicates its remaining data to the surviving replicas, and only then deletes it. On shutdown, the replica waits up to a minute for the surviving replicas to fetch the data parts only it has.
Before a replica is removed on scale-down, the operator unloads client traffic from it, replicates its remaining data to the surviving replicas, and only then deletes it. On shutdown, the replica waits up to a minute for the surviving replicas to fetch the data parts only it has. [Scaling to zero](/products/kubernetes-operator/guides/scaling#scaling-to-zero) leaves no surviving replicas, so each replica keeps its data on its volume.

### Server logging {#server-logging}

Expand Down
18 changes: 16 additions & 2 deletions docs/guides/kubernetes-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ apply an owned resource during reconciliation.

### Cluster readiness {#cluster-readiness}

Emitted on both kinds when the cluster crosses a readiness boundary.
Emitted on both kinds when the cluster crosses a readiness boundary. Stopping a
cluster by scaling it to zero emits no `ClusterNotReady`.

| Reason | Type | Meaning |
|---|---|---|
| `ClusterReady` | Normal | The cluster became ready: every ClickHouse shard has at least one ready replica, or the Keeper quorum has a leader and enough followers (or its single standalone replica is up). |
| `ClusterReady` | Normal | The cluster became ready: every ClickHouse shard has at least one ready replica, or the Keeper quorum has a leader and enough followers (or its single standalone replica is up), or the cluster is scaled to zero and no replicas are left. |
| `ClusterNotReady` | Warning | The cluster left the ready state — a ClickHouse shard has no ready replica left, or the Keeper quorum lost its leader or too many followers. |

### Scaling {#scaling}
Expand Down Expand Up @@ -115,6 +116,17 @@ the operator cannot use. See the External Secret feature in the
| `ExternalSecretNotFound` | Warning | The referenced Secret does not exist in the cluster's namespace. |
| `ExternalSecretInvalid` | Warning | The Secret exists but is missing required keys (only reported under the `Observe` policy). |

### Keeper availability {#keeper-availability}

Emitted on `ClickHouseCluster` repeatedly while the cluster has replicas but its
Keeper has no nodes, for example after the `KeeperCluster` was
[scaled to zero](/products/kubernetes-operator/guides/scaling#scaling-to-zero)
before the `ClickHouseCluster`.

| Reason | Type | Meaning |
|---|---|---|
| `NoKeeperNodes` | Warning | The Keeper has no nodes, so replicated tables are read-only until it is scaled up. |

### Version checks {#version-checks}

Emitted by the version checks for `ClickHouseCluster` and `KeeperCluster`.
Expand Down Expand Up @@ -182,6 +194,8 @@ A few common signals and where they point:
operator can use it.
- **`ClickHouseWarning`** — the problem is inside ClickHouse, not the operator.
Treat the message as you would a row from `system.warnings`.
- **`NoKeeperNodes`** — ClickHouse runs while its `KeeperCluster` is scaled to
zero. Scale the Keeper back up, or stop the `ClickHouseCluster` first.

## Related guides {#related-guides}

Expand Down
20 changes: 16 additions & 4 deletions docs/guides/scaling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
```

On scale up the operator creates the new per-replica StatefulSets, synchronizes the schema to the new replicas (see [Automatic schema sync](#automatic-schema-sync)), and only then lets each new pod become ready for client traffic.
On scale down it removes the surplus StatefulSets and cleans up the stale replicated-database replica registrations the removed replicas left behind.
On scale down it removes the surplus StatefulSets and cleans up the stale replicated-database replica registrations the removed replicas left behind, except when [scaling to zero](#scaling-to-zero).

## Scaling shards {#scaling-shards}

Expand All @@ -44,13 +44,23 @@ spec:
Each shard holds a distinct slice of the data, and the operator does not copy or move rows between shards.
A `Distributed` table or an explicit routing scheme decides which shard a row lands on, so adding a shard gives new writes somewhere to land without touching the rows already stored in the existing shards.

## Scaling to zero {#scaling-to-zero}

Set `replicas: 0` to stop a `ClickHouseCluster` or a `KeeperCluster`. The operator removes the per-replica resources, such as StatefulSets and ConfigMaps, and the PodDisruptionBudgets it manages, and keeps the PersistentVolumeClaims and the replica registrations in Keeper. Once no replica Pods are left, the cluster reports `Ready` with the `ScaledToZero` reason. Scale back to the previous replica count to resume: each replica reattaches its volume.

Unlike a regular scale down, stopping does not replicate data between replicas: each replica keeps its own data on its volume, so resuming with fewer replicas than before leaves the data held only by the others on their volumes. Data survives only on persistent volumes: a `ClickHouseCluster` without `dataVolumeClaimSpec` loses its data, and a `KeeperCluster` without one loses the replication metadata of every `ClickHouseCluster` that uses it.

A `KeeperCluster` stops and resumes all quorum members at once instead of one at a time, so resume it with the same replica count it had before.

To stop ClickHouse together with its Keeper, scale the `ClickHouseCluster` to zero first, wait until it reports `ScaledToZero`, and then scale the `KeeperCluster`. Resume in the reverse order: wait until all `KeeperCluster` replicas are ready (`status.readyReplicas`) before scaling up the `ClickHouseCluster`. While a `ClickHouseCluster` runs without Keeper nodes, the operator emits the [`NoKeeperNodes`](/products/kubernetes-operator/guides/kubernetes-events#keeper-availability) warning event.

## Automatic schema sync {#automatic-schema-sync}

When `spec.settings.enableDatabaseSync` is `true` (the default), the operator keeps the schema aligned as the topology changes:

- **On scale up** — the operator replicates the database definitions of the replicas already serving client traffic to the newly created replicas, so a fresh replica joins with the same `Replicated` and integration databases as the rest of the cluster. Before any replica serves traffic, for example on cluster creation, the definitions of all reachable replicas are merged. Replicas that already serve traffic are never changed, so create databases with `ON CLUSTER` to have them on every replica.
While replicas that served traffic exist but none of them is reachable, new replicas wait.
- **On scale down** — once a replica is gone, the operator drops its registration from each `Replicated` database with `SYSTEM DROP DATABASE REPLICA`, so the shrunk cluster does not wait on a `Replicated` database replica that no longer exists.
- **On scale down** — once a replica is gone, the operator drops its registration from each `Replicated` database with `SYSTEM DROP DATABASE REPLICA`, so the shrunk cluster does not wait on a `Replicated` database replica that no longer exists. [Scaling to zero](#scaling-to-zero) keeps the registrations for the resume.

This covers `Replicated` databases and integration database engines. It does not move table data — row data lives in `ReplicatedMergeTree` tables and replicates through Keeper independently of this schema sync. With a single replica there is nothing to replicate.

Expand All @@ -75,12 +85,14 @@ kubectl get clickhousecluster sample -o yaml | sed -n '/conditions:/,/^[^ ]/p'
| `SchemaInSync` | `SchemaSyncDisabled` | `enableDatabaseSync` is `false` |
| `Ready` | `AllShardsReady` | Every shard has a ready replica |
| `Ready` | `SomeShardsNotReady` | At least one shard has no ready replica |
| `Ready` | `ScalingDown` | The cluster is scaled to zero, but replicas are still stopping |
| `Ready` | `ScaledToZero` | The cluster is scaled to zero and no replica Pods are left |

A scale operation is complete when `ClusterSizeAligned` reports `UpToDate`, `SchemaInSync` reports `ReplicasInSync`, and `Ready` reports `AllShardsReady`.
A scale operation is complete when `ClusterSizeAligned` reports `UpToDate`, `SchemaInSync` reports `ReplicasInSync`, and `Ready` reports `AllShardsReady`, or `ScaledToZero` after scaling to zero.

## Scaling Keeper {#scaling-keeper}

A `KeeperCluster` runs a RAFT quorum, so the operator changes its membership **one replica at a time** and only while the cluster is in a stable state. This protects the quorum: a `2F+1` cluster tolerates `F` members down, so a 3-node cluster keeps working with one member missing and a 5-node cluster with two.
A `KeeperCluster` runs a RAFT quorum, so the operator changes its membership **one replica at a time** and only while the cluster is in a stable state, except when [scaling to or from zero](#scaling-to-zero). This protects the quorum: a `2F+1` cluster tolerates `F` members down, so a 3-node cluster keeps working with one member missing and a 5-node cluster with two.

```yaml
spec:
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ClickHouseClusterSpec defines the desired state of ClickHouseCluster.

| Field | Type | Description | Required | Default |
|-------|------|-------------|----------|---------|
| `replicas` | integer | Number of replicas in the single shard. | false | 3 |
| `replicas` | integer | Number of replicas in the single shard.<br />Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept. | false | 3 |
| `shards` | integer | Number of shards in the cluster. | false | 1 |
| `keeperClusterRef` | [KeeperClusterReference](#keeperclusterreference) | Reference to the KeeperCluster that is used for ClickHouse coordination.<br />When namespace is omitted, the ClickHouseCluster namespace is used.<br />Mutually exclusive with `externalKeeper`, exactly one of them must be set. | false | |
| `externalKeeper` | [ExternalKeeperSpec](#externalkeeperspec) | Keeper ensemble that is not managed by this operator, used for ClickHouse coordination.<br />The operator does not own the lifecycle of these servers and only points ClickHouse at them.<br />Mutually exclusive with `keeperClusterRef`, exactly one of them must be set. | false | |
Expand Down Expand Up @@ -361,7 +361,7 @@ KeeperClusterSpec defines the desired state of KeeperCluster.

| Field | Type | Description | Required | Default |
|-------|------|-------------|----------|---------|
| `replicas` | integer | Number of replicas in the cluster | false | 3 |
| `replicas` | integer | Number of replicas in the cluster.<br />Set to 0 to stop the cluster: replicas are removed, their PersistentVolumeClaims are kept. | false | 3 |
| `podTemplate` | [PodTemplateSpec](#podtemplatespec) | Parameters passed to the Keeper pod spec. | false | |
| `containerTemplate` | [ContainerTemplateSpec](#containertemplatespec) | Parameters passed to the Keeper container spec. | false | |
| `dataVolumeClaimSpec` | [PersistentVolumeClaimSpec](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#persistentvolumeclaimspec-v1-core) | Specification of persistent storage for ClickHouse Keeper data. | false | |
Expand Down
Loading
Loading