

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 控制对亚马逊 MSK ZooKeeper 集群中 Apache 节点的访问权限
<a name="zookeeper-security"></a>

出于安全考虑，您可以限制对属于您的 Amazon MSK ZooKeeper 集群的 Apache 节点的访问权限。要限制对节点的访问，您可以为节点分配单独的安全组。然后，您可以决定有权访问该安全组的人员。

**重要**  
本节不适用于在 KRaft 模式下运行的集群。请参阅[KRaft 模式](metadata-management.md#kraft-intro)。

**Topics**
+ [将 Apache ZooKeeper 节点放在单独的安全组中](zookeeper-security-group.md)
+ [在 Apache 中使用 TLS 安全性 ZooKeeper](zookeeper-security-tls.md)

# 将 Apache ZooKeeper 节点放在单独的安全组中
<a name="zookeeper-security-group"></a>

要限制对 Apache ZooKeeper 节点的访问，您可以为其分配单独的安全组。您可以通过设置安全组规则来选择谁有权访问此新安全组。

1. 获取您的集群的 Apache ZooKeeper 连接字符串。要了解如何操作，请参阅[ZooKeeper 模式](metadata-management.md#msk-get-connection-string)。连接字符串包含您的 Apache ZooKeeper 节点的 DNS 名称。

1. 使用 `host` 或 `ping` 等工具将您在上一步中获得的 DNS 名称转换为 IP 地址。稍后您需要在此过程中使用这些 IP 地址，因此请保存这些地址。

1. 登录 AWS 管理控制台 并打开 Amazon EC2 控制台，网址为[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在左侧窗格的 **Network & Security (网络与安全性)** 下，选择 **Network Interfaces (网络接口)**。

1. 在网络接口表上方的搜索字段中，键入集群名称，然后键入 return。这会将表中显示的网络接口数限制为与您的集群关联的接口。

1. 选中与列表中的第一个网络接口对应的行开头处的复选框。

1. 在页面底部的详细信息窗格中，查找**主私 IPv4 有 IP**。如果此 IP 地址与您在本过程第一步中获得的 IP 地址相匹配，则表示该网络接口已分配给集群中的一个 Apache ZooKeeper 节点。否则，取消选中此网络接口旁边的复选框，然后选择列表中的下一个网络接口。选择网络接口的顺序无关紧要。在接下来的步骤中，您将对分配给 Apache ZooKeeper 节点的所有网络接口逐一执行相同的操作。

1. 当您选择与 Apache ZooKeeper 节点对应的网络接口时，请选择页面顶部的**操作**菜单，然后选择**更改安全组**。将新安全组分配给此网络接口。有关创建安全组的信息，请参阅 Amazon VPC 文档中的[创建安全组](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html?shortFooter=true#CreatingSecurityGroups)。

1. 重复上一步为与集群的 Apache ZooKeeper 节点关联的所有网络接口分配相同的新安全组。

1. 现在，您可以选择有权访问此新安全组的人员。有关设置安全组规则的信息，请参阅 Amazon VPC 文档中的[添加、删除和更新规则](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html?shortFooter=true#AddRemoveRules)。

# 在 Apache 中使用 TLS 安全性 ZooKeeper
<a name="zookeeper-security-tls"></a>

您可以使用 TLS 安全性在客户端和 Apache ZooKeeper 节点之间传输时进行加密。要在 Apache ZooKeeper 节点上实现 TLS 安全，请执行以下操作：
+ 集群必须使用 Apache Kafka 版本 2.5.1 或更高版本才能在 Apache 中使用 TLS 安全性。 ZooKeeper
+ 在创建或配置集群时启用 TLS 安全。使用 Apache Kafka 版本 2.5.1 或更高版本创建并启用 TLS 的集群会自动对 Apache 终端节点使用 TLS 安全性。 ZooKeeper 有关设置 TLS 安全的信息，请参阅[Amazon MSK 加密入门](msk-working-with-encryption.md)。
+ 使用[DescribeCluster ](https://docs.aws.amazon.com/msk/1.0/apireference/clusters-clusterarn.html#DescribeCluster)操作检索 TLS Apache ZooKeeper 端点。
+ 创建 Apache ZooKeeper 配置文件，以便与`kafka-configs.sh`和[https://kafka.apache.org/documentation/#security_authz_cli](https://kafka.apache.org/documentation/#security_authz_cli)工具或 ZooKeeper 外壳一起使用。对于每个工具，您都使用`--zk-tls-config-file`参数来指定 Apache ZooKeeper 配置。

  以下示例显示了一个典型的 Apache ZooKeeper 配置文件：

  ```
  zookeeper.ssl.client.enable=true
  zookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty
  zookeeper.ssl.keystore.location=kafka.jks
  zookeeper.ssl.keystore.password=test1234
  zookeeper.ssl.truststore.location=truststore.jks
  zookeeper.ssl.truststore.password=test1234
  ```
+ 对于其他命令（例如`kafka-topics`），必须使用`KAFKA_OPTS`环境变量来配置 Apache ZooKeeper 参数。以下示例说明如何配置`KAFKA_OPTS`环境变量以将 Apache ZooKeeper 参数传递给其他命令：

  ```
  export KAFKA_OPTS="
  -Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty 
  -Dzookeeper.client.secure=true 
  -Dzookeeper.ssl.trustStore.location=/home/ec2-user/kafka.client.truststore.jks
  -Dzookeeper.ssl.trustStore.password=changeit"
  ```

  配置 `KAFKA_OPTS` 环境变量后，您便可正常使用 CLI 命令。以下示例使用环境变量中的 Apache ZooKeeper 配置创建 Apache Kafka 主题：`KAFKA_OPTS`

  ```
  <path-to-your-kafka-installation>/bin/kafka-topics.sh --create --zookeeper ZooKeeperTLSConnectString --replication-factor 3 --partitions 1 --topic AWSKafkaTutorialTopic
  ```

**注意**  
您在 Apache ZooKeeper 配置文件中使用的参数名称与您在`KAFKA_OPTS`环境变量中使用的参数名称不一致。注意在配置文件和 `KAFKA_OPTS` 环境变量中与参数一起使用的名称。

有关使用 TLS 访问您的 Apache ZooKeeper 节点的更多信息，请参阅 [KIP-515：启用 ZK 客户端使用新的 TLS 支持的身份验证](https://cwiki.apache.org/confluence/display/KAFKA/KIP-515%3A+Enable+ZK+client+to+use+the+new+TLS+supported+authentication)。