建立一個實用程式來產生 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中文網其他相關文章!