Para agentes de IA: hay un índice de documentación disponible en https://www.mongodb.com/es/docs/llms.txt — versiones en markdown de todas las páginas están disponibles agregando .md a cualquier ruta URL.

ReplicaSets multi-clúster sin un Service Mesh

Las implementaciones de MongoDB multinube en clústeres de Kubernetes permiten añadir instancias de MongoDB en clústeres globales que abarcan varias regiones geográficas para aumentar la disponibilidad y la distribución global de los datos.

Antes de comenzar el siguiente procedimiento, tome las siguientes acciones:

# This script builds on top of the environment configured in the setup guides.# It depends (uses) the following env variables defined there to work correctly.# If you don't use the setup guide to bootstrap the environment, then define them here.# ${K8S_CLUSTER_0_CONTEXT_NAME}# ${K8S_CLUSTER_1_CONTEXT_NAME}# ${K8S_CLUSTER_2_CONTEXT_NAME}# ${MDB_NAMESPACE}# ${CUSTOM_DOMAIN} export RS_RESOURCE_NAME=mdbexport MONGODB_VERSION="8.0.5-ent" export MDB_CLUSTER_0_EXTERNAL_DOMAIN="${K8S_CLUSTER_0}.${CUSTOM_DOMAIN}"export MDB_CLUSTER_1_EXTERNAL_DOMAIN="${K8S_CLUSTER_1}.${CUSTOM_DOMAIN}"export MDB_CLUSTER_2_EXTERNAL_DOMAIN="${K8S_CLUSTER_2}.${CUSTOM_DOMAIN}"

Se puede encontrar todo el código fuente incluido en el repositorio de MongoDB Kubernetes operador.

1

Ejecute el siguiente script para crear el certificado CA requerido con su emisor de certificados.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOFapiVersion: cert-manager.io/v1kind: Certificatemetadata: name: mdb-certspec: dnsNames: - "*.${MDB_CLUSTER_0_EXTERNAL_DOMAIN}" - "*.${MDB_CLUSTER_1_EXTERNAL_DOMAIN}" - "*.${MDB_CLUSTER_2_EXTERNAL_DOMAIN}" duration: 240h0m0s issuerRef: name: my-ca-issuer kind: ClusterIssuer renewBefore: 120h0m0s secretName: cert-prefix-mdb-cert usages: - server auth - client authEOF
2

Establece spec.credentials, spec.opsManager.configMapRef.name, que definiste en el procedimiento Multi-Cluster Ops Manager sin una Service Mesh; define tus configuraciones de seguridad e implementa el recurso MongoDBMultiCluster.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBMultiClustermetadata: name: ${RS_RESOURCE_NAME}spec: type: ReplicaSet version: ${MONGODB_VERSION} opsManager: configMapRef: name: mdb-org-project-config credentials: mdb-org-owner-credentials duplicateServiceObjects: false persistent: true backup: mode: enabled security: certsSecretPrefix: cert-prefix tls: ca: ca-issuer authentication: enabled: true modes: ["SCRAM"] clusterSpecList: - clusterName: ${K8S_CLUSTER_0_CONTEXT_NAME} members: 2 externalAccess: externalDomain: "${MDB_CLUSTER_0_EXTERNAL_DOMAIN}" externalService: annotations: external-dns.alpha.kubernetes.io/hostname: "{podName}.${MDB_CLUSTER_0_EXTERNAL_DOMAIN}" - clusterName: ${K8S_CLUSTER_1_CONTEXT_NAME} members: 1 externalAccess: externalDomain: "${MDB_CLUSTER_1_EXTERNAL_DOMAIN}" externalService: annotations: external-dns.alpha.kubernetes.io/hostname: "{podName}.${MDB_CLUSTER_1_EXTERNAL_DOMAIN}" - clusterName: ${K8S_CLUSTER_2_CONTEXT_NAME} members: 2 externalAccess: externalDomain: "${MDB_CLUSTER_2_EXTERNAL_DOMAIN}" externalService: annotations: external-dns.alpha.kubernetes.io/hostname: "{podName}.${MDB_CLUSTER_2_EXTERNAL_DOMAIN}"EOF
3

Ejecute el siguiente comando para confirmar que el recurso MongoDBMultiCluster se está ejecutando.

echo; echo "Waiting for MongoDB to reach Running phase..."kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" wait --for=jsonpath='{.status.phase}'=Running "mdbmc/${RS_RESOURCE_NAME}" --timeout=900secho; echo "Pods running in cluster ${K8S_CLUSTER_0_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get podsecho; echo "Pods running in cluster ${K8S_CLUSTER_1_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get podsecho; echo "Pods running in cluster ${K8S_CLUSTER_2_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get pods
4

Ejecuta el siguiente comando para crear un usuario y una contraseña de MongoDB. Utiliza contraseñas seguras para tus implementaciones.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOFapiVersion: v1kind: Secretmetadata: name: rs-user-passwordtype: OpaquestringData: password: password---apiVersion: mongodb.com/v1kind: MongoDBUsermetadata: name: rs-userspec: passwordSecretKeyRef: name: rs-user-password key: password username: "rs-user" db: "admin" mongodbResourceRef: name: ${RS_RESOURCE_NAME} roles: - db: "admin" name: "root"EOF kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" wait --for=jsonpath='{.status.phase}'=Updated -n "${MDB_NAMESPACE}" mdbu/rs-user --timeout=300s
5

Ejecuta el siguiente comando mongosh para asegurarte de que puedes acceder a la instancia en ejecución de MongoDB.

external_ip="$(kubectl get --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" svc "${RS_RESOURCE_NAME}-0-0-svc-external" -o=jsonpath="{.status.loadBalancer.ingress[0].ip}")" mkdir -p certskubectl get --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" cm/ca-issuer -o=jsonpath='{.data.ca-pem}' > certs/ca.crt for attempt in 1 2 3 4 5 6; do if mongosh --host "${external_ip}" --username rs-user --password password --tls --tlsCAFile certs/ca.crt --tlsAllowInvalidHostnames --eval "db.runCommand({connectionStatus : 1})"; then break fi if (( attempt < 6 )); then echo "mongosh auth failed (attempt ${attempt}/6), retrying in 10s..." >&2 sleep 10 fidone
{ authInfo: { authenticatedUsers: [ { user: 'rs-user', db: 'admin' } ], authenticatedUserRoles: [ { role: 'root', db: 'admin' } ] }, ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1792487693, i: 9 }), signature: { hash: Binary.createFromBase64('exoTqpH9opaVCYVVqgG7i0T+q4I=', 0), keyId: Long('7698676015622520837') } }, operationTime: Timestamp({ t: 1792487693, i: 9 })}