创建一个实用程序来生成 100 个 MongoDB 集合,每个集合填充 100 万个随机文档,并将其部署到 Kubernetes 上涉及几个步骤。本指南逐步介绍了从设置 Kubernetes 环境到生成集合以及在专用命名空间中部署作业的整个过程。
确保您有 Kubernetes 集群(例如 GKE、EKS、AKS 或 Minikube)并配置 kubectl 以连接到它。
要保持此部署隔离,请创建一个名为 my-lab 的命名空间:
kubectl create namespace my-lab kubectl get ns my-lab
创建 mongo-pv.yaml 文件来定义 MongoDB 数据的持久卷:
apiVersion: v1 kind: PersistentVolume metadata: name: mongo-pv namespace: my-lab spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: path: /data/mongo
应用PV:
kubectl apply -f mongo-pv.yaml
在 mongo-pvc.yaml 中定义持久卷声明:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mongo-pvc namespace: my-lab spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi
应用 PVC:
kubectl apply -f mongo-pvc.yaml
在 mongo-deployment.yaml 中定义 MongoDB 部署和服务:
apiVersion: apps/v1 kind: Deployment metadata: name: mongo namespace: my-lab spec: replicas: 1 selector: matchLabels: app: mongo template: metadata: labels: app: mongo spec: containers: - name: mongo image: mongo:latest ports: - containerPort: 27017 env: - name: MONGO_INITDB_ROOT_USERNAME value: "root" - name: MONGO_INITDB_ROOT_PASSWORD value: "password" volumeMounts: - name: mongo-storage mountPath: /data/db volumes: - name: mongo-storage persistentVolumeClaim: claimName: mongo-pvc --- apiVersion: v1 kind: Service metadata: name: mongo namespace: my-lab spec: type: ClusterIP ports: - port: 27017 targetPort: 27017 selector: app: mongo
应用部署:
kubectl apply -f mongo-deployment.yaml
通过连接来验证 MongoDB 部署:
kubectl exec -it <mongo-pod-name> -n my-lab -- mongosh -u root -p password
缩减并备份 MongoDB 部署以确保数据持续存在:
kubectl scale deployment mongo --replicas=0 -n my-lab kubectl scale deployment mongo --replicas=1 -n my-lab
使用 Python,定义一个脚本来创建集合并用随机文档填充它们:
import random import string import pymongo from pymongo import MongoClient def random_string(length=10): return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) def create_collections_and_populate(db_name='mydatabase', collections_count=100, documents_per_collection=1_000_000): client = MongoClient('mongodb://root:password@mongo:27017/') db = client[db_name] for i in range(collections_count): collection_name = f'collection_{i+1}' collection = db[collection_name] print(f'Creating collection: {collection_name}') bulk_data = [{'name': random_string(), 'value': random.randint(1, 100)} for _ in range(documents_per_collection)] collection.insert_many(bulk_data) print(f'Inserted {documents_per_collection} documents into {collection_name}') if __name__ == "__main__": create_collections_and_populate()
创建一个 Dockerfile 来容器化 Python 脚本:
FROM python:3.9-slim WORKDIR /app COPY mongo_populator.py . RUN pip install pymongo CMD ["python", "mongo_populator.py"]
构建镜像并将其推送到容器注册表:
docker build -t <your-docker-repo>/mongo-populator:latest . docker push <your-docker-repo>/mongo-populator:latest
在 mongo-populator-job.yaml 中定义一个作业来运行集合生成脚本:
apiVersion: batch/v1 kind: Job metadata: name: mongo-populator namespace: my-lab spec: template: spec: containers: - name: mongo-populator image: <your-docker-repo>/mongo-populator:latest env: - name: MONGO_URI value: "mongodb://root:password@mongo:27017/" restartPolicy: Never backoffLimit: 4
申请工作:
kubectl apply -f mongo-populator-job.yaml
作业完成后,连接到 MongoDB 以检查数据:
kubectl exec -it <mongo-pod-name> -n my-lab -- mongosh -u root -p password
在 MongoDB 中:
use mydatabase show collections db.collection_9.find().limit(5).pretty() db.getCollectionNames().forEach(function(collection) { var count = db[collection].countDocuments(); print(collection + ": " + count + " documents"); });
每个集合应包含 100 万个文档,确认数据生成作业成功。
以上是在 Kubernetes 上部署 MongoDB 集合生成器的详细内容。更多信息请关注PHP中文网其他相关文章!