This procedure migrates any replica set that Ops Manager or Cloud Manager manages into Kubernetes under Kubernetes Operator, using normal MongoDB replication. This includes deployments that run on virtual machines or bare metal, and deployments already managed by a different Kubernetes Operator instance. The migration is live and incremental: you extend the replica set into Kubernetes, promote the Kubernetes members, and then prune the external members. There is no snapshot restore and no mongosync involved.
开始之前
在迁移副本集之前确认以下内容:
A Ops Manager or Cloud Manager connection
ConfigMapwith the keysbaseUrl,orgId, andprojectName.An API key
Secretwith the keyspublicKeyandprivateKey.The Kubernetes Operator
ServiceAccounthasbatch/jobspermissions (create,get,list,watch, anddelete) for the dry-run connectivity Job.该项目只包含一个部署。
您还需要:
An existing replica set that Ops Manager or Cloud Manager manages.
Exactly one deployment in the Ops Manager or Cloud Manager project.
安装了Kubernetes Operator 的Kubernetes集群。
The
kubectl mongodbplugin at a version that matches Kubernetes Operator. Kubernetes Operator enforces this match on every reconcile, including the dry run.虚拟机主机和Kubernetes Pod 之间的双向网络连接和主机名解析。
如果部署使用 TLS,则覆盖所有 SAN 的预配置 TLS 证书。
已掌握SCRAM用户密码。您无法从自动化配置中恢复它们。
开始之前进行的备份。
正常运行且处于目标状态的部署。
始终至少保留 3 个有投票权的节点。 Kubernetes Operator 拒绝了超过 7 个投票节点。如果超过 7,请添加无投票权的Kubernetes成员或删除有投票权的外部成员。
一次仅进行一种更改:添加Kubernetes节点、修剪外部节点或一次仅更改一个节点的投票和优先级。迁移开始后,准入验证会拒绝混合更改,也会拒绝在迁移过程中删除Kubernetes成员或添加外部成员。
仅当部署处于目标状态时才采取行动。
最后迁移主节点 (primary node in the replica set)。将投票和优先级转移到节点上可能会触发选举,并且在副本集选举新的主节点 (primary node in the replica set)时,写入可能会短暂失败。最后迁移主节点 (primary node in the replica set)可避免触发该选举及其导致的写入停机时间,直到最后一步。
注意
您还可以通过提高Kubernetes中节点的优先级触发重新选举。
While
spec.externalMembersis non-empty, Kubernetes Operator forces one-member-at-a-time scaling. This is why each change needs its own wait for goal state.
步骤
生成MongoDB自定义资源。
运行 kubectl mongodb migrate-to-mck mongodb 命令:
kubectl mongodb migrate-to-mck mongodb \ --config-map-name <configmap> \ --secret-name <secret> \ --namespace <namespace> \ -o mongodb-cr.yaml
检查生成的文件。它包含:
spec.externalMembers,列出源进程。spec.members设置为0。The
mongodb.com/migration-dry-run: trueannotation, which the plugin always adds.
该命令还支持这些可选标志:
--certs-secret-prefix: required when TLS is enabled. Setsspec.security.certsSecretPrefix.--prometheus-secret-name: use when Prometheus is enabled. TheSecretmust already exist and have apasswordkey.--resource-name-override: setsmetadata.nameon the generated resource. The plugin normalizes the replica set name automatically when it is not a valid Kubernetes name, and setsspec.replicaSetNameOverridefor you.
生成 MongoDBUser 自定义资源。
仅当您要迁移数据库用户时才需完成此步骤。迁移不需要它。
Pre-create one Secret per SCRAM user, each with a password key. Run:
kubectl mongodb migrate-to-mck users \ --config-map-name <configmap> \ --secret-name <secret> \ --namespace <namespace> \ --users-secrets-file users.csv \ -o users-cr.yaml
The CSV file maps users to Secrets, one per line, in the form username:database,secret-name. Omit --users-secrets-file to be prompted for each user instead.
该插件根据 $external数据库生成 X.509 和LDAP用户。它会跳过自动化代理用户。
如果部署使用 TLS,请颁发Kubernetes成员证书。
从签署虚拟机证书的同一证书颁发机构颁发Kubernetes成员证书。
Kubernetes Operator expects a kubernetes.io/tls Secret named <certsSecretPrefix>-<resourceName>-cert and a CA ConfigMap named <resourceName>-ca that contains both a ca-pem key and an mms-ca.crt key. The certificates need SANs that cover the per-Pod and Service DNS names, and both server auth and client auth usages.
If the source deployment does not use TLS, set net.tls.mode to disabled in Ops Manager or Cloud Manager, on the existing virtual machine deployment, before you migrate. You do not need to set anything in the MongoDB custom resource for this case.
注意
根据设计,下一步的试运行会发现 CA 不匹配的情况。
配置外部访问权限。
Before you run the dry run, configure spec.externalAccess so that the virtual machine members can reach the Kubernetes Pods. To let those members resolve the Pods by hostname, you can also set spec.externalAccess.externalDomain. To learn about the fields involved and the DNS requirements, see Network Requirements for Migration.
此设置由用户所有:您可以为您的环境配置 LoadBalancer 或 NodePort 以及 DNS 记录。
重要
Don't set externalDomain if you use MongoDB Search or Vector Search with this deployment. MongoDBSearch does not support a MongoDB resource that sets externalDomain, and you can't remove the field after you create the cluster. To learn more, see MongoDB Search Interaction with Migration.
运行运行并检查连接性。
Apply the generated resource with the mongodb.com/migration-dry-run annotation still present. While the annotation is set, Kubernetes Operator makes no automation config changes and only validates connectivity.
Kubernetes Operator creates a Job named <resourceName>-connectivity-check, which dials every external member and authenticates. The Job deletes itself using ttlSecondsAfterFinished, and the next reconcile re-creates it, so re-validation is automatic. You can fix problems in Kubernetes or in the Ops Manager UI and re-run freely.
The dry run checks Kubernetes-to-virtual-machine reachability (DNS, TLS, firewalls, and member addresses) and credentials, including the __system role on the local database, and the CA when TLS is enabled. It does not check virtual-machine-to-Kubernetes inbound connectivity.
由于该方向完全取决于您的网络设置,因此没有任何单个命令可以保证这一方向。相反,请仔细检查此清单:
Confirm that the hostnames the Kubernetes members will have, following the pattern
<metadata.name>-0.<spec.externalAccess.externalDomain>, are resolvable from the virtual machine members.确认可从虚拟机访问Kubernetes节点或 LoadBalancer IP。
Read the result from status.conditions[type=NetworkConnectivityVerified]:
验证器作业退出代码 | 条件状态 | 原因 | 含义 |
|---|---|---|---|
作业仍在运行 |
|
| The |
|
|
| 所有外部成员均可访问并通过身份验证。 |
|
|
| Credentials, the authentication mechanism, or a missing |
|
|
| DNS、TLS、超时或无法访问的成员。检查作业 Pod 日志。 |
|
|
| 未分类故障。检查作业 Pod 日志。 |
Failures that occur before the Job starts use the reasons OperatorImageUnknown, BuildStatefulSetOptions, AgentCertSecretFailed, and AgentCertSubject.
Kubernetes Operator removes the NetworkConnectivityVerified condition from status.conditions entirely once no external members remain.
将副本集扩展到Kubernetes。
删除试运行注解:
kubectl annotate mdb <resourceName> \ mongodb.com/migration-dry-run-
This is the point at which Kubernetes Operator takes ownership of the Ops Manager project.
Raise spec.members and hand-write spec.memberConfig together.
警告
在增加节点数之前设置 spec.memberConfig
By default, new Kubernetes members join as voting members. The CRD defaults are votes: 1 and priority: "1", which let a still-syncing member participate in an election before it has finished its initial sync.
MongoDB recommends that you write one spec.memberConfig entry per new Kubernetes member with votes: 0 and priority: "0" before you raise the member count, so that a still-syncing member cannot win an election. votes is an integer. priority is a string.
示例,要添加三个Kubernetes成员作为无投票权:
spec: memberConfig: - votes: 0 priority: "0" - votes: 0 priority: "0" - votes: 0 priority: "0"
Wait for full initial sync and goal state. If you generated MongoDBUser resources in the optional step above, apply them now and confirm each one reaches a status.phase of Updated.
提升Kubernetes成员。
Shift votes and priority onto the Kubernetes members by editing spec.memberConfig. votes is an integer. priority is a string containing a float.
External members keep whatever votes and priority they carry in the source automation config. spec.externalMembers has no votes or priority field.
等待目标状态。
修剪外部成员。
重要
Remove only one entry from spec.externalMembers at a time. Wait for goal state after each removal before you remove the next entry.
例如:
kubectl patch mdb <resourceName> --type=json \ -p='[{"op":"remove","path":"/spec/externalMembers/0"}]'
您可以删除条目,但一旦迁移开始就永远不能添加。
Watch status.conditions[type=Migrating].reason move through Extending, InProgress, and Pruning:
原因 | 状态 | 含义 |
|---|---|---|
|
| 已设立试运行注解。 |
|
| 所需的Kubernetes成员数超过了上次核对的数量。 |
|
| The |
|
| 外部成员存在,但没有任何变化。这也是第一次和解的原因。 |
|
| 所有外部成员均已删除。 |
Precedence is Validating > Extending > Pruning > InProgress. A prune that also grows the Kubernetes side reports Extending, which is another reason to make one change at a time.
不允许同时进行剪枝和扩展。
To script against migration completion, use kubectl wait --for=condition=Migrating=False rather than polling status.phase.
连接字符串密钥
Kubernetes Operator automatically generates a connection string Secret containing no credentials, named <metadata.name>-cluster-connection-string, and keeps it in sync with the active nodes. Point your applications at this Secret instead of hardcoding a connection string.
迁移期间的可用性
在整个迁移中,读取保持可用。在选举期间,写入可能会短暂失败,因此您的驱动程序必须使用可重试写入。如果读取目标移动,读取延迟可能会增加,因此请在与虚拟机相同的地区运行Kubernetes集群。针对已迁移的从节点(secondary node from replica set)的长时间运行游标会中断,因为没有静默模式。