为 TiDB 组件间通信开启加密传输

    • TiDB 与 TiKV、PD
    • TiKV 与 PD
    • TiDB Control 与 TiDB,TiKV Control 与 TiKV,PD Control 与 PD
    • TiKV、PD、TiDB 各自集群内内部通讯

    目前暂不支持只开启其中部分组件的加密传输。

    1. 准备证书。

      推荐为 TiDB、TiKV、PD 分别准备一个 Server 证书,并保证可以相互验证,而它们的 Control 工具则可选择共用 Client 证书。

      有多种工具可以生成自签名证书,如 ,easy-rsacfssl

      这里提供一个使用 openssl 生成证书的示例:。

    2. 配置证书。

      • TiDB

        config 文件或命令行参数中设置:

      • TiKV

        config 文件或命令行参数中设置,并设置相应的 URL 为 https:

        1. [security]
        2. # set the path for certificates. Empty string means disabling secure connectoins.
        3. ca-path = "/path/to/ca.pem"
        4. cert-path = "/path/to/tikv-server.pem"
        5. key-path = "/path/to/tikv-server-key.pem"
      • config 文件或命令行参数中设置,并设置相应的 URL 为 https:

        1. [security]
        2. cacert-path = "/path/to/ca.pem"
        3. # Path of file that contains X509 certificate in PEM format.
        4. cert-path = "/path/to/pd-server.pem"
        5. # Path of file that contains X509 key in PEM format.
        6. key-path = "/path/to/pd-server-key.pem"
      • TiFlash(从 v4.0.5 版本开始引入)

        在 文件中设置,将 http_port 项改为 https_port:

        1. [security]
        2. # Path of file that contains list of trusted SSL CAs. if set, following four settings shouldn't be empty
        3. ca_path = "/path/to/ca.pem"
        4. # Path of file that contains X509 certificate in PEM format.
        5. cert_path = "/path/to/tiflash-server.pem"
        6. # Path of file that contains X509 key in PEM format.
        7. key_path = "/path/to/tiflash-server-key.pem"

        tiflash-learner.toml 文件中设置,

        1. [security]
        2. # Sets the path for certificates. The empty string means that secure connections are disabled.
        3. ca-path = "/path/to/ca.pem"
        4. cert-path = "/path/to/tiflash-server.pem"
        5. key-path = "/path/to/tiflash-server-key.pem"
      • TiCDC

        在启动命令行中设置,并设置相应的 URL 为 https

        此时 TiDB 集群各个组件间已开启加密传输。

          1. ./pd-ctl -u https://127.0.0.1:2379 --cacert /path/to/ca.pem --cert /path/to/client.pem --key /path/to/client-key.pem

      通常被调用者除了校验调用者提供的密钥、证书和 CA 有效性外,还需要校验调用方身份以防止拥有有效证书的非法访问者进行访问(例如:TiKV 只能被 TiDB 访问,需阻止拥有合法证书但非 TiDB 的其他访问者访问 TiKV)。

      • TiDB

        config 文件或命令行参数中设置:

        1. [security]
        2. cluster-verify-cn = [
        3. "TiDB-Server",
        4. "TiKV-Control",
        5. ]
      • TiKV

        config 文件或命令行参数中设置:

      • PD

        config 文件或命令行参数中设置:

        1. [security]
        2. cert-allowed-cn = ["TiKV-Server", "TiDB-Server", "PD-Control"]
      • TiCDC

        在启动命令行中设置:

        1. cdc server --pd=https://127.0.0.1:2379 --log-file=ticdc.log --addr=0.0.0.0:8301 --advertise-addr=127.0.0.1:8301 --ca=/path/to/ca.pem --cert=/path/to/ticdc-cert.pem --key=/path/to/ticdc-key.pem --cert-allowed-cn="client1,client2"
      • TiFlash(从 v4.0.5 版本开始引入)

        tiflash.toml 文件中设置:

        1. [security]
        2. cert_allowed_cn = ["TiKV-Server", "TiDB-Server"]
        1. [security]

      TiDB、PD 和 TiKV 和各种 Client 都会在每次新建相互通讯的连接时重新读取当前的证书和密钥文件内容,实现证书和密钥的重加载。目前暂不支持 CA 的重加载。