9장. Ceph 블록 장치 Python 모듈 사용
rbd
python 모듈은 Ceph 블록 장치 이미지에 대해 파일과 유사한 액세스를 제공합니다. 기본 제공 툴을 사용하려면 rbd
및 rados
Python 모듈을 가져옵니다.
사전 요구 사항
- 실행 중인 Red Hat Ceph Storage 클러스터.
- 노드에 대한 루트 수준 액세스.
절차
RADOS에 연결하고 IO 컨텍스트를 엽니다.
cluster = rados.Rados(conffile='my_ceph.conf') cluster.connect() ioctx = cluster.open_ioctx('mypool')
이미지를 생성하는 데 사용하는
:class:rbd.RBD
오브젝트를 인스턴스화합니다.rbd_inst = rbd.RBD() size = 4 * 1024**3 # 4 GiB rbd_inst.create(ioctx, 'myimage', size)
이미지에서 I/O를 수행하려면
:class:rbd.Image 오브젝트를 인스턴스화합니다.
image = rbd.Image(ioctx, 'myimage') data = 'foo' * 200 image.write(data, 0)
이렇게 하면 이미지의 처음 600바이트에 'foo'가 기록됩니다. 데이터는
:type:unicode
일 수 없습니다.librbd
는:c:type:char
보다 넓은 문자를 처리하는 방법을 알지 못합니다.이미지를 닫고 IO 컨텍스트 및 RADOS에 대한 연결을 종료합니다.
image.close() ioctx.close() cluster.shutdown()
안전하게 하려면 이러한 호출 각각이 별도의
:finally
블록에 있어야 합니다.import rados import rbd cluster = rados.Rados(conffile='my_ceph_conf') try: ioctx = cluster.open_ioctx('my_pool') try: rbd_inst = rbd.RBD() size = 4 * 1024**3 # 4 GiB rbd_inst.create(ioctx, 'myimage', size) image = rbd.Image(ioctx, 'myimage') try: data = 'foo' * 200 image.write(data, 0) finally: image.close() finally: ioctx.close() finally: cluster.shutdown()
이 작업은 번거로울 수 있으므로 Rados,Ioctx 및 Image 클래스를 자동으로 닫거나 종료하는 컨텍스트 관리자로 사용할 수 있습니다. 이를 컨텍스트 관리자로 사용하면 위의 예가 됩니다.
with rados.Rados(conffile='my_ceph.conf') as cluster: with cluster.open_ioctx('mypool') as ioctx: rbd_inst = rbd.RBD() size = 4 * 1024**3 # 4 GiB rbd_inst.create(ioctx, 'myimage', size) with rbd.Image(ioctx, 'myimage') as image: data = 'foo' * 200 image.write(data, 0)