本文档采用自动化机器翻译技术翻译。 尽管我们力求提供准确的译文,但不对翻译内容的完整性、准确性或可靠性作出任何保证。 若出现任何内容不一致情况,请以原始 英文 版本为准,且原始英文版本为权威文本。

这是尚未发布的文档。 SUSE® Storage 1.12 (Dev).

Python客户端

您可以使用Python绑定访问Longhorn API。

  1. 获取Longhorn端点

    与Longhorn通信的一种方式是通过`longhorn-frontend`服务。

    如果您在安装了Longhorn的同一集群内运行自动化/脚本,请连接到端点`http://longhorn-frontend.longhorn-system/v1`。

    如果您在本地计算机上运行自动化/脚本,请使用`kubectl port-forward`将`longhorn-frontend`服务转发到localhost:

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

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

  2. 使用Python客户端

    将包含Python客户端的文件 longhorn.py导入到您的Python脚本中,并从端点创建客户端:

     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