kubernetes 如何通过编程将文件从k8 pod复制到GCP存储器?

bbuxkriu  于 2023-01-16  发布在  Kubernetes
关注(0)|答案(2)|浏览(189)

我正在将我的springboot应用程序映像部署到GCP。我的springboot应用程序处理数据并创建一个文件。我想将此文件复制到GCP存储。能否从Kubernetes pod到GCP存储实现此操作?

jchrr9hc

jchrr9hc1#

您可以使用GCSFUSE将GCS存储桶作为卷装载到Kubernetes Pod。
以下是用于安装和卸载GCS铲斗的集装箱生命周期挂钩示例。

#...

securityContext:
  privileged: true
  capabilities:
    add:
      - SYS_ADMIN
lifecycle:
  postStart:
    exec:
      command: ["gcsfuse", "-o", "nonempty,allow_other", "bucket-name", "/usr/share/nginx/bucket-data"]
  preStop:
    exec:
      command: ["fusermount", "-u", "/usr/share/nginx/bucket-data"]

#...

参考:MOUNTING GOOGLE CLOUD STORAGE BUCKET TO KUBERNETES POD
要将文件从Pod复制到GCS存储桶,请使用Fabric8 Kubernetes Clienthere中对此进行了详细描述。
示例代码:

public class DownloadFileFromPod {
    public static void main(String[] args) {
        try (KubernetesClient client = new KubernetesClientBuilder().build()) {
            // Path Where to copy file to cloud storage
            Path downloadToPath = new File("path_of_gcs").toPath();
            // Using Kubernetes Client to copy file from pod
            client.pods()
                    .inNamespace("default")                         // <- Namespace of pod
                    .withName("quarkus-84dc4885b-tsck6")            // <- Name of pod
                    .file("/deployments/quarkus-1.0.0-runner.jar")  // <- Path of file inside pod
                    .copy(downloadToPath);                            // <- path where to copy downloaded file
        }
    }
}

您也可以使用Cloud Storage client libraries以编程方式实现这一点。
下面是使用java上传文件的示例

import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class UploadObject {
  public static void uploadObject(
      String projectId, String bucketName, String objectName, String filePath) throws IOException {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    // The ID of your GCS object
    // String objectName = "your-object-name";

    // The path to your file to upload
    // String filePath = "path/to/your/file"

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    BlobId blobId = BlobId.of(bucketName, objectName);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();

    // Optional: set a generation-match precondition to avoid potential race
    // conditions and data corruptions. The request returns a 412 error if the
    // preconditions are not met.
    Storage.BlobTargetOption precondition;
    if (storage.get(bucketName, objectName) == null) {
      // For a target object that does not yet exist, set the DoesNotExist precondition.
      // This will cause the request to fail if the object is created before the request runs.
      precondition = Storage.BlobTargetOption.doesNotExist();
    } else {
      // If the destination already exists in your bucket, instead set a generation-match
      // precondition. This will cause the request to fail if the existing object's generation
      // changes before the request runs.
      precondition =
          Storage.BlobTargetOption.generationMatch(
              storage.get(bucketName, objectName).getGeneration());
    }
    storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition);

    System.out.println(
        "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
  }
}

另外,请查看this thread with python示例以了解更多细节。

ubbxdtey

ubbxdtey2#

是的,你可以,你需要在你的springboot应用程序镜像中安装gsutil,然后在那个容器中作为configmap或secret挂载你的存储令牌。
最后,您可以像这样运行gsutil

gsutil cp source_file gs://my-bucket

相关问题