You can convert an Ops Manager resource that uses an internally managed Application Database to one that uses an external Application Database: a MongoDB custom resource with spec.role set to AppDB. This lets Ops Manager back up and restore the Application Database. To learn more, see Back Up the Ops Manager Application Database.
The migration re-points ownership of the existing Application Database StatefulSet and its Persistent Volumes from the Ops Manager resource to the MongoDB resource. The Kubernetes Operator doesn't move or recreate your data, and it doesn't restart the Ops Manager pods.
迁移如何进行
The Ops Manager resource starts with an internally managed Application Database, and the Kubernetes Operator owns a StatefulSet named <primary-om-name>-db. The migration proceeds as follows:
You create a MongoDB resource with
spec.roleset toAppDBand the same name as the StatefulSet, in a project that a management Ops Manager instance manages. The MongoDB resource can't take ownership of the StatefulSet yet, so it stays in thePendingphase with a message that it can't take ownership of the Application Database StatefulSet.You add
spec.externalApplicationDatabaseRefto the Ops Manager resource. The Kubernetes Operator detaches the StatefulSet by removing the Ops Manager resource's owner reference and marking the StatefulSet as ready for migration. The MongoDB resource then takes ownership of it.
由于Kubernetes Operator 计算与以前相同的连接字符串,并且使用相同的主机名,因此Ops Manager Pod 不会重新启动。
先决条件
在开始之前,请完成以下任务:
Install the Kubernetes Operator and
kubectl. To learn more, see Install with Kubernetes.Deploy a primary Ops Manager resource that uses an internally managed Application Database. This resource sets
spec.applicationDatabase, and the Kubernetes Operator owns a StatefulSet named<primary-om-name>-db. To learn more, see Deploy an Ops Manager Resource.Deploy a management Ops Manager resource that is in the
Runningphase and that owns the project for the external Application Database. To learn more, see Deploy Ops Manager with an External Application Database.Confirm that the Kubernetes Operator created the programmatic API key secret for the management Ops Manager resource. The Kubernetes Operator names this secret
<namespace>-<management-om-name>-admin-key.
Considerations
在迁移之前,请查看以下注意事项:
您在MongoDB资源上设立的应用程序数据库版本必须与内部托管的应用程序数据库运行的版本匹配,并且必须是管理Ops Manager实例提供的MongoDB版本。
The Kubernetes Operator doesn't support
spec.applicationDatabase.passwordSecretKeyRefduring migration. The Kubernetes Operator generates a new password, and it rotates any password that you provided.如果MongoDB资源配置与内部托管的应用程序数据库配置不同(例如不同的 Pod 模板或不同的容器设立),则应用程序数据库可能会在一到两分钟内不可用。如果两个配置在语义上相同,则应用程序数据库仍然可用。
Kubernetes Operator 仅支持在单个Kubernetes集群上迁移。
步骤
为迁移设置环境变量。
export K8S_CTX="<your-kube-context>" export MDB_NS="mongodb" export MANAGEMENT_OM_NAME="management-om" export PRIMARY_OM_NAME="primary-om" export APPDB_NAME="${PRIMARY_OM_NAME}-db" export APPDB_VERSION="8.0.5-ent" export MANAGEMENT_OM_URL="http://${MANAGEMENT_OM_NAME}-svc.${MDB_NS}.svc.cluster.local:8080" export MANAGEMENT_OM_ADMIN_KEY_SECRET="${MDB_NS}-${MANAGEMENT_OM_NAME}-admin-key" export APPDB_PROJECT_CONFIGMAP="${APPDB_NAME}-config" export APPDB_PROJECT_NAME="external-appdb"
确认起始状态。
Confirm that the primary Ops Manager resource and its internally managed Application Database are in the
Runningphase:kubectl get om "${PRIMARY_OM_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" \ -o jsonpath='{.status.opsManager.phase} / appdb={.status.applicationDatabase.phase}{"\n"}' 确认Ops Manager资源拥有应用程序数据库 StatefulSet:
kubectl get statefulset "${APPDB_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" \ -o jsonpath='{range .metadata.ownerReferences[*]}{.kind}/{.name}{"\n"}{end}' The command returns
MongoDBOpsManager/${PRIMARY_OM_NAME}.
为外部应用程序数据库创建MongoDB资源。
为资源指定与现有应用程序数据库 StatefulSet 相同的名称。
kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF apiVersion: mongodb.com/v1 kind: MongoDB metadata: name: ${APPDB_NAME} spec: members: 3 version: ${APPDB_VERSION} type: ReplicaSet role: AppDB opsManager: configMapRef: name: ${APPDB_PROJECT_CONFIGMAP} credentials: ${MANAGEMENT_OM_ADMIN_KEY_SECRET} persistent: true security: authentication: enabled: true modes: ["SCRAM"] ignoreUnknownUsers: true EOF
确认MongoDB资源正在等待所有权。
Until the Ops Manager resource releases the StatefulSet, the MongoDB resource stays in the Pending phase. This is expected and isn't an error.
kubectl wait --for=jsonpath='{.status.phase}'=Pending \ mdb/"${APPDB_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" --timeout=300s kubectl get mdb "${APPDB_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" \ -o jsonpath='{.status.message}{"\n"}'
状态消息报告资源无法获取应用程序数据库 StatefulSet 的所有权。
将外部应用程序数据库引用添加到Ops Manager资源。
kubectl patch om "${PRIMARY_OM_NAME}" \ --context "${K8S_CTX}" -n "${MDB_NS}" \ --type merge \ -p "{\"spec\":{\"externalApplicationDatabaseRef\":{\"name\":\"${APPDB_NAME}\",\"kind\":\"MongoDB\"}}}"
You can remove spec.applicationDatabase in the same patch. Leaving it in place has no effect: once you set spec.externalApplicationDatabaseRef, the Kubernetes Operator stops managing an internal Application Database for this resource.
验证迁移。
确认MongoDB资源拥有应用程序数据库 StatefulSet:
kubectl get statefulset "${APPDB_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" \ -o jsonpath='{range .metadata.ownerReferences[*]}{.kind}/{.name}{"\n"}{end}' The command returns
MongoDB/${APPDB_NAME}.Confirm that the Ops Manager resource reports its Application Database as
Disabled, which means that the Kubernetes Operator no longer manages an internal Application Database for it:kubectl get om "${PRIMARY_OM_NAME}" --context "${K8S_CTX}" -n "${MDB_NS}" \ -o jsonpath='{.status.applicationDatabase.phase}{"\n"}' 检查 Ops Manager Pod 的期限和重启次数,确认Ops Manager Pod 没有重启:
kubectl get pods --context "${K8S_CTX}" -n "${MDB_NS}"
常见迁移问题
MongoDB资源报告版本错误
If the MongoDB resource reports Failed with a message that the MongoDB version isn't available, set spec.version to a version that the management Ops Manager instance offers, and keep the Ops Manager version and the Application Database version on the same major version.
补丁发布后, MongoDB资源保持待处理状态
The Kubernetes Operator must detach the StatefulSet before the MongoDB resource can take ownership of it. Confirm that you applied spec.externalApplicationDatabaseRef to the primary Ops Manager resource, and that the value of spec.externalApplicationDatabaseRef.name is exactly <primary-om-name>-db.
MongoDB资源报告项目错误
If the MongoDB resource stays in the Pending phase with a project or registration error, the management Ops Manager public API might not have been ready when you created the resource. Confirm that the management Ops Manager resource is in the Running phase and that its API answers requests, then reconcile the resource again.
Ops Manager Pod 在迁移期间重新启动
如果Ops Manager Pod 重新启动,则计算出的连接字符串会发生变化。对于使用默认端口的副本集,迁移前后的连接字符串是相同的。 Kubernetes Operator 不支持此迁移的非默认端口。
后续步骤
To back up the external Application Database, enable backup on the MongoDB resource in the project that the management Ops Manager instance manages. To learn more, see Configure MongoDB Database Backups.
To reverse this migration, see Return an External Application Database to Ops Manager.