客户端通过Rados模块控制ceph存储集群

来源:互联网 发布:黄金比例算法 编辑:程序博客网 时间:2024/04/30 13:16

环境:
客户端: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,syscluster = 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,syscluster = 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 atioctx.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:    ObjectIteratorObjectIterator.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()

本文出自“he ivy ”的博客,转载请务必保留此出处:http://blog.csdn.net/heivy/article/details/50560585

0 0
原创粉丝点击