Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/
Enterprise Kubernetes Operator
/ /

从外部 Kubernetes 连接 MongoDB 数据库资源

以下过程描述如何从 Kubernetes 集群外部连接到 Kubernetes 中部署的 MongoDB 资源。

为了支持在 Kubernetes 外部访问您的数据库,它们必须运行 MongoDB 4.2.3 或更高版本。

如果您创建的自定义服务需要从外部访问 Kubernetes Operator 部署的 MongoDB 自定义资源并在 Kubernetes 中使用就绪探针,请将 Kubernetes 中的 publishNotReadyAddresses设置为true

publishNotReadyAddresses设置表示与此服务的端点交互的代理应忽略该服务的就绪状态。将publishNotReadyAddresses 设置为true 会覆盖为托管服务的 Pod 配置的就绪探针的行为。

默认情况下,publishNotReadyAddresses 设置为false。在这种情况下,当 Kubernetes Operator 中托管 MongoDB 自定义资源的 Pod 失去与 Cloud Manager 或 Ops Manager 的连接时,为这些 Pod 配置的就绪探针将失败。但是,如果将 publishNotReadyAddresses 设置为 true

  • Kubernetes 不会关闭就绪探针失效的服务。

  • Kubernetes 将所有终结点都视为准备就绪,即使托管这些终结点服务的 Pod 的探测表明它们尚未准备就绪。

  • MongoDB 自定义资源仍可用于读写操作。

提示

以下操作步骤将引导您使用 Kubernetes Operator 中的内置配置选项来为部署配置外部连接。

如何连接 Kubernetes Operator 从 Kubernetes 集群外部部署的 MongoDB 资源取决于此资源。

要从 Kubernetes 集群外部连接到部署 Kubernetes Operator 的 MongoDB 独立资源:

1

如果尚未部署独立资源,请按照说明部署一个独立资源

此操作步骤使用以下示例:

20---
21apiVersion: mongodb.com/v1
22kind: MongoDB
23metadata:
24 name: <my-standalone>
25spec:
26 version: "4.2.2-ent"
27 opsManager:
28 configMapRef:
29 name: <configMap.metadata.name>
30 # Must match metadata.name in ConfigMap file
31 credentials: <mycredentials>
32 type: Standalone
33...
2

要从外部资源连接到独立资源,请配置 spec.externalAccess设置:

externalAccess: {}

此设置指示 Kubernetes Operator 为独立资源中的 MongoDB Pod 创建外部 LoadBalancer 服务。外部服务为外部连接提供入口点。添加此不带值的设置后,外部服务的默认值如下:

字段
说明

Name

<pod-name>-svc-external

外部服务的名称。您无法更改此值。

Type

LoadBalancer

创建外部 LoadBalancer 服务。

Port

<Port Number>

mongod的端口。

publishNotReadyAddress

true

指定即使 Pod 尚未准备就绪,也会创建 DNS 记录。对于任何数据库Pod,请勿设立为false

(可选)如果您需要向服务添加值或覆盖默认值,请指定:

例如,以下设置会覆盖外部服务的默认值,以配置独立资源,从而创建公开 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

3

在独立资源中,运行以下命令以验证 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路由来自外部域的流量。

4

要从 Kubernetes 集群外部连接到部署,请使用 MongoDB Shell (mongosh) 并指定您通过外部域公开的 MongoDB Pod 地址。

例子

如果外部FQDN<my-standalone>.<external-domain> ,则可以使用以下命令从Kubernetes集群外部连接到此分片分片集群实例:

mongosh "mongodb://<my-standalone>.<external-domain>"

重要

此过程解释了启用外部连接的最简单方法。您可以在生产中使用其他实用程序。

要从 Kubernetes 集群外部连接到部署 Kubernetes Operator 的 MongoDB 副本集资源:

1

如果您尚未部署副本集,请按照说明部署副本集

您必须通过为 设置提供值来为副本集启用spec.security.certsSecretPrefix TLS 。副本集必须使用通过 存储的自定义 CA spec.security.tls.ca证书。

2

要从外部资源连接到副本集,请配置spec.externalAccess 设置:

externalAccess: {}

此设置指示Kubernetes Operator 为副本集的MongoDB Pod 创建外部 LoadBalancer 服务。外部服务为外部连接提供入口点。添加此不带值的设置会创建具有以下默认值的外部服务:

字段
说明

Name

<pod-name>-svc-external

外部服务的名称。您无法更改此值。

Type

LoadBalancer

创建外部 LoadBalancer 服务。

Port

<Port Number>

mongod的端口。

publishNotReadyAddress

true

指定即使 Pod 尚未准备就绪,也会创建 DNS 记录。对于任何数据库Pod,请勿设立为false

(可选)如果您需要向服务添加值或覆盖默认值,请指定:

示例,以下设置会覆盖外部服务的默认值,以配置副本集以创建公开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

3

将每个外部DNS名称添加到证书SAN

4

在副本集,运行以下命令以验证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 路由来自外部域的流量。

5
6

更改此 YAML 文件的设立,以匹配所需的副本集配置。

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: <my-replica-set>
6spec:
7 members: 3
8 version: "4.2.2-ent"
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...
7

打开您首选的文本编辑器,将对象规范粘贴到资源文件末尾的spec 部分。

8
类型
必要性
说明
例子
spec.connectivity

集合

可选的

如果需要在 Kubernetes 外部访问数据库,请添加此参数和值。此设置允许您为 Kubernetes 集群提供与在 Kubernetes 集群中不同的 DNS 设置。Kubernetes Operator 为副本集节点使用水平分割 DNS。此功能允许 Kubernetes 集群内部与 Kubernetes 外部进行通信。

您可以为每台主机添加多个外部映射。

水平分割要求

spec.security

字符串

必需

添加 Secret 名称的 <prefix>,其需包含您 MongoDB 部署的 TLS 证书。

devDb

9

确认 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...
10
11

在任意目录中,调用以下Kubernetes命令来更新并重启副本集:

kubectl apply -f <replica-set-conf>.yaml
12

在开发环境中,对于副本集中的每个主机,运行以下命令:

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分片分片集群资源,请执行以下操作:

1

如果您尚未部署分分片集群,请按照说明部署一个。

您必须通过配置以下设置为分分片集群启用TLS

类型
必要性
说明
例子
spec.security

字符串

必需

添加 Secret 名称的 <prefix>,其需包含您 MongoDB 部署的 TLS 证书。

devDb

集合

Optional

应添加到该部署中的每个 Pod 的 TLS 证书的每个域的列表。在您设置该参数时,Kubernetes Operator 转换为 TLS 证书的每个 CSR 都包含一个 <pod name>.<additional cert domain> 格式的 SAN

example.com

2

要从外部资源连接到分片分片集群,请配置spec.externalAccess 设置:

externalAccess: {}

此设置指示Kubernetes Operator 为分片集群中的 mongosPod 创建外部 LoadBalancer 服务。外部服务为外部连接提供入口点。添加此不带值的设置会创建具有以下默认值的外部服务:

字段
说明

Name

<pod-name>-svc-external

外部服务的名称。您无法更改此值。

Type

LoadBalancer

创建外部 LoadBalancer 服务。

Port

<Port Number>

mongod的端口。

publishNotReadyAddress

true

指定即使 Pod 尚未准备就绪,也会创建 DNS 记录。对于任何数据库Pod,请勿设立为false

(可选)如果您需要向服务添加值或覆盖默认值,请指定:

示例,以下设置会覆盖用于配置分片集群的外部服务的默认值,从而创建公开 mongosPod 的 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

3

将每个外部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>

mongos实例使用以下SAN

<my-sharded-cluster>-mongos-<pod-index>-svc-external.<external-domain>

配置类似于以下示例的spec.security.tls.additionalCertificateDomains设置。 您使用的每个TLS证书都必须包含分分片、配置服务器或mongos实例相应的SAN 。 Kubernetes Operator 会验证您的配置。

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: <my-sharded-cluster>
6spec:
7 version: "4.2.2-ent"
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...
4

在分分片集群中,运行以下命令,验证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

根据您的集群配置或云提供商,LoadBalancer 服务的IP解决是外部可访问的IP解决或FQDN 。 您可以使用IP解决或FQDN路由来自外部域的流量。 此示例有两个mongos实例,因此Kubernetes Operator 创建两个外部服务。

5

要从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 ""

后退

Inside Kubernetes

在此页面上