以下过程描述如何从 Kubernetes 集群外部连接到 Kubernetes 中部署的 MongoDB 资源。
先决条件
兼容的 MongoDB 版本
为了支持在 Kubernetes 外部访问您的数据库,它们必须运行 MongoDB 4.2.3 或更高版本。
Considerations
配置就绪探针覆盖
如果您创建的自定义服务需要从外部访问 Kubernetes Operator 部署的 MongoDB 自定义资源并在 Kubernetes 中使用就绪探针(readiness probe),请将 Kubernetes 中的 publishNotReadyAddresses 设置为 true。
The publishNotReadyAddresses setting indicates that an agent that interacts with endpoints for this service should disregard the service's ready state. Setting publishNotReadyAddresses to true overrides the behavior of the readiness probe configured for the Pod hosting your service.
默认情况下,publishNotReadyAddresses 设置为false。在这种情况下,当 Kubernetes Operator 中托管 MongoDB 自定义资源的 Pod 失去与 Cloud Manager 或 Ops Manager 的连接时,为这些 Pod 配置的就绪探针将失败。但是,如果将 publishNotReadyAddresses 设置为 true:
Kubernetes 不会关闭就绪探针失效的服务。
Kubernetes 将所有终结点都视为准备就绪,即使托管这些终结点服务的 Pod 的探测表明它们尚未准备就绪。
MongoDB 自定义资源仍可用于读写操作。
提示
Kubernetes API Reference and search for
publishNotReadyAddresses
步骤
以下操作步骤将引导您使用 Kubernetes Operator 中的内置配置选项来为部署配置外部连接。
如何连接 Kubernetes Operator 从 Kubernetes 集群外部部署的 MongoDB 资源取决于此资源。
要从 Kubernetes 集群外部连接到部署 Kubernetes Operator 的 MongoDB 独立资源:
使用 Kubernetes Operator 部署独立资源。
如果尚未部署独立资源,请按照说明部署一个独立资源。
此操作步骤使用以下示例:
20 21 apiVersion: mongodb.com/v1 22 kind: MongoDB 23 metadata: 24 name: <my-standalone> 25 spec: 26 version: "8.0.0" 27 opsManager: 28 configMapRef: 29 name: <configMap.metadata.name> 30 # Must match metadata.name in ConfigMap file 31 credentials: <mycredentials> 32 type: Standalone 33 ...
为 MongoDB Pod 创建一个外部服务。
要从外部资源连接到独立资源,请配置 spec.externalAccess设置:
externalAccess: {}
此设置指示 Kubernetes Operator 为独立资源中的 MongoDB Pod 创建外部 LoadBalancer 服务。外部服务为外部连接提供入口点。添加此不带值的设置后,外部服务的默认值如下:
字段 | 值 | 说明 |
|---|---|---|
|
| 外部服务的名称。您无法更改此值。 |
|
| 创建外部 LoadBalancer 服务。 |
|
| A port for |
|
| Specifies that DNS records are created even if the Pod isn't ready. Do not set to |
(可选)如果您需要向服务添加值或覆盖默认值,请指定:
特定于您的云提供商的注释,位于
spec.externalAccess.externalService.annotations在
spec.externalAccess.externalService.spec中覆盖服务规范。
例如,以下设置会覆盖外部服务的默认值,以配置独立资源,从而创建公开 MongoDB Pod 的 NodePort 服务:
externalAccess: externalService: annotations: # cloud-specific annotations for the service spec: type: NodePort # default is LoadBalancer port: 27017 # you can specify other spec overrides if necessary
提示
要了解更多信息,请参阅 Kubernetes 文档中的注释和 ServiceSpec。
验证外部服务。
在独立资源中,运行以下命令以验证 Kubernetes Operator 是否为您的部署创建了外部服务。
kubectl get services
该命令返回类似于以下输出的服务列表。对于集群中的每个数据库 Pod,Kubernetes Operator 创建一个名为 <pod-name>-0-svc-external 的外部服务。此服务是根据您在外部服务规范中提供的值和覆盖内容进行配置的。
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE <my-standalone>-0-svc-external LoadBalancer 10.102.27.116 <lb-ip-or-fqdn> 27017:27017/TCP 8m30s
根据您的集群配置或云提供商,LoadBalancer 服务的 IP 地址是外部可访问的 IP 地址或 FQDN。您可以使用 IP 地址或 FQDN 路由来自外部域的流量。
测试与独立资源的连接。
要从 Kubernetes 集群外部连接到部署,请使用 MongoDB Shell (mongosh) 并指定您通过外部域公开的 MongoDB Pod 地址。
例子
If you have an external FQDN of <my-standalone>.<external-domain>, you can connect to this sharded cluster instance from outside of the Kubernetes cluster by using the following command:
mongosh "mongodb://<my-standalone>.<external-domain>"
重要
此过程解释了启用外部连接的最简单方法。您可以在生产中使用其他实用程序。
要从 Kubernetes 集群外部连接到部署 Kubernetes Operator 的 MongoDB 副本集资源:
使用Kubernetes Operator 部署副本集。
如果您尚未部署副本集,请按照说明部署副本集。
You must enable TLS for the replica set by providing a value for the spec.security.certsSecretPrefix setting. The replica set must use a custom CA certificate stored with spec.security.tls.ca.
为 MongoDB Pod 创建外部服务。
要从外部资源连接到副本集,请配置spec.externalAccess 设置:
externalAccess: {}
此设置指示Kubernetes Operator 创建外部 LoadBalancer 副本集MongoDB Pod 的 MongoDB 服务。外部服务为外部连接提供入口点。 添加此不带值的设置会创建具有以下默认值的外部服务:
字段 | 值 | 说明 |
|---|---|---|
|
| 外部服务的名称。您无法更改此值。 |
|
| 创建外部 LoadBalancer 服务。 |
|
| A port for |
|
| Specifies that DNS records are created even if the Pod isn't ready. Do not set to |
(可选)如果您需要向服务添加值或覆盖默认值,请指定:
特定于您的云提供商的注释,位于
spec.externalAccess.externalService.annotations在
spec.externalAccess.externalService.spec中覆盖服务规范。
例如,以下设置会覆盖外部服务的默认值,以配置副本集以创建公开MongoDB Pod 的NodePort 服务:
externalAccess: externalService: annotations: # cloud-specific annotations for the service spec: type: NodePort # default is LoadBalancer port: 27017 # you can specify other spec overrides if necessary
提示
要了解更多信息,请参阅 Kubernetes 文档中的注释和 ServiceSpec。
验证外部服务。
在副本集,运行以下命令以验证Kubernetes Operator 是否为您的部署创建了外部服务。
kubectl get services
该命令将返回类似于以下输出的服务列表。 对于集群中的每个数据库Pod, Kubernetes Operator 都会创建一个名为 <pod-name>-<pod-idx>-svc-external 的外部服务。 此服务是根据您在外部服务规范中提供的值和覆盖项进行配置的。
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE <my-replica-set>-0-svc-external LoadBalancer 10.102.27.116 <lb-ip-or-fqdn> 27017:27017/TCP 8m30s
根据您的集群配置或云提供商,LoadBalancer 服务的 IP 地址是外部可访问的 IP 地址或 FQDN。您可以使用 IP 地址或 FQDN 路由来自外部域的流量。
Copy the sample replica set resource.
更改此 YAML 文件的设立,以匹配所需的副本集配置。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-replica-set> 6 spec: 7 members: 3 8 version: "8.0.0" 9 type: ReplicaSet 10 opsManager: 11 configMapRef: 12 name: <configMap.metadata.name> 13 credentials: <mycredentials> 14 persistent: true
15 security: 16 tls: 17 enabled: true 18 connectivity: 19 replicaSetHorizons: 20 - "example-website": "web1.example.com:30907" 21 - "example-website": "web2.example.com:32350" 22 - "example-website": "web3.example.com:31185" 23 ...
将突出显示的设置更改为您的首选值。
键 | 类型 | 必要性 | 说明 | 例子 |
|---|---|---|---|---|
| 集合 | 可选的 | 如果需要在 Kubernetes 外部访问数据库,请添加此参数和值。此设置允许您为 Kubernetes 集群提供与在 Kubernetes 集群中不同的 DNS 设置。Kubernetes Operator 为副本集节点使用水平分割 DNS。此功能允许 Kubernetes 集群内部与 Kubernetes 外部进行通信。 您可以为每台主机添加多个外部映射。 水平分割要求
| |
| 字符串 | 必需 | 添加 Secret 名称的 |
|
确认副本集资源中的外部主机名和外部服务值。
确认 spec.connectivity.replicaSetHorizons 设置中的外部主机名正确无误。
外部主机名应与 Kubernetes 工作节点的 DNS 名称相匹配。它们可以是 Kubernetes 集群中的任何节点。如果 pod 在另一个节点上运行,则 Kubernetes 节点使用内部路由。
将spec.connectivity.replicaSetHorizons中的端口设置为外部服务值。
例子
15 security: 16 tls: 17 enabled: true 18 connectivity: 19 replicaSetHorizons: 20 - "example-website": "web1.example.com:30907" 21 - "example-website": "web2.example.com:32350" 22 - "example-website": "web3.example.com:31185" 23 ...
Save your replica set config file.
Update and restart your replica set deployment.
在任意目录中,调用以下Kubernetes命令来更新并重启副本集:
kubectl apply -f <replica-set-conf>.yaml
测试与副本集的连接。
在开发环境中,对于副本集中的每个主机,运行以下命令:
mongosh --host <my-replica-set>/web1.example.com \ --port 30907 --ssl \ --sslAllowInvalidCertificates
注意
请勿在生产中使用--sslAllowInvalidCertificates标志。
在生产中,为副本集中的每个主机指定TLS证书和CA ,以安全地连接到客户端工具或应用程序:
mongosh --host <my-replica-set>/web1.example.com \ --port 30907 \ --tls \ --tlsCertificateKeyFile server.pem \ --tlsCAFile ca-pem
如果连接成功,您应该会看到:
Enterprise <my-replica-set> [primary]
要从Kubernetes集群外部连接到Kubernetes Operator 部署的MongoDB分片分片集群资源,请执行以下操作:
使用Kubernetes Operator 部署分片分片集群。
如果您尚未部署分分片集群,请按照说明部署一个。
您必须通过配置以下设置为分片集群启用 TLS:
键 | 类型 | 必要性 | 说明 | 例子 |
|---|---|---|---|---|
| 字符串 | 必需 | 添加 Secret 名称的 |
|
| 集合 | Optional | List of every domain that should be added to TLS certificates to each pod in this deployment. When you set this parameter, every CSR that the Kubernetes Operator transforms into a TLS certificate includes a SAN in the form |
|
为 mongos Pod 创建外部服务。
要从外部资源连接到分片分片集群,请配置spec.externalAccess 设置:
externalAccess: {}
This setting instructs the Kubernetes Operator to create an external LoadBalancer service for the mongos Pods in your sharded cluster. The external service provides an entry point for external connections. Adding this setting with no values creates an external service with the following default values:
字段 | 值 | 说明 |
|---|---|---|
|
| 外部服务的名称。您无法更改此值。 |
|
| 创建外部 LoadBalancer 服务。 |
|
| A port for |
|
| Specifies that DNS records are created even if the Pod isn't ready. Do not set to |
(可选)如果您需要向服务添加值或覆盖默认值,请指定:
特定于您的云提供商的注释,位于
spec.externalAccess.externalService.annotations在
spec.externalAccess.externalService.spec中覆盖服务规范。
For example, the following settings override the default values for the external service to configure your sharded cluster to create NodePort services that expose the mongos Pods:
externalAccess: externalService: annotations: # cloud-specific annotations for the service spec: type: NodePort # default is LoadBalancer port: 27017 # you can specify other spec overrides if necessary
提示
要了解更多信息,请参阅 Kubernetes 文档中的注释和 ServiceSpec。
将主题备用名称添加到 TLS 证书。
将每个外部DNS名称添加到证书SAN 。
每个 MongoDB 主机使用以下 SAN:
<my-sharded-cluster>-<shard>-<pod-index>.<external-domain> <my-sharded-cluster>-config-<pod-index>.<external-domain> <my-sharded-cluster>-mongos-<pod-index>.<external-domain>
The mongos instance uses the following SAN:
<my-sharded-cluster>-mongos-<pod-index>-svc-external.<external-domain>
Configure the spec.security.tls.additionalCertificateDomains setting similar to the following example. Each TLS certificate that you use must include the corresponding SAN for the shard, config server, or mongos instance. The Kubernetes Operator validates your configuration.
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-sharded-cluster> 6 spec: 7 version: "8.0.0" 8 opsManager: 9 configMapRef: 10 name: <configMap.metadata.name> 11 # Must match metadata.name in ConfigMap file 12 shardCount: 2 13 mongodsPerShardCount: 3 14 mongosCount: 2 15 configServerCount: 3 16 credentials: my-secret 17 type: ShardedCluster 18 externalAccess: {} 19 security: 20 tls: 21 certsSecretPrefix: <prefix> 22 additionalCertificateDomains: 23 - "<external-domain>" 24 ...
验证外部服务。
在分分片集群中,运行以下命令,验证Kubernetes Operator 是否为您的部署创建了外部服务。
kubectl get services
该命令将返回类似于以下输出的服务列表。 对于集群中的每个mongos实例, Kubernetes Operator 都会创建一个名为<pod-name>-<pod-idx>-svc-external的外部服务。 此服务是根据您在外部服务规范中提供的值和覆盖项进行配置的。
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE <my-sharded-cluster>-mongos-0-svc-external LoadBalancer 10.102.27.116 <lb-ip-or-fqdn> 27017:27017/TCP 8m30s <my-sharded-cluster>-mongos-1-svc-external LoadBalancer 10.102.27.116 <lb-ip-or-fqdn> 27017:27017/TCP 8m30s
Depending on your cluster configuration or cloud provider, the IP address of the LoadBalancer service is an externally accessible IP address or FQDN. You can use the IP address or FQDN to route traffic from your external domain. This example has two mongos instances, therefore the Kubernetes Operator creates two external services.
测试与分分片集群的连接。
要从Kubernetes集群外部连接到您的部署,请使用MongoDB Shell ( mongosh ) 并指定您通过外部域公开的mongos实例的地址。
例子
如果您的外部 FQDN 为 <my-sharded-cluster>-mongos-0-svc-external.<external-domain> 和 <my-sharded-cluster>-mongos-1-svc-external.<external-domain> 地址命令:mongodb://<my-sharded-cluster> - mongos-0-svc-external.,<external-domain> <my-sharded-cluster>- mongos -1-svc-external.<external-domain> ,可以使用以下命令从Kubernetes集群外部连接到此分片集群实例:
mongosh ""