运行 mongo shell 使用 kubernetes 作业的脚本

Running mongo shell script using kubernetes job

我需要创建 kubernetes 作业,它将 运行 低于 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);

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

您可以使用 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);