【问题标题】:Running mongo shell script using kubernetes job使用 kubernetes 作业运行 mongo shell 脚本
【发布时间】:2022-02-23 17:36:38
【问题描述】:

我需要创建将在 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);

我需要一份这份工作的样板。我应该创建持久卷并声明它,还是有可能在没有持久卷的情况下运行此作业?我需要运行一次,然后删除它。

【问题讨论】:

    标签: mongodb shell kubernetes jobs


    【解决方案1】:

    您可以使用configMap 更轻松地解决它,然后将configMap 挂载为将在文件中解析的卷。

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

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

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

      你可以通过运行来检查你的文件是否存储在里面:

      $ kubectl describe cm mongoscript-cm
      
    2. 从 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. 检查它在 pod 内的外观

      连接到 pod:

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

      检查文件出现:

      # ls /opt
      mongoscript.js
      

      检查其内容:

      # 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);
      

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2020-03-01
      • 2019-06-19
      • 2016-11-07
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多