AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

Kubernetes の外部から MongoDB Database リソースへの接続

次の手順では、Kubernetes クラスターの外部から Kubernetes に配置された MongoDB リソースに接続する方法について説明します。

Kubernetes 外からデータベースにアクセスするには、MongoDB 4.2.3 以降を実行する必要があります。

Kubernetes Operator によって配置された MongoDB カスタム リソースへの外部アクセスを必要とするカスタム サービスを作成し、Kubernetes で準備状況検証を使用する場合は、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に設定されています。 この場合、MongoDB KubernetesOperator で カスタム リソースをホストするポッドがCloud Manager またはMongoDB Ops Manager への接続を失うと、これらのポッドに構成された準備状況は失敗します。ただし、 publishNotReadyAddressesの設定をtrueに設定すると、次の効果が生じます。

  • Kubernetes は、準備状況調査に失敗したサービスをシャットダウンしません。

  • Kubernetes はすべてのエンドポイントを 準備完了 と見なします これらのエンドポイントのサービスをホストしているポッドの検証が、準備ができていないことを示していても。

  • 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: "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...
2

外部リソースからスタンドアロン リソースに接続するには、 spec.externalAccessを構成します 設定:

externalAccess: {}

この設定は、Kubernetes Operator に外部 LoadBalancer を作成するように指示します スタンドアロン リソース内の MongoDB ポッドのサービス。外部サービスは、外部接続のエントリポイントを提供します。 値なしでこの設定を追加すると、次のデフォルト値を持つ外部サービスが作成されます。

フィールド
説明

Name

<pod-name>-svc-external

外部サービスの名前。 この値は変更できません。

Type

LoadBalancer

Port

<Port Number>

A port for mongod.

publishNotReadyAddress

true

Specifies that DNS records are created even if the Pod isn't ready. Do not set to false for any database Pod.

オプションとして、サービスに値を追加したり、デフォルト値を上書きしたりする必要がある場合は、次を指定します。

たとえば、次の設定は外部サービスのデフォルト値を上書きし、スタンドアロン リソースを構成して NodePort サービス を作成します。 MongoDB ポッドを公開する:

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

Tip

詳細については、 Kubernetesドキュメントの 注釈ServiceSpec を参照してください。

3

スタンドアロン リソースで次のコマンドを実行して、Kubernetes Operator が配置用の外部サービスを作成したことを確認します。

$ kubectl get services

このコマンドは、次の出力のようなサービスのリストを返します。 Kubernetes Operator は、クラスター内の各データベース<pod-name> ポッドに対して、"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 ポッド アドレスを指定します。

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 レプリカセット リソースに接続するには、次の手順に従います。

1

レプリカセットを配置していない場合は、手順に従ってレプリカセットを配置します。

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.

2

外部リソースからレプリカセットに接続するには、 spec.externalAccessを構成します 設定:

externalAccess: {}

この設定は、 Kubernetes Operator に対して、 レプリカセット内のMongoDBポッド用の外部 LoadBalancer サービスを作成するように指示します。外部サービスは、外部接続のエントリ点を提供します。値なしでこの設定を追加すると、次のデフォルト値を持つ外部サービスが作成されます。

フィールド
説明

Name

<pod-name>-svc-external

外部サービスの名前。 この値は変更できません。

Type

LoadBalancer

Port

<Port Number>

A port for mongod.

publishNotReadyAddress

true

Specifies that DNS records are created even if the Pod isn't ready. Do not set to false for any database Pod.

オプションとして、サービスに値を追加したり、デフォルト値を上書きしたりする必要がある場合は、次を指定します。

例、次の設定は外部サービスのデフォルト値を上書きし、 MongoDBポッドを公開する 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

Tip

詳細については、 Kubernetesドキュメントの 注釈ServiceSpec を参照してください。

3

各外部DNS名を証明書SANに追加します。

4

レプリカセットで次のコマンドを実行して、Kubernetes Operator が配置用の外部サービスを作成したことを確認します。

$ kubectl get services

このコマンドは、次の出力のようなサービスのリストを返します。 Kubernetes Operator は、クラスター内の各データベース<pod-name> <pod-idx>ポッドに対して、[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: "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...
7

Open your preferred text editor and paste the object specification at the end of your resource file in the spec section.

8
キー
タイプ
必要性
説明

spec.connectivity
.replicaSetHorizons

コレクション

条件付き

Kubernetes 外でデータベースにアクセスする必要がある場合は、このパラメータと値を追加します。 この設定により、Kubernetes クラスター内と Kubernetes クラスターに対して異なるDNS設定を提供できます。 Kubernetes Operator は、レプリカセット ノードに スプリット ホライゾンDNSを使用します。 この機能により、Kubernetes クラスター内と Kubernetes 外部からの両方で通信が可能になります。

ホストごとに複数の外部マッピングを追加できます。

スプリットホライゾンの要件

  • この配列内の各値が一意であることを確認してください。

  • TLSspec.security.certsSecretPrefix を有効にするには、 設定の値を指定します。このメソッドでスプリットホライズンを使用するには、 TLSプロトコルの サーバー名表示 拡張機能が必要です。

spec.security
certsSecretPrefix

string

必須

MongoDB 配置のTLS証明書を含むシークレット名の<prefix>を追加します。

devDb

9

spec.connectivity.replicaSetHorizons設定の外部ホスト名が正しいことを確認します。

外部ホスト名は、Kubernetes ワーカー ノードのDNS名と一致する必要があります。 これらは、Kubernetes クラスター内の任意のノードになります。 ポッドが別のノードで実行される場合、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
.certsSecretPrefix

string

必須

MongoDB 配置のTLS証明書を含むシークレット名の<prefix>を追加します。

devDb

コレクション

任意

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 <pod name>.<additional cert domain>.

example.com

2

外部リソースからシャーディングされたクラスターに接続するには、 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:

フィールド
説明

Name

<pod-name>-svc-external

外部サービスの名前。 この値は変更できません。

Type

LoadBalancer

Port

<Port Number>

A port for mongod.

publishNotReadyAddress

true

Specifies that DNS records are created even if the Pod isn't ready. Do not set to false for any database Pod.

オプションとして、サービスに値を追加したり、デフォルト値を上書きしたりする必要がある場合は、次を指定します。

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

Tip

詳細については、 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>

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

シャーディングされたクラスターで次のコマンドを実行して、Kubernetes Operator が配置用の外部サービスを作成したことを確認します。

$ kubectl get services

このコマンドは、次の出力のようなサービスのリストを返します。 Kubernetes Operator は、クラスター内の各mongosインスタンスに対して、 <pod-name>-<pod-idx>-svc-externalという名前の外部サービスを作成します。 This service is configured according to the values and overrides you provide in the external service specification.

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.

5

Kubernetes クラスターの外部から配置に接続するには、MongoDB Shell( mongosh )を使用し、外部ドメインを通じて公開したmongosインスタンスのアドレスを指定します。

If you have external FQDN of <my-sharded-cluster>-mongos-0-svc-external.<external-domain> and <my-sharded-cluster>-mongos-1-svc-external.<external-domain> addressCommand: mongodb://<my-sharded-cluster>-mongos-0-svc-external.<external-domain>,<my-sharded-cluster>-mongos-1-svc-external.<external-domain>, you can connect to this sharded cluster instance from outside of the Kubernetes cluster by using the following command:

mongosh ""
このページを評価