一、配置文件/etc/ceph/ceph.conf以下 ui
[global]
auth cluster required = cephx
auth service required = cephx
auth client required = cephx
;keyring = /etc/ceph/keyring
ax open files = 131072
log file = /var/log/ceph/$name.log
pid file = /var/run/ceph/$name.pid
osd pool default size = 2
osd pool default min_size = 1
[mon]
mon data = /data/$name
;keyring = /data/mon.0/keyring
[mon.0]
host = ceph-one
mon addr = 192.168.122.1:6789
;[mds]
;[mds.0]
;host = ceph-one
[osd]
osd data = /data/$name
osd journal = /data/$name/journal
osd journal size = 1000
osd mkfs type = xfs
devs = /dev/ceph/$name
osd crush update on start = 0
;keyring = /etc/ceph/keyring
[osd.0]
host = ceph-one
[osd.1]
host = ceph-two
[osd.2]
host = ceph-three
[osd.3]
host = ceph-four
[osd.4]
host = ceph-five
[osd.5]
host = ceph-six 命令行
如上所示auth cluster required=cepghx中三項所有配置爲cephx選項,若是不作權限校驗所有配置爲none three
二、生成keyring文件 get
ceph-authtool --create-keyring /data/mon.0/ceph.mon.keyring --gen-key -n mon. --cap mon 'allow *' cmd
上面的命令生成keyring文件在/data/mon.0/ceph.mon.keyring,若是不配置monitor使用默認keyring文件/data/mon.0/keyring,其中/data/mon.0是ceph.conf的mon data配置目錄
ceph auth get-or-create osd.0 mon 'allow rwx' osd 'allow *' -o /data/osd.0/keyring string
上面命令生成osd的keyring it
ceph auth get-or-create client.admin mds 'allow' osd 'allow *' mon 'allow *' > /etc/ceph/ceph.client.admin.keyring io
上面命令生成client.admin的keyring,應該爲rados的訪問命令。 編譯
三、rados使用keyring訪問代碼示例 test
#include <stdio.h>
#include <string.h>
#include <rados/librados.h>
rados_t cluster; //聲明 集羣句柄
rados_ioctx_t io;
char *poolname = "data";
char cluster_name[] = "ceph";
char user_name[] = "client.admin"; //用戶名
uint64_t flags;
char xattr[] = "en_US";
int err;
char *buf, *st;
size_t buflen, stlen;
char *cmd[2];
cmd[1] = NULL;
err = rados_create2(&cluster, cluster_name, user_name, flags); //建立集羣句柄
char fsid[50]={0};
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");
}
//讀取配置文件,完成cluster句柄
err = rados_conf_set(cluster,"mon_host", "127.0.0.1");
err = rados_conf_set(cluster,"key", "AQDhSK9UeA+nOhAAhA7s2GLdU8seDVMBqK+iwg==");
//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");
}
err = rados_connect(cluster);
注意rados_conf_set兩行代碼,我使用的ceph_test.c編譯命令行
gcc ceph_test.c -o ceph_test -lrados