Longhorn 云原生容器分布式存储 - Python Client

内容来源于官方 Longhorn 1.1.2 英文技术手册。

系列

目前,您可以使用 Longhorn UI 操作 Longhorn。同时,您可以使用 Python 访问 Longhorn API,如下所示。

  1. 获取 Longhorn API 端点

    Longhorn 通信的一种方式是通过 longhorn-frontend service。

    如果您在安装 Longhorn 的同一集群中运行自动化/脚本(automation/scripting)工具,请连接到端点 http://longhorn-frontend.longhorn-system/v1

    如果您在本地机器上运行自动化/脚本(automation/scripting)工具,请使用 kubectl port-forwardlonghorn-frontend service 转发到 localhost

    kubectl port-forward services/longhorn-frontend 8080:http -n longhorn-system

    并连接到端点 http://localhost:8080/v1

  2. 使用 Python Client

    longhorn.py 文件(包含 Python client)导入到以下 Python 脚本中,并从 API 端点创建一个 client

    https://github.com/longhorn/longhorn-tests/blob/master/manager/integration/tests/longhorn.py

    import longhorn
    
    # If automation/scripting tool is inside the same cluster in which Longhorn is installed
    longhorn_url = 'http://longhorn-frontend.longhorn-system/v1'
    # If forwarding `longhorn-frontend` service to localhost
    longhorn_url = 'http://localhost:8080/v1'
    
    client = longhorn.Client(url=longhorn_url)
    
    # Volume operations
    # List all volumes
    volumes = client.list_volume()
    # Get volume by NAME/ID
    testvol1 = client.by_id_volume(id="testvol1")
    # Attach TESTVOL1
    testvol1 = testvol1.attach(hostId="worker-1")
    # Detach TESTVOL1
    testvol1.detach()
    # Create a snapshot of TESTVOL1 with NAME
    snapshot1 = testvol1.snapshotCreate(name="snapshot1")
    # Create a backup from a snapshot NAME
    testvol1.snapshotBackup(name=snapshot1.name)
    # Update the number of replicas of TESTVOL1
    testvol1.updateReplicaCount(replicaCount=2)
    # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
    
    # Node operations
    # List all nodes
    nodes = client.list_node()
    # Get node by NAME/ID
    node1 = client.by_id_node(id="worker-1")
    # Disable scheduling for NODE1
    client.update(node1, allowScheduling=False)
    # Enable scheduling for NODE1
    client.update(node1, allowScheduling=True)
    # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
    
    # Setting operations
    # List all settings
    settings = client.list_setting()
    # Get setting by NAME/ID
    backupTargetsetting = client.by_id_setting(id="backup-target")
    # Update a setting
    backupTargetsetting = client.update(backupTargetsetting, value="s3://backupbucket@us-east-1/")
    # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
公众号:黑客下午茶
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值