1. 程式人生 > >客戶端通過Rados模組控制ceph儲存叢集

客戶端通過Rados模組控制ceph儲存叢集

環境:
客戶端:Ubuntu12.04
叢集:Ubuntu14.04
ceph : 0.94.2 (5fb85614ca8f354284c713a2f9c610860720bbf3)

客戶端安裝python-ceph

1、在客戶端執行 sudo apt-get install python-ceph

2、把叢集的配置檔案和clint.admin.keyring檔案拷貝到客戶端如/etc/ceph/資料夾下

直接將node1節點下的/etc/ceph資料夾拷貝過來。

連結到測試叢集

1/建一個python檔案,如:client.py

2/呼叫rados.py模組:import rados

3/在連結叢集之前,需要建立一個叢集handle,叢集名一般取ceph,這裡使用cluster,預設使用的是client.admin這個keyring。

import rados,sys
cluster = rados.Rados(conffile = '/path/to/ceph.conf') #這裡是/etc/ceph/ceph.conf

也可以使用其他的keyring,告訴cluster去哪裡取這個keyring。

cluster = rados.Rados(conffile = '/path/to/ceph.conf',keyring = '/path/to/keyring')
   #預設的是ceph.client.admin.keyring,可以不給出

連結到叢集

import rados,sys

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

cluster.connect()

函式呼叫例項

在連結到叢集的基礎上

無引數

獲取叢集的fsid:

cluster.get_fsid()

叢集狀態:

cluster.get_cluster_stats()

叢集版本:

cluster.version()

pool 操作

列出所有池的名稱:

pools = cluster.list_pools()

for pool in pools:

    print pool

新建池:

cluster.create_pool('poolname')  #poolname 是字串,要加引號

檢視poolname池是否存在:

cluster.pool_exists('poolname')  #poolname 是字串,要加引號;返回值是True和False

刪除池:

cluster.delete_pool('poolname')

輸入輸出內容

讀出、寫入到ceph的儲存叢集需要 input/output context (ioctx),呼叫Rados的open_ioctx()來新建一個ioctx,引數是打算使用的池的名稱

ioctx = cluster.open_ioctx('poolname')

操作完成後,要關閉ioctx:

ioctx.close()

同步操作示例:

#Writing object 'hw' with contents 'Hello World!' to pool 'data'."
ioctx.write_full("hw", "Hello World!")

#Contents of object 'hw'
print ioctx.read("hw",length=int,offset=int) #length 預設是物件長度,offset預設是0

#Removing object 'hw'"
ioctx.remove_object("hw")

非同步操作示例:

ioctx.aio_write(oject_name,write_data,offset=**,oncomplete=None,onsafe=None)

        offset(int):byte offset in the object to begin writing at

ioctx.aio_write_full(object_name, write_data, oncomplete=None, onsafe=None)

ioctx.aio_append(object_name, append-data, oncomplete=None, onsafe=None)

ioctx.aio_read(object_name, length, offset, oncomplete)

當我們新建一個物件,我們可以給物件寫擴充套件屬性(XATTRs),讀物件的(XATTRs)

#Writing XATTR 'lang' with value 'en_US' to object 'hw'
ioctx.set_xattr("hw", "lang", "en_US")    #寫

#Getting XATTR 'lang' from object 'hw'
print ioctx.get_xattr("hw", "lang")       #讀

列出ioctx所在池的所有物件:

object_iterator = ioctx.list_objects()

while True :

    try :
            rados_object = object_iterator.next()
            print "Object contents = " + rados_object.read()

    except StopIteration :
            break

檢視ioctx所在池使用資訊:

ioctx.get_stats()


    Returns:    dict - contains the following keys:
            num_bytes (int) - size of pool in bytes
            num_kb (int) - size of pool in kbytes
            num_objects (int) - number of objects in the pool
            num_object_clones (int) - number of object clones
            num_object_copies (int) - number of object copies
            num_objects_missing_on_primary (int) - number of objets
            missing on primary
            num_objects_unfound (int) - number of unfound objects
            num_objects_degraded (int) - number of degraded objects
            num_rd (int) - bytes read
            num_rd_kb (int) - kbytes read
            num_wr (int) - bytes written
            num_wr_kb (int) - kbytes written

修改io context相關的auid “owner”:

ioctx.change_auid(auid)

    需要你有現在和新auid的寫許可權

對ceph配置的操作

讀取配置資訊:

如果已經連線到叢集cluster:

    cluster.conf_get('configuration option') 
    #引數是ceph的所有配置項,比如ceph.conf檔案裡面global部分等號前面的內容,都可以做引數

連線到叢集cluster,使用Rados模組呼叫:

    rados.Rados.conf_get(cluster_name,'configuration option') 
    #使用Rados模組時,一定要給出叢集名稱。

官方文件裡所有使用Rados模組的都需要再加一個引數(叢集名稱),而且放在第一個引數的位置。或者參考官方文件,把Rados替換為cluster_name(比如本文中的cluster)。

修改配置資訊:

Rados.conf_set(cluster_name,'configuration option','value')  
#value 是所要修改的配置的新內容,是字串的格式,要加引號,

配置cluster handle 通過配置檔案:

Rados.conf_read_file(cluster_name,'/path/to/the/config/file')

檢視librados C 庫的版本

Rados.version(cluster_name)

連線叢集

Rados.connect(cluster_name)

Rados.shutdown(cluster_name)

Rados.get_fsid(cluster_name)

獲取叢集已用資訊:

Rados.get_cluster_stats(cluster_name)

檢查Rados 物件是否在特殊狀態:

Rados.require_state(cluster,"*args")

物件操作

Ioctx.list_objects()
    Get ObjectIterator on rados.Ioctx object.
    Returns:    ObjectIterator
ObjectIterator.next()¶
Object.read(length = 1024*1024)
Object.write(string_to_write)
Object.get_xattrs()
Object.get_xattr(xattr_name)
Object.set_xattr(xattr_name, xattr_value)
Object.rm_xattr(xattr_name)
Object.stat()
Object.remove()