> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-home-button.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 配置 ClickHouse 与 ZooKeeper 之间安全 SSL/TLS 通信的指南

# ClickHouse 与 ZooKeeper 之间的可选安全通信配置

<CloudNotSupportedBadge />

<Note>
  本页不适用于 [ClickHouse Cloud](https://clickhouse.com/cloud)。本文所述流程在 ClickHouse Cloud 服务中已实现自动化。
</Note>

你应为通过 SSL 与 ClickHouse client 通信配置 `ssl.keyStore.location`、`ssl.keyStore.password` 以及 `ssl.trustStore.location`、`ssl.trustStore.password`。这些选项从 Zookeeper 3.5.2 版本开始提供。

你可以将 `zookeeper.crt` 添加到受信任的证书中。

```bash theme={null}
sudo cp zookeeper.crt /usr/local/share/ca-certificates/zookeeper.crt
sudo update-ca-certificates
```

`config.xml` 中的客户端部分如下所示：

```xml theme={null}
<client>
    <certificateFile>/etc/clickhouse-server/client.crt</certificateFile>
    <privateKeyFile>/etc/clickhouse-server/client.key</privateKeyFile>
    <loadDefaultCAFile>true</loadDefaultCAFile>
    <cacheSessions>true</cacheSessions>
    <disableProtocols>sslv2,sslv3</disableProtocols>
    <preferServerCiphers>true</preferServerCiphers>
    <invalidCertificateHandler>
        <name>RejectCertificateHandler</name>
    </invalidCertificateHandler>
</client>
```

将 ZooKeeper 添加到 ClickHouse 配置中，并添加一些集群和宏配置：

```xml theme={null}
<clickhouse>
    <zookeeper>
        <node>
            <host>localhost</host>
            <port>2281</port>
            <secure>1</secure>
        </node>
    </zookeeper>
</clickhouse>
```

启动 `clickhouse-server`。你应该会在日志中看到：

```text theme={null}
<Trace> ZooKeeper: initialized, hosts: secure://localhost:2281
```

前缀 `secure://` 表示该连接使用 SSL 加密。

要确认流量已加密，请在安全端口上运行 `tcpdump`：

```bash theme={null}
tcpdump -i any dst port 2281 -nnXS
```

并在 `clickhouse-client` 中执行查询：

```sql theme={null}
SELECT * FROM system.zookeeper WHERE path = '/';
```

对于未加密连接，你会在 `tcpdump` 输出中看到类似下面的内容：

```text theme={null}
..../zookeeper/quota.
```

在加密连接中，你不应该看到这个。
