Kyle Pericak

"It works in my environment."

Mon 03 February 2020

Python Ceph Examples

Posted by Kyle Pericak in development   

This post covers some of the ways you can use Ceph from Python

The two libraries used here:


Deploy ceph.conf

Grab the ceph.conf file from your cluster or make a new valid one and place it on the filesystem of the server which will execute the Python code.

Also copy the keyring over. Check here if you want to use anything other than the admin keyring.

Install Dependencies

Ceph doesn't distribute a pure python library. Instead it seems to rely on some C stuff that needs to be installed, so you need to use apt instead of pip. This is a pain, since so far as I can tell you can't isolate these dependencies inside a virtual environment.

apt-get install python-rados


Be sure that the rados and rbd libraries, and ceph.conf are deployed first.

Instantiate rados and rbd instances

The cluster and rbd_inst variables will be used through these examples. The rbd_inst methods will expect ioctx as their first argument.

import rbd
import rados

cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')

pool_name = 'volumes'
ioctx = cluster.open_ioctx(pool_name)
rbd_inst = rbd.RBD()

Usage examples

# Get cluster stats
# Returns dict with kb, num_objects, kb_avail, and kb_used

# Get cluster health by issuing a command to ceph's monitor
cmd = {"prefix":"status", "format":"json"}
## mon_command returns a tuple of (return code, data/buffer, errors)
command_result = cluster.mon_command(json.dumps(cmd), b'', timeout=5)
status = json.loads(command_result[1])

# Get Pool usage stats
# Returns num_objects_degraded, num_kb, num_wr, num_rd, num_objects_unfound,
#   num_objects_missing_on_primary, num_object_clones, num_objects,
#   num_object_copies, num_bytes, num_rd_kb, num_wr_kb

# List pools

# Create pool
pool_name = 'test_pool'

# Delete pool

# List rados block devices

# Create block device
rbd_name = 'sample-device'
size_gb = 10
rbd_inst.create(ioctx, rbd_name, size_gb)

# Query a block device (image)
image = rbd.Image(ioctx, rbd_name)

# Resize image

# Create a snapshot
snap_name = 'sample-snap'

# List snaps of image

# Revert to snapshot
snap_name = 'sample-snap'

# Delete a snapshot

# Close an open image (when you're done working with it)

# Delete an image/volume. Must not have snapshots. Must be closed.
rbd_inst.remove(ioctx, rbd_name)

Javascript appears to be disabled. Comments can't load.