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)