php librados,librados 简介

第二步:配置集群句柄¶

A Ceph Client, via librados, interacts directly with OSDs to store

and retrieve data. To interact with OSDs, the client app must invoke

librados and connect to a Ceph Monitor. Once connected, librados

retrieves the Cluster Map from the Ceph Monitor. When the client app

wants to read or write data, it creates an I/O context and binds to a

pool. The pool has an associated ruleset that defines how it

will place data in the storage cluster. Via the I/O context, the client

provides the object name to librados, which takes the object name

and the cluster map (i.e., the topology of the cluster) and computes the

placement group and OSD for locating the data. Then the client application

can read or write data. The client app doesn’t need to learn about the topology

of the cluster directly.

23319d156826b61ae9944bfb046ce2b1.png

The Ceph Storage Cluster handle encapsulates the client configuration, including:

The user ID for rados_create() or user name for rados_create2()

(preferred).

The cephx authentication key

The monitor ID and IP address

Logging levels

Debugging levels

Thus, the first steps in using the cluster from your app are to 1) create

a cluster handle that your app will use to connect to the storage cluster,

and then 2) use that handle to connect. To connect to the cluster, the

app must supply a monitor address, a username and an authentication key

(cephx is enabled by default).

Tip

Talking to different Ceph Storage Clusters – or to the same cluster

with different users – requires different cluster handles.

RADOS provides a number of ways for you to set the required values. For

the monitor and encryption key settings, an easy way to handle them is to ensure

that your Ceph configuration file contains a keyring path to a keyring file

and at least one monitor address (e.g,. mon host). For example:

[global]

mon host = 192.168.1.1

keyring = /etc/ceph/ceph.client.admin.keyring

Once you create the handle, you can read a Ceph configuration file to configure

the handle. You can also pass arguments to your app and parse them with the

function for parsing command line arguments (e.g., rados_conf_parse_argv()),

or parse Ceph environment variables (e.g., rados_conf_parse_env()). Some

wrappers may not implement convenience methods, so you may need to implement

these capabilities. The following diagram provides a high-level flow for the

initial connection.

864c9dfa76515079f0c8efabcc9051d2.png

Once connected, your app can invoke functions that affect the whole cluster

with only the cluster handle. For example, once you have a cluster

handle, you can:

Get cluster statistics

Use Pool Operation (exists, create, list, delete)

Get and set the configuration

One of the powerful features of Ceph is the ability to bind to different pools.

Each pool may have a different number of placement groups, object replicas and

replication strategies. For example, a pool could be set up as a “hot” pool that

uses SSDs for frequently used objects or a “cold” pool that uses erasure coding.

The main difference in the various librados bindings is between C and

the object-oriented bindings for C++, Java and Python. The object-oriented

bindings use objects to represent cluster handles, IO Contexts, iterators,

exceptions, etc.

C Example¶

For C, creating a simple cluster handle using the admin user, configuring

it and connecting to the cluster might look something like this:

#include

#include

#include

int main (int argc, char argv**)

{

/* Declare the cluster handle and required arguments. */

rados_t cluster;

char cluster_name[] = "ceph";

char user_name[] = "client.admin";

uint64_t flags;

/* Initialize the cluster handle with the "ceph" cluster name and the "client.admin" user */

int err;

err = rados_create2(&cluster, cluster_name, user_name, flags);

if (err < 0) {

fprintf(stderr, "%s: Couldn't create the cluster handle! %s\n", argv[0], strerror(-err));

exit(EXIT_FAILURE);

} else {

printf("\nCreated a cluster handle.\n");

}

/* Read a Ceph configuration file to configure the cluster handle. */

err = rados_conf_read_file(cluster, "/etc/ceph/ceph.conf");

if (err < 0) {

fprintf(stderr, "%s: cannot read config file: %s\n", argv[0], strerror(-err));

exit(EXIT_FAILURE);

} else {

printf("\nRead the config file.\n");

}

/* Read command line arguments */

err = rados_conf_parse_argv(cluster, argc, argv);

if (err < 0) {

fprintf(stderr, "%s: cannot parse command line arguments: %s\n", argv[0], strerror(-err));

exit(EXIT_FAILURE);

} else {

printf("\nRead the command line arguments.\n");

}

/* Connect to the cluster */

err = rados_connect(cluster);

if (err < 0) {

fprintf(stderr, "%s: cannot connect to cluster: %s\n", argv[0], strerror(-err));

exit(EXIT_FAILURE);

} else {

printf("\nConnected to the cluster.\n");

}

}

Compile your client and link to librados using -lrados. For example:

gcc ceph-client.c -lrados -o ceph-client

C++ Example¶

The Ceph project provides a C++ example in the ceph/examples/librados

directory. For C++, a simple cluster handle using the admin user requires

you to initialize a librados::Rados cluster handle object:

#include

#include

#include

int main(int argc, const char **argv)

{

int ret = 0;

/* Declare the cluster handle and required variables. */

librados::Rados cluster;

char cluster_name[] = "ceph";

char user_name[] = "client.admin";

uint64_t flags;

/* Initialize the cluster handle with the "ceph" cluster name and "client.admin" user */

{

ret = cluster.init2(user_name, cluster_name, flags);

if (ret < 0) {

std::cerr << "Couldn't initialize the cluster handle! error " << ret << std::endl;

ret = EXIT_FAILURE;

return 1;

} else {

std::cout << "Created a cluster handle." << std::endl;

}

}

/* Read a Ceph configuration file to configure the cluster handle. */

{

ret = cluster.conf_read_file("/etc/ceph/ceph.conf");

if (ret < 0) {

std::cerr << "Couldn't read the Ceph configuration file! error " << ret << std::endl;

ret = EXIT_FAILURE;

return 1;

} else {

std::cout << "Read the Ceph configuration file." << std::endl;

}

}

/* Read command line arguments */

{

ret = cluster.conf_parse_argv(argc, argv);

if (ret < 0) {

std::cerr << "Couldn't parse command line options! error " << ret << std::endl;

ret = EXIT_FAILURE;

return 1;

} else {

std::cout << "Parsed command line options." << std::endl;

}

}

/* Connect to the cluster */

{

ret = cluster.connect();

if (ret < 0) {

std::cerr << "Couldn't connect to cluster! error " << ret << std::endl;

ret = EXIT_FAILURE;

return 1;

} else {

std::cout << "Connected to the cluster." << std::endl;

}

}

return 0;

}

Compile the source; then, link librados using -lrados.

For example:

g++ -g -c ceph-client.cc -o ceph-client.o

g++ -g ceph-client.o -lrados -o ceph-client

Python Example¶

Python uses the admin id and the ceph cluster name by default, and

will read the standard ceph.conf file if the conffile parameter is

set to the empty string. The Python binding converts C++ errors

into exceptions.

import rados

try:

cluster = rados.Rados(conffile='')

except TypeError as e:

print 'Argument validation error: ', e

raise e

print "Created cluster handle."

try:

cluster.connect()

except Exception as e:

print "connection error: ", e

raise e

finally:

print "Connected to the cluster."

Execute the example to verify that it connects to your cluster.

python ceph-client.py

Java Example¶

Java requires you to specify the user ID (admin) or user name

(client.admin), and uses the ceph cluster name by default . The Java

binding converts C++-based errors into exceptions.

import com.ceph.rados.Rados;

import com.ceph.rados.RadosException;

import java.io.File;

public class CephClient {

public static void main (String args[]){

try {

Rados cluster = new Rados("admin");

System.out.println("Created cluster handle.");

File f = new File("/etc/ceph/ceph.conf");

cluster.confReadFile(f);

System.out.println("Read the configuration file.");

cluster.connect();

System.out.println("Connected to the cluster.");

} catch (RadosException e) {

System.out.println(e.getMessage() + ": " + e.getReturnValue());

}

}

}

Compile the source; then, run it. If you have copied the JAR to

/usr/share/java and sym linked from your ext directory, you won’t need

to specify the classpath. For example:

javac CephClient.java

java CephClient

PHP 实例¶

在启用了 RADOS 扩展的 PHP 上,新建集群句柄非常简单:

$r = rados_create();

rados_conf_read_file($r, '/etc/ceph/ceph.conf');

if (!rados_connect($r)) {

echo "Failed to connect to Ceph cluster";

} else {

echo "Successfully connected to Ceph cluster";

}

把上述内容保存为 rados.php 并运行:

php rados.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值