This tutorial shows you how to configure TLS on a self-managed replica set. Select an approach based on whether you want to use intra-cluster mTLS, which is required to enable X.509 authentication between nodes.
重要
These steps apply to self-managed MongoDB deployments. MongoDB Atlas clusters use TLS by default. If you use Cloud Manager or Ops Manager, configure TLS through your deployment management tool.
开始之前
开始之前,请确认您具有以下内容:
步骤
如果您不需要服务器节点之间的集群内 mTLS 或 X.509 身份验证,请遵循此程序。
编辑每个节点上的配置文件
在每个节点上,找到并打开 mongod 配置文件。如果该文件不存在,则创建它。添加以下 TLS 选项。使用证书文件的绝对路径。
示例,主节点 (primary node in the replica set)的配置文件如下所示:
net: tls: mode: allowTLS certificateKeyFile: /etc/ssl/mongodb/mongo0.pem CAFile: /etc/ssl/mongodb/ca.pem allowConnectionsWithoutCertificates: true security: clusterAuthMode: keyFile keyFile: /etc/ssl/mongodb/keyfile setParameter: tlsWithholdClientCertificate: true
mode设置为allowTLS接受 TLS 连接,但不要求连接,因此节点可以完成滚动重启而不中断复制。certificateKeyFile指定节点的证书。将文件名替换为所配置的特定节点的证书密钥文件。CAFile指定签署节点证书的 CA 证书。allowConnectionsWithoutCertificates允许客户端在不提供 TLS 证书的情况下连接。将
clusterAuthMode设置为keyFile,使节点能够使用密钥文件进行身份验证。keyFile指定节点用于相互身份验证的密钥文件。tlsWithholdClientCertificate可防止节点在向集群中的其他节点建立出站连接时呈现其服务器证书。这是必要的,因为它可防止节点使用服务器身份验证证书进行客户端身份验证,尤其是当证书不包含clientAuthEKU 时。
按照此过程配置副本集,以使其在节点之间使用集群内 mTLS 和 X.509 身份验证。
编辑每个节点上的配置文件
重要
Your node certificates must include both serverAuth and clientAuth EKU for intra-cluster mTLS. If your certificates do not include clientAuth, see the Public Certificate Authority Policy Changes Affecting mTLS Technical Advisory for alternative configuration options.
在每个节点上,找到并打开 mongod 配置文件。如果该文件不存在,则创建它。添加以下 TLS 选项。使用证书文件的绝对路径。
示例,主节点 (primary node in the replica set)的配置文件如下所示:
net: tls: mode: allowTLS certificateKeyFile: /etc/ssl/mongodb/mongo0.pem CAFile: /etc/ssl/mongodb/ca.pem allowConnectionsWithoutCertificates: true clusterAuthX509: attributes: O=MongoDB security: clusterAuthMode: x509
mode设置为allowTLS接受 TLS 连接,但不要求连接,因此节点可以完成滚动重启而不中断复制。certificateKeyFile指定节点的证书。将文件名替换为所配置的特定节点的证书密钥文件。CAFile指定签署节点证书的 CA 证书。allowConnectionsWithoutCertificates允许客户端在不提供 TLS 证书的情况下连接。net.tls.clusterAuthX509.attributes指定将节点识别为集群节点而非普通客户端的 X.509 证书属性。有关所需证书属性,请参阅节点 X.509 证书。将
clusterAuthMode设置为x509可使节点使用 X.509 证书进行身份验证。
检查日志以验证 TLS 配置
滚动重启完成后,检查每个节点上的日志,以验证 TLS 配置正确。您的日志必须包括以下内容,以验证 mTLS 和 X.509 身份验证配置正确:
"msg":"Successfully authenticated", "attr": { "isClusterMember":true, "mechanism":"MONGODB-X509", "user":"CN=mongo1.example.com,O=MongoDB,L=New York City,ST=New-York,C=US" }
您应该会看到副本集中其他两个节点的类似日志消息。示例,如果您查看 mongo0 上的日志,应该会看到与 mongo1 和 mongo2 相关的类似消息。
后续步骤
要了解如何使用客户端应用程序连接到部署,请继续阅读连接到启用 TLS 的副本集。