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.
Get Longhorn endpoint
One way to communicate with Longhorn is through
longhorn-frontendservice.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/v1If you run your automation/scripting tool on your local machine, use
kubectl port-forwardto forward thelonghorn-frontendservice to localhost:kubectl port-forward services/longhorn-frontend 8080:http -n longhorn-system
and connect to endpoint
http://localhost:8080/v1Using Python Client
Import file longhorn.py which contains the Python client into your Python script and create a client from the endpoint:
import longhorn# If automation/scripting tool is inside the same cluster in which Longhorn is installedlonghorn_url = 'http://longhorn-frontend.longhorn-system/v1'# If forwarding `longhorn-frontend` service to localhostlonghorn_url = 'http://localhost:8080/v1'client = longhorn.Client(url=longhorn_url)# Volume operations# List all volumesvolumes = client.list_volume()# Get volume by NAME/IDtestvol1 = client.by_id_volume(id="testvol1")# Attach TESTVOL1testvol1 = testvol1.attach(hostId="worker-1")# Detach TESTVOL1testvol1.detach()# Create a snapshot of TESTVOL1 with NAMEsnapshot1 = testvol1.snapshotCreate(name="snapshot1")# Create a backup from a snapshot NAMEtestvol1.snapshotBackup(name=snapshot1.name)# Update the number of replicas of TESTVOL1testvol1.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 nodesnodes = client.list_node()# Get node by NAME/IDnode1 = client.by_id_node(id="worker-1")# Disable scheduling for NODE1client.update(node1, allowScheduling=False)# Enable scheduling for NODE1client.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 settingssettings = client.list_setting()# Get setting by NAME/IDbackupTargetsetting = client.by_id_setting(id="backup-target")# Update a settingbackupTargetsetting = 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