Python Client

    Currently, you can operate Longhorn using Longhorn UI. We are planning to build a dedicated Longhorn CLI in the upcoming releases.

    In the meantime, you can access Longhorn API using Python binding, as we demonstrated below.

    1. Get Longhorn endpoint

      One way to communicate with Longhorn is through longhorn-frontend service.

      If you run your automation/scripting tool inside the same cluster in which Longhorn is installed, connect to the endpoint http://longhorn-frontend.longhorn-system/v1

      If you run your automation/scripting tool on your local machine, use kubectl port-forward to forward the longhorn-frontend service to localhost:

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

      and connect to endpoint http://localhost:8080/v1

    2. Using Python Client

      Import file longhorn.py which contains the Python client into your Python script and create a client from the endpoint:

      1. import longhorn
      2. # If automation/scripting tool is inside the same cluster in which Longhorn is installed
      3. longhorn_url = 'http://longhorn-frontend.longhorn-system/v1'
      4. # If forwarding `longhorn-frontend` service to localhost
      5. longhorn_url = 'http://localhost:8080/v1'
      6. client = longhorn.Client(url=longhorn_url)
      7. # Volume operations
      8. # List all volumes
      9. volumes = client.list_volume()
      10. # Get volume by NAME/ID
      11. testvol1 = client.by_id_volume(id="testvol1")
      12. # Attach TESTVOL1
      13. testvol1 = testvol1.attach(hostId="worker-1")
      14. # Detach TESTVOL1
      15. testvol1.detach()
      16. # Create a snapshot of TESTVOL1 with NAME
      17. snapshot1 = testvol1.snapshotCreate(name="snapshot1")
      18. # Create a backup from a snapshot NAME
      19. testvol1.snapshotBackup(name=snapshot1.name)
      20. # Update the number of replicas of TESTVOL1
      21. testvol1.updateReplicaCount(replicaCount=2)
      22. # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
      23. # Node operations
      24. # List all nodes
      25. nodes = client.list_node()
      26. # Get node by NAME/ID
      27. node1 = client.by_id_node(id="worker-1")
      28. # Disable scheduling for NODE1
      29. client.update(node1, allowScheduling=False)
      30. # Enable scheduling for NODE1
      31. client.update(node1, allowScheduling=True)
      32. # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
      33. # Setting operations
      34. # List all settings
      35. settings = client.list_setting()
      36. # Get setting by NAME/ID
      37. backupTargetsetting = client.by_id_setting(id="backup-target")
      38. # Update a setting
      39. backupTargetsetting = client.update(backupTargetsetting, value="s3://backupbucket@us-east-1/")
      40. # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests

    © 2019-2023 Longhorn Authors | Documentation Distributed under CC-BY-4.0

    © 2023 The Linux Foundation. All rights reserved. The Linux Foundation has registered trademarks and uses trademarks. For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.