繁体   English   中英

我可以使用 k8s python 客户端拍摄卷快照吗?

[英]Can I take a volume snapshot with the k8s python client?

在 python k8s 客户端中,我使用以下代码

yaml 文件

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
    name: test-snapshot
    namespace: test
spec:
    volumeSnapshotClassName: snapshotclass
source:
    persistentVolumeClaimName: test-pvc

python代码

res = utils.create_from_dict(k8s_client, yaml_file)

但是,我收到了这条消息:

AttributeError: module 'kubernetes.client' has no attribute 'SnapshotStorageV1Api'

我想在 k8s 中进行卷快照。 我怎样才能做到这一点?

请给我一些建议!

正如我在问题下发表的部分评论中指出的那样:

你见过这个 github 问题评论: github.com/kubernetes-client/python/issues /...?

评论中发布的链接是 github 问题:

  • VolumeSnapshot
  • VolumeSnapshotClass
  • VolumeSnapshotContent

支持 Kubernetes python 客户端。

引用用户@roycaihw 在此 github 问题中发表的评论,解释了如何制作快照的方法:

看起来这些 API 是 CRD: https://kubernetes.io/docs/concepts/storage/volume-snapshot-classes/#the-volumesnapshotclass-resource

如果是这种情况,您可以在安装后使用 CustomObject API 向这些 API 发送请求。 示例: https://github.com/kubernetes-client/python/blob/master/examples/custom_object.py

-- Github.com:Kubernetes 客户端:ZA7F5F35426B927411FC9231B 5685 评论:1932


例子

下面是一个可以生成VolumeSnapshot的 Python 代码示例:

from kubernetes import client, config


def main():
    config.load_kube_config()

    api = client.CustomObjectsApi()

    # it's my custom resource defined as Dict
    my_resource = {
        "apiVersion": "snapshot.storage.k8s.io/v1beta1",
        "kind": "VolumeSnapshot",
        "metadata": {"name": "python-snapshot"},
        "spec": {
            "volumeSnapshotClassName": "example-snapshot-class",
            "source": {"persistentVolumeClaimName": "example-pvc"}
                }
    }

    # create the resource
    api.create_namespaced_custom_object(
        group="snapshot.storage.k8s.io",
        version="v1beta1",
        namespace="default",
        plural="volumesnapshots",
        body=my_resource,
    )

if __name__ == "__main__":
    main()

请更改此代码中的值以支持您的特定设置(即apiVersion.metadata.name.metadata.namespace等)。

旁注!

此 Python 代码已使用GKE及其 gce gce-pd-csi-driver进行了测试。

运行此代码后,应创建VolumeSnapshot

  • $ kubectl get volumesnapshots
NAME               AGE
python-snapshot    19m
  • $ kubectl get volumesnapshotcontents
NAME                                               AGE
snapcontent-71380980-6d91-45dc-ab13-4b9f42f7e7f2   19m

其他资源:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM