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

MongoDB Enterprise Edition で検索をインストールと使用

Kubernetes演算子 を使用して、 MongoDB Search とベクトル検索リソースを配置して、 Kubernetesクラスター上のMongoDB Enterprise v 以上で実行できます。この手順では、8.2.0mongot Kubernetesクラスターで新規または既存のレプリカセットを使用して実行するように プロセスを配置して構成する方法を示します。この配置では TLS 証明書を使用して、 MongoDBノードとmongot 検索プロセス間の安全な通信を確保します。

MongoDB Search とベクトル検索を配置するには、次のものが必要です。

  • ローカルで使用可能な kubeconfig を含む実行中のKubernetesクラスター。

  • Kubernetesコマンドライン ツール(kubectl)は、クラスターと通信するように構成されています。

  • Helm、Kubernetesのパッケージマネージャーである Helm は、 Kubernetes演算子をインストールします。

  • このチュートリアルのコマンドを実行中には、Batch v5.1 以上が必要です。

  • MongoDB Ops Manager または MongoDB Cloud Manager のプロジェクトと API 認証情報。

1

この手順の後続のステップで使用する環境変数を設定します。次のコマンドをコピーして、環境の値を更新し、それらを実行することで変数をロードします。

1# set it to the context name of the k8s cluster
2export K8S_CTX="<local cluster context>"
3
4# the following namespace will be created if not exists
5export MDB_NS="mongodb"
6
7# name of the MongoDB Custom Resource.
8export MDB_RESOURCE_NAME="mdb-rs"
9
10export MDB_MEMBERS=3
11# OM/CM's project name to be used to manage mongodb replica set
12export OPS_MANAGER_PROJECT_NAME="<arbitrary project name>"
13
14# URL to Cloud Manager or Ops Manager instance
15export OPS_MANAGER_API_URL="https://cloud-qa.mongodb.com"
16
17# The API key can be an Org Owner - the operator can create the project automatically then.
18# The API key can also be created in a particular project that was created manually with the Project Owner scope.
19export OPS_MANAGER_API_USER="<SET API USER>"
20export OPS_MANAGER_API_KEY="<SET API KEY>"
21export OPS_MANAGER_ORG_ID="<SET ORG ID>"
22
23# minimum required MongoDB version for running MongoDB Search is 8.2.0
24export MDB_VERSION="8.2.0-ent"
25
26# root admin user for convenience, not used here at all in this guide
27export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME"
28# regular user performing restore and search queries on sample mflix database
29export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME"
30# user for MongoDB Search to connect to the replica set to synchronise data from
31export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME"
32
33export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"
34# comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operator
35export OPERATOR_ADDITIONAL_HELM_VALUES=""
36
37export MDB_TLS_CERT_SECRET_PREFIX="certs"
38export MDB_TLS_CA_CONFIGMAP="${MDB_RESOURCE_NAME}-ca-configmap"
39
40export CERT_MANAGER_NAMESPACE="cert-manager"
41export MDB_TLS_SELF_SIGNED_ISSUER="selfsigned-bootstrap-issuer"
42export MDB_TLS_CA_CERT_NAME="my-selfsigned-ca"
43export MDB_TLS_CA_SECRET_NAME="root-secret"
44export MDB_TLS_CA_ISSUER="my-ca-issuer"
45export MDB_TLS_SERVER_CERT_SECRET_NAME="${MDB_TLS_CERT_SECRET_PREFIX}-${MDB_RESOURCE_NAME}-cert"
46export MDB_SEARCH_TLS_SECRET_NAME="${MDB_RESOURCE_NAME}-search-tls"
47
48export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=${MDB_RESOURCE_NAME}&tls=true&tlsCAFile=/tls/ca.crt"

環境変数が設定されていることを検証します。

必要な環境変数がすべて設定されていることを確認するには、ターミナルで次のコードを実行します。

1required=(
2 K8S_CTX
3 MDB_NS
4 MDB_RESOURCE_NAME
5 MDB_VERSION
6 MDB_MEMBERS
7 CERT_MANAGER_NAMESPACE
8 MDB_TLS_CA_SECRET_NAME
9 MDB_TLS_SERVER_CERT_SECRET_NAME
10 MDB_SEARCH_TLS_SECRET_NAME
11 MDB_ADMIN_USER_PASSWORD
12 MDB_SEARCH_SYNC_USER_PASSWORD
13 MDB_USER_PASSWORD
14 OPERATOR_HELM_CHART
15 OPS_MANAGER_PROJECT_NAME
16 OPS_MANAGER_API_URL
17 OPS_MANAGER_API_USER
18 OPS_MANAGER_API_KEY
19)
20
21missing_req=()
22for v in "${required[@]}"; do [[ -n "${!v:-}" ]] || missing_req+=("${v}"); done
23if (( ${#missing_req[@]} )); then
24 echo "ERROR: Missing required environment variables:" >&2
25 for m in "${missing_req[@]}"; do echo " - ${m}" >&2; done
26else
27 echo "All required environment variables present."
28fi
2

Helm は、 Kubernetes上のMongoDBインスタンスの配置と管理を自動化します。Kubernetes Operator 演算子をインストールするための Helmチャートを含む Helmリポジトリをすでに追加している場合は、この手順をスキップします。それ以外の場合は、 Helmリポジトリを追加します。

を追加、コピー、貼り付け、実行するには、次のコマンドを実行します。

1helm repo add mongodb https://mongodb.github.io/helm-charts
2helm repo update mongodb
3helm search repo mongodb/mongodb-kubernetes
3

Kubernetes 演算子は、MongoDBMongoDBOpsManager、MongoDBSearch のカスタム リソースを監視し、MongoDB配置のライフサイクルを管理します。MongoDB コントローラー for Kubernetes 演算子をすでにインストールしている場合は、この手順をスキップします。それ以外の場合は、前の手順で追加した Helm リポジトリから MongoDB 用の Kubernetes Operator をインストールします。

MongoDB Controls for Kubernetes Operator を mongodb名前空間にインストールするには、次のコマンドをコピー、貼り付け、実行します。

1helm upgrade --install --debug --kube-context "${K8S_CTX}" \
2 --create-namespace \
3 --namespace="${MDB_NS}" \
4 mongodb-kubernetes \
5 ${OPERATOR_ADDITIONAL_HELM_VALUES:+--set ${OPERATOR_ADDITIONAL_HELM_VALUES}} \
6 "${OPERATOR_HELM_CHART}"
4

The cert-manager is required for managing TLS certificates. If you already have cert-manager installed in your cluster, skip this step. Otherwise, install cert-manager using Helm.

cert-manager名前空間に cert-manager をインストールするには、ターミナルで次のコマンドを実行します。

1helm upgrade --install \
2 cert-manager \
3 oci://quay.io/jetstack/charts/cert-manager \
4 --kube-context "${K8S_CTX}" \
5 --namespace "${CERT_MANAGER_NAMESPACE}" \
6 --create-namespace \
7 --set crds.enabled=true
8
9for deployment in cert-manager cert-manager-cainjector cert-manager-webhook; do
10 kubectl --context "${K8S_CTX}" \
11 -n "${CERT_MANAGER_NAMESPACE}" \
12 wait --for=condition=Available "deployment/${deployment}" --timeout=300s
13done
14
15echo "cert-manager is ready in namespace ${CERT_MANAGER_NAMESPACE}."
5

MongoDBMongoDBSearch リソースの TLS 証明書を発行する認証局インフラストラクチャを作成します。コマンドは、次のアクションを実行します。

  • 自己署名 ClusterIssuer を作成します。

  • CA 証明書を生成します。

  • すべての名前空間が使用できるクラスター全体の CA 発行者を公開します。

  • MongoDBリソースがそれを使用できるように、ConfigMap を通じて CA バンドルを公開します。

1# Bootstrap a self-signed ClusterIssuer to mint the CA secret consumed by application workloads.
2kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST
3apiVersion: cert-manager.io/v1
4kind: ClusterIssuer
5metadata:
6 name: ${MDB_TLS_SELF_SIGNED_ISSUER}
7spec:
8 selfSigned: {}
9EOF_MANIFEST
10
11kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_SELF_SIGNED_ISSUER}"
12
13kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST
14apiVersion: cert-manager.io/v1
15kind: Certificate
16metadata:
17 name: ${MDB_TLS_CA_CERT_NAME}
18 namespace: ${CERT_MANAGER_NAMESPACE}
19spec:
20 isCA: true
21 commonName: ${MDB_TLS_CA_CERT_NAME}
22 secretName: ${MDB_TLS_CA_SECRET_NAME}
23 privateKey:
24 algorithm: ECDSA
25 size: 256
26 issuerRef:
27 name: ${MDB_TLS_SELF_SIGNED_ISSUER}
28 kind: ClusterIssuer
29EOF_MANIFEST
30
31kubectl --context "${K8S_CTX}" wait --for=condition=Ready -n "${CERT_MANAGER_NAMESPACE}" certificate "${MDB_TLS_CA_CERT_NAME}"
32
33kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST
34apiVersion: cert-manager.io/v1
35kind: ClusterIssuer
36metadata:
37 name: ${MDB_TLS_CA_ISSUER}
38spec:
39 ca:
40 secretName: ${MDB_TLS_CA_SECRET_NAME}
41EOF_MANIFEST
42
43kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_CA_ISSUER}"
44
45TMP_CA_CERT="$(mktemp)"
46trap 'rm -f "${TMP_CA_CERT}"' EXIT
47
48kubectl --context "${K8S_CTX}" get secret "${MDB_TLS_CA_SECRET_NAME}" -n "${CERT_MANAGER_NAMESPACE}" -o jsonpath="{.data['ca\\.crt']}" | base64 --decode > "${TMP_CA_CERT}"
49
50kubectl --context "${K8S_CTX}" create configmap "${MDB_TLS_CA_CONFIGMAP}" -n "${MDB_NS}" \
51 --from-file=ca-pem="${TMP_CA_CERT}" --from-file=mms-ca.crt="${TMP_CA_CERT}" \
52 --from-file=ca.crt="${TMP_CA_CERT}" \
53 --dry-run=client -o yaml | kubectl --context "${K8S_CTX}" apply -f -
6

MongoDBサーバー(${MDB_RESOURCE_NAME}-server-tls )と サービス( )の両方の TLSMongoDBSearch ${MDB_RESOURCE_NAME}-search-tls証明書を発行します。 MongoDBサーバー証明書には、ポッドとサービス通信に必要なすべての DNS 名が含まれます。どちらの証明書もサーバーとクライアントの認証をサポートしています。

1server_certificate="${MDB_RESOURCE_NAME}-server-tls"
2search_certificate="${MDB_RESOURCE_NAME}-search-tls"
3
4mongo_dns_names=()
5for ((member = 0; member < MDB_MEMBERS; member++)); do
6 mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}")
7 mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local")
8done
9mongo_dns_names+=(
10 "${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local"
11 "*.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local"
12)
13
14search_dns_names=(
15 "${MDB_RESOURCE_NAME}-search-svc.${MDB_NS}.svc.cluster.local"
16)
17
18render_dns_list() {
19 local dns_list=("$@")
20 for dns in "${dns_list[@]}"; do
21 printf " - \"%s\"\n" "${dns}"
22 done
23}
24
25kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF_MANIFEST
26apiVersion: cert-manager.io/v1
27kind: Certificate
28metadata:
29 name: ${server_certificate}
30 namespace: ${MDB_NS}
31spec:
32 secretName: ${MDB_TLS_SERVER_CERT_SECRET_NAME}
33 issuerRef:
34 name: ${MDB_TLS_CA_ISSUER}
35 kind: ClusterIssuer
36 duration: 240h0m0s
37 renewBefore: 120h0m0s
38 usages:
39 - digital signature
40 - key encipherment
41 - server auth
42 - client auth
43 dnsNames:
44$(render_dns_list "${mongo_dns_names[@]}")
45---
46apiVersion: cert-manager.io/v1
47kind: Certificate
48metadata:
49 name: ${search_certificate}
50 namespace: ${MDB_NS}
51spec:
52 secretName: ${MDB_SEARCH_TLS_SECRET_NAME}
53 issuerRef:
54 name: ${MDB_TLS_CA_ISSUER}
55 kind: ClusterIssuer
56 duration: 240h0m0s
57 renewBefore: 120h0m0s
58 usages:
59 - digital signature
60 - key encipherment
61 - server auth
62 - client auth
63 dnsNames:
64$(render_dns_list "${search_dns_names[@]}")
65EOF_MANIFEST
66
67kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${server_certificate}" --timeout=300s
68kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${search_certificate}" --timeout=300s
7

MongoDB Enterpriseをすでに配置している場合は、次の手順に進みます。それ以外の場合は、 MongoDB Enterpriseリソースを配置します。

MongoDB Enterpriseを配置するには、次の手順を実行します。

  1. MongoDB Ops Managerを使用している場合は、MongoDB Ops ManagerプロジェクトのConfigMapとシークレットを作成します。

    MongoDB Ops Managerと統合する構成と認証情報を保存するには、次のコマンドをコピー、貼り付け、実行します。

    1kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create configmap om-project \
    2 --from-literal=projectName="${OPS_MANAGER_PROJECT_NAME}" --from-literal=baseUrl="${OPS_MANAGER_API_URL}" \
    3 --from-literal=orgId="${OPS_MANAGER_ORG_ID:-}"
    4
    5kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create secret generic om-credentials \
    6 --from-literal=publicKey="${OPS_MANAGER_API_USER}" \
    7 --from-literal=privateKey="${OPS_MANAGER_API_KEY}"
  2. mdb-rs という名前の MongoDB カスタムリソースを作成します。

    リソースは、mongod コンテナと mongodb-agent コンテナの CPU とメモリ リソースを定義し、 Kubernetes Operator に 3 ノードでMongoDBレプリカセットを構成するように指示します。

    MongoDB Enterpriseを配置するには、以下をコピーして貼り付け、名前空間で実行します。

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDB
    4metadata:
    5 name: ${MDB_RESOURCE_NAME}
    6spec:
    7 members: ${MDB_MEMBERS}
    8 version: ${MDB_VERSION}
    9 type: ReplicaSet
    10 opsManager:
    11 configMapRef:
    12 name: om-project
    13 credentials: om-credentials
    14 security:
    15 authentication:
    16 enabled: true
    17 ignoreUnknownUsers: true
    18 modes:
    19 - SCRAM
    20 certsSecretPrefix: ${MDB_TLS_CERT_SECRET_PREFIX}
    21 tls:
    22 enabled: true
    23 ca: ${MDB_TLS_CA_CONFIGMAP}
    24 agent:
    25 logLevel: INFO
    26 podSpec:
    27 podTemplate:
    28 spec:
    29 containers:
    30 - name: mongodb-enterprise-database
    31 resources:
    32 limits:
    33 cpu: "2"
    34 memory: 2Gi
    35 requests:
    36 cpu: "1"
    37 memory: 1Gi
    38EOF
  3. MongoDBリソースの配置が完了するまで待ちます。

    MongoDB カスタムリソースを適用すると、 Kubernetes演算子によってMongoDBノード(ポッド)の配置が開始されます。このステップでは、mdbc-rs リソースのステータス フェーズが Running になるまで実行を一時停止します。これはMongoDB Communityレプリカセットが操作可能であることを示します。

    1echo "Waiting for MongoDB resource to reach Running phase..."
    2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdb/${MDB_RESOURCE_NAME}" --timeout=400s
    3echo; echo "MongoDB resource"
    4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}"
    5echo; echo "Pods running in cluster ${K8S_CTX}"
    6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
8

MongoDB、安全なアクセスのために認証が必要です。この手順では、3 つのKubernetesシークレットを作成します。

  • mdb-admin-user-password: MongoDB管理者の認証情報。

  • mdb-user-password: 検索クエリを実行する権限を持つユーザーの認証情報。

  • mdbc-rs-search-sync-source-password: データを同期し、インデックスを管理するために mongot プロセスによって内部的に使用される専用検索ユーザーの認証情報。

Kubernetes Operator はこれらのシークレットをMongoDBポッドにマウントします。

シークレットを作成するには、 MongoDB Serverを配置し、 MongoDB Search とベクトル検索を配置する予定の名前空間で、以下をコピーして貼り付け、実行します。

1# admin user with root role
2kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
3 create secret generic mdb-admin-user-password \
4 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}"
5
6kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
7apiVersion: mongodb.com/v1
8kind: MongoDBUser
9metadata:
10 name: mdb-admin
11spec:
12 username: mdb-admin
13 db: admin
14 mongodbResourceRef:
15 name: ${MDB_RESOURCE_NAME}
16 passwordSecretKeyRef:
17 name: mdb-admin-user-password
18 key: password
19 roles:
20 - name: root
21 db: admin
22EOF
23
24# user used by MongoDB Search to connect to MongoDB database to synchronize data from
25# For MongoDB <8.2, the operator will be creating the searchCoordinator custom role automatically
26# From MongoDB 8.2, searchCoordinator role will be a built-in role.
27kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
28 create secret generic "${MDB_RESOURCE_NAME}-search-sync-source-password" \
29 --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}"
30kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
31apiVersion: mongodb.com/v1
32kind: MongoDBUser
33metadata:
34 name: search-sync-source-user
35spec:
36 username: search-sync-source
37 db: admin
38 mongodbResourceRef:
39 name: ${MDB_RESOURCE_NAME}
40 passwordSecretKeyRef:
41 name: ${MDB_RESOURCE_NAME}-search-sync-source-password
42 key: password
43 roles:
44 - name: searchCoordinator
45 db: admin
46EOF
47
48# user performing search queries
49kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
50 create secret generic mdb-user-password \
51 --from-literal=password="${MDB_USER_PASSWORD}"
52kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
53apiVersion: mongodb.com/v1
54kind: MongoDBUser
55metadata:
56 name: mdb-user
57spec:
58 username: mdb-user
59 db: admin
60 mongodbResourceRef:
61 name: ${MDB_RESOURCE_NAME}
62 passwordSecretKeyRef:
63 name: mdb-user-password
64 key: password
65 roles:
66 - name: readWrite
67 db: sample_mflix
68EOF
9

負荷分散なしで、検索ノードの 1 つのインスタンスを配置できます。配置するには、次の手順を実行します。

  1. mdbc-rs という名前の MongoDBSearch カスタムリソースを作成します。

    このリソースは、検索ノードの CPU とメモリのリソース要件を指定します。このカスタムリソースの設定の詳細については、MongoDB Search とベクトル検索の設定を参照してください。

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDBSearch
    4metadata:
    5 name: ${MDB_RESOURCE_NAME}
    6spec:
    7 # no need to specify source.mongodbResourceRef if MongoDBSearch CR has the same name as MongoDB CR
    8 # the operator infer it automatically
    9 security:
    10 tls:
    11 certificateKeySecretRef:
    12 name: ${MDB_SEARCH_TLS_SECRET_NAME}
    13 resourceRequirements:
    14 limits:
    15 cpu: "3"
    16 memory: 5Gi
    17 requests:
    18 cpu: "2"
    19 memory: 3Gi
    20EOF
  2. MongoDBSearchリソースの配置が完了するまで待機します。

    MongoDBSearch カスタムリソースを適用すると、Kubernetes演算子による検索ノード(ポッド)の配置が開始されます。このステップでは、mdbc-rs リソースのステータス フェーズが Running になるまで実行を一時停止します。このフェーズはMongoDB Communityレプリカセットが操作可能であることを示します。

    1echo "Waiting for MongoDBSearch resource to reach Running phase..."
    2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdbs/${MDB_RESOURCE_NAME}" --timeout=300s
10

MongoDBSearch を使用した MongoDBリソースの配置が成功させることを確認します。

1echo "Waiting for MongoDB resource to reach Running phase..."
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdb/${MDB_RESOURCE_NAME}" --timeout=400s
11

MongoDBレプリカセットノード、 Kubernetes演算子用のMongoDBコントロール、および検索ノードの名前空間ポッド内のすべての実行中中のポッドを表示します。

1echo; echo "MongoDB resource"
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}"
3echo; echo "MongoDBSearch resource"
4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdbs/${MDB_RESOURCE_NAME}"
5echo; echo "Pods running in cluster ${K8S_CTX}"
6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods

MongoDB Enterprise Edition で使用するMongoDB Search とベクトル検索を正常に配置したので、 MongoDBクラスターにデータを追加し、 MongoDB Search およびベクトル検索インデックスを作成し、データに対してクエリを実行できます。詳細については、 MongoDB Search とベクトル検索 の設定 を参照してください。

このページを評価