Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Menu Docs
Página inicial do Docs
/
Operador de Kubernetes empresarial
/ /

Cluster fragmentado de vários clusters sem uma interface de serviço

Você pode distribuir clusters fragmentados do MongoDB em vários clusters Kubernetes. Com a funcionalidade de vários clusters, você pode:

  • Melhore a resiliência de sua implantação distribuindo-a em vários clusters Kubernetes, cada um em uma região geográfica diferente.

  • Configure sua implantação para fragmentação geográfica implantando nós primários de fragmentos especificados em diferentes clusters Kubernetes localizados mais próximos do aplicação ou dos clientes que dependem desses dados, reduzindo a latência.

  • Ajuste seu sistema para melhorar o desempenho. Por exemplo, você pode implantar nós analíticos somente leitura para todos ou para shards especificados em diferentes clusters Kubernetes ou com alocações de recursos personalizadas.

Antes de iniciar o procedimento a seguir, execute as seguintes ações:

  • Instale o kubectl.

  • Instale o mongosh

  • Conclua o procedimento GKE Clusters ou equivalente.

  • Conclua o procedimento DNS externo ou equivalente.

  • Conclua o procedimento Certificados TLS ou equivalente.

  • Conclua o procedimento Implementar o operador MongoDB.

  • Conclua o procedimento de procedimento do Multi-Cluster Ops Manager. Você pode pular esta etapa se usar o Cloud Manager em vez do Ops Manager.

  • Configure as variáveis de ambiente necessárias da seguinte maneira:

# 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}
export SC_RESOURCE_NAME=mdb-sh
export MONGODB_VERSION="8.0.5-ent"

Você pode encontrar todo o código-fonte incluído no repositório do MongoDB Kubernetes Operator.

1

Execute o comando a seguir para gerar os certificados TLS necessários para cada shard, seus mongos e seus servidores de configuração.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-cert
usages:
- server auth
- client auth
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-0-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-0-cert
usages:
- server auth
- client auth
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-1-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-1-cert
usages:
- server auth
- client auth
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-2-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-2-cert
usages:
- server auth
- client auth
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-config-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-config-cert
usages:
- server auth
- client auth
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: mdb-sh-mongos-cert
spec:
dnsNames:
- "*.${MDB_NAMESPACE}.svc.cluster.local"
duration: 240h0m0s
issuerRef:
name: my-ca-issuer
kind: ClusterIssuer
renewBefore: 120h0m0s
secretName: cert-prefix-mdb-sh-mongos-cert
usages:
- server auth
- client auth
EOF
2

Execute o seguinte comando para implementar seus recursos personalizados.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOF
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
name: ${SC_RESOURCE_NAME}
spec:
shardCount: 3
# we don't specify mongodsPerShardCount, mongosCount and configServerCount as they don't make sense for multi-cluster
topology: MultiCluster
type: ShardedCluster
version: ${MONGODB_VERSION}
opsManager:
configMapRef:
name: mdb-org-project-config
credentials: mdb-org-owner-credentials
persistent: true
backup:
mode: enabled
externalAccess: {}
security:
certsSecretPrefix: cert-prefix
tls:
ca: ca-issuer
authentication:
enabled: true
modes: ["SCRAM"]
mongos:
clusterSpecList:
- clusterName: ${K8S_CLUSTER_0_CONTEXT_NAME}
members: 2
configSrv:
clusterSpecList:
- clusterName: ${K8S_CLUSTER_0_CONTEXT_NAME}
members: 3 # config server will have 3 members in main cluster
- clusterName: ${K8S_CLUSTER_1_CONTEXT_NAME}
members: 1 # config server will have additional non-voting, read-only member in this cluster
memberConfig:
- votes: 0
priority: "0"
shard:
clusterSpecList:
- clusterName: ${K8S_CLUSTER_0_CONTEXT_NAME}
members: 3 # each shard will have 3 members in this cluster
- clusterName: ${K8S_CLUSTER_1_CONTEXT_NAME}
members: 1 # each shard will have additional non-voting, read-only member in this cluster
memberConfig:
- votes: 0
priority: "0"
EOF
3

Execute o seguinte comando para confirmar que todos os recursos estão instalados.

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 "mdb/${SC_RESOURCE_NAME}" --timeout=900s
echo; echo "Pods running in cluster ${K8S_CLUSTER_0_CONTEXT_NAME}"
kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get pods
echo; echo "Pods running in cluster ${K8S_CLUSTER_1_CONTEXT_NAME}"
kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get pods
echo; echo "Pods running in cluster ${K8S_CLUSTER_2_CONTEXT_NAME}"
kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" get pods
4

Execute o comando a seguir para criar um usuário e credenciais em seu cluster fragmentado.

kubectl apply --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" -f - <<EOF
apiVersion: v1
kind: Secret
metadata:
name: sc-user-password
type: Opaque
stringData:
password: password
---
apiVersion: mongodb.com/v1
kind: MongoDBUser
metadata:
name: sc-user
spec:
passwordSecretKeyRef:
name: sc-user-password
key: password
username: "sc-user"
db: "admin"
mongodbResourceRef:
name: ${SC_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/sc-user --timeout=300s
5

Execute o seguinte comando para verificar se o recurso MongoDB em seu cluster fragmentado está acessível.

external_ip="$(kubectl get --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" svc "${SC_RESOURCE_NAME}-mongos-0-0-svc-external" -o=jsonpath="{.status.loadBalancer.ingress[0].ip}")"
mkdir -p certs
kubectl get --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" cm/ca-issuer -o=jsonpath='{.data.ca-pem}' > certs/ca.crt
mongosh --host "${external_ip}" --username sc-user --password password --tls --tlsCAFile certs/ca.crt --tlsAllowInvalidHostnames --eval "db.runCommand({connectionStatus : 1})"
{
authInfo: {
authenticatedUsers: [ { user: 'sc-user', db: 'admin' } ],
authenticatedUserRoles: [ { role: 'root', db: 'admin' } ]
},
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1741702735, i: 1 }),
signature: {
hash: Binary.createFromBase64('kVqqNDHTI1zxYrPsU0QaYqyksJA=', 0),
keyId: Long('7480555706358169606')
}
},
operationTime: Timestamp({ t: 1741702735, i: 1 })
}

Voltar

Conjuntos de réplicas em vários clusters

Nesta página