使用 kubernetes 作业运行 mongo shell 脚本

发布于 2025-01-09 10:39:48 字数 1073 浏览 0 评论 0原文

我需要创建将在 mongo shell 上运行以下脚本的 kubernetes 作业:

var operations = [];
db.product.find().forEach(function(doc) {
    var documentLink = doc.documentLink; 
    var operation = { updateMany :{ 
"filter" : {"_id" : doc._id},
"update" : {$set:{"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
    $unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
    operations.push(operation); 
});
operations.push( {
    ordered: true,      
    writeConcern: { w: "majority", wtimeout: 5000 } 
});
db.product.bulkWrite(operations);

我需要该作业的示例。我应该创建持久卷并声明其所有权,还是可以在没有持久卷的情况下运行此作业?我需要运行一次然后将其删除。

I need to create kubernetes job that will run below script on mongo shell:

var operations = [];
db.product.find().forEach(function(doc) {
    var documentLink = doc.documentLink; 
    var operation = { updateMany :{ 
"filter" : {"_id" : doc._id},
"update" : {$set:{"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
    $unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
    operations.push(operation); 
});
operations.push( {
    ordered: true,      
    writeConcern: { w: "majority", wtimeout: 5000 } 
});
db.product.bulkWrite(operations);

I will need a sample of how that job will look like. Should I create presistent volume and claim to it or is there possibility to run this job without persistent volume? I need to run this once and then remove it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2025-01-16 10:39:48

您可以使用 configMap,然后将 configMap 安装为将在文件中解析的卷。

下面是如何继续操作的示例(注意!您需要为其使用正确的图像以及 mongo shell 工作方式的一些其他更改):

  1. 从文件创建一个 configMap。可以通过运行以下命令来完成:

    $ kubectl create cm mongoscript-cm --from-file=mongoscript.js
    已创建 configmap/mongoscript-cm
    

    您可以通过运行以下命令来检查您的文件是否存储在其中:

    $ kubectl 描述 cm mongoscript-cm
    
  2. 从 configmap 创建一个带有卷挂载的作业 (规范模板与 pod 中使用的相同):

    api版本:batch/v1
    种类:工作
    元数据:
      名称: mongojob
    规格:
      模板:
        规格:
          容器:
          - 名称:mongojob
            image: ubuntu # 出于测试目的,您需要使用合适的
            命令:['bin/bash', '-c', 'echo STARTED ;猫 /opt/mongoscript.js ;睡眠 120 ; echo FINISHED'] # 命令相同,用于演示目的
            体积安装:
            - 名称:mongoscript
              mountPath: /opt # 文件挂载位置
          卷:
          - 名称:mongoscript
            配置映射:
              name: mongoscript-cm # 引用之前创建的configmap
          restartPolicy: OnFailure # 作业所需
    
  3. 检查它在 Pod 内部的外观

    连接到 Pod:

    $ kubectl exec -it mongojob--1-8w4ml -- /bin/bash
    

    检查文件已显示:

    <前><代码># ls /opt
    mongoscript.js

    检查其内容:

    # cat /opt/mongoscript.js 
    var 操作 = [];
    db.product.find().forEach(函数(doc) {
        var documentLink = doc.documentLink; 
        var 操作 = { updateMany :{ 
    “过滤器”:{“_id”:doc._id},
    “更新”:{$set {“documentLinkMap.en”:documentLink,“documentLinkMap.de”:“”},
        $未设置: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr ":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":""," TechnicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","版本.$[].descriptionMap.tr":"","版本.$[].r eleaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""} }}};
        操作.push(操作); 
    });
    操作.push( {
        订购:真实,      
        writeConcern: { w: "多数", wtimeout: 5000 } 
    });
    db.product.bulkWrite(操作);
    

You can solve it much easier with configMap and then mount the configMap as a volume which will be resolved in a file.

Below is example how to proceed with it (Note! You will need to use a proper image for it as well as some other changes how mongo shell works):

  1. Create a configMap from file. Can be done by running this command:

    $ kubectl create cm mongoscript-cm --from-file=mongoscript.js
    configmap/mongoscript-cm created
    

    You can check that you file is stored inside by running:

    $ kubectl describe cm mongoscript-cm
    
  2. Create a job with volume mount from configmap (spec template is the same as it used in pods):

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: mongojob
    spec:
      template:
        spec:
          containers:
          - name: mongojob
            image: ubuntu # for testing purposes, you need to use appropriate one
            command: ['bin/bash', '-c', 'echo STARTED ; cat /opt/mongoscript.js ; sleep 120 ; echo FINISHED'] # same for command, that's for demo purposes
            volumeMounts:
            - name: mongoscript
              mountPath: /opt # where to mount the file
          volumes:
          - name: mongoscript
            configMap:
              name: mongoscript-cm # reference to previously created configmap
          restartPolicy: OnFailure # required for jobs
    
  3. Checking how it looks inside the pod

    Connect to the pod:

    $ kubectl exec -it mongojob--1-8w4ml -- /bin/bash
    

    Check file is presented:

    # ls /opt
    mongoscript.js
    

    Check its content:

    # cat /opt/mongoscript.js 
    var operations = [];
    db.product.find().forEach(function(doc) {
        var documentLink = doc.documentLink; 
        var operation = { updateMany :{ 
    "filter" : {"_id" : doc._id},
    "update" : {$set {"documentLinkMap.en":documentLink,"documentLinkMap.de":""},
        $unset: {documentLink:"","descriptionMap.tr":"","news.tr":"","descriptionInternal.tr":"","salesDescription.tr":"","salesInternal.tr":"","deliveryDescription.tr":"","deliveryInternal.tr":"","productRoadMapDescription.tr":"","productRoadMapInternal.tr":"","technicalsAndIntegration.tr":"","technicalsAndIntegrationInternal.tr":"","versions.$[].descriptionMap.tr":"","versions.$[].releaseNoteMap.tr":"","versions.$[].artifacts.$[].descriptionMap.tr":"","versions.$[].artifacts.$[].artifactNotes.tr":""}}}};
        operations.push(operation); 
    });
    operations.push( {
        ordered: true,      
        writeConcern: { w: "majority", wtimeout: 5000 } 
    });
    db.product.bulkWrite(operations);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文