使用ceph的对象存储

Ceph 对象存储

Ceph 对象存储使用 Ceph 对象网关守护进程( radosgw ),它是个与 Ceph 存储集群交互的 FastCGI 模块。因为它提供了与 OpenStack Swift 和 Amazon S3 兼容的接口, RADOS 要有它自己的用户管理。 Ceph 对象网关可与 Ceph FS 客户端或 Ceph 块设备客户端共用一个存储集群。 S3 和 Swift 接口共用一个通用命名空间,所以你可以用一个接口写如数据、然后用另一个接口取出数据。

Ceph 对象网关是一个构建在 librados 之上的对象存储接口,它为应用程序访问Ceph 存储集群提供了一个 RESTful 风格的网关 。 Ceph 对象存储支持 2 种接口:

兼容S3: 提供了对象存储接口,兼容 亚马逊S3 RESTful 接口的一个大子集。
兼容Swift: 提供了对象存储接口,兼容 Openstack Swift 接口的一个大子集。

更多信息
http://docs.ceph.org.cn/radosgw/

安装Ceph对象网关

Ceph 对象存储使用 Ceph 对象网关守护进程( radosgw ),所以在使用对象存储之前,我们需要先安装配置好对象网关RGW。

Ceph RGW的FastCGI支持多种Web服务器作为前端,例如Nginx、Apache2等。 从Ceph Hammer版本开始,使用ceph-deploy部署时将会默认使用内置的civetweb作为前端,区别在于配置的方式不同,我们这里采用默认civetweb方式安装配置RGW。

Ceph 从 v0.80 开始,使用内嵌 Civetweb 作为 Web Server,无需额外安装 web 服务器或配置 FastCGI,其默认端口为 7480。在 admi管理节点目录通过 ceph-deploy 安装 Ceph RGW。这里我们还是使用admin节点做测试。

使用nginx方式可参考链接 https://blog.frognew.com/2017/02/ceph-rgw.html

使用apache方式可参考官网链接:
http://docs.ceph.org.cn/radosgw/config/

在规划的作为网关的服务器中,使用命令运行安装网关服务

扫描二维码关注公众号,回复: 1480637 查看本文章
sudo yum install -y ceph-radosgw

创建RGW用户和keyring

在规划的作为网关的服务器上创建keyring:

sudo ceph-authtool --create-keyring /etc/ceph/ceph.client.radosgw.keyring
sudo chmod +r /etc/ceph/ceph.client.radosgw.keyring

生成ceph-radosgw服务对应的用户和key:

sudo ceph-authtool /etc/ceph/ceph.client.radosgw.keyring -n client.radosgw.gateway --gen-key

为用户添加访问权限:

sudo ceph-authtool -n client.radosgw.gateway --cap osd 'allow rwx' --cap mon 'allow rwx' /etc/ceph/ceph.client.radosgw.keyring

导入keyring到集群配置文件中:

sudo ceph -k /etc/ceph/ceph.client.admin.keyring auth add client.radosgw.gateway -i /etc/ceph/ceph.client.radosgw.keyring

把集群配置文件推送到其他机子

需要两边的机子都安装有scp服务
使用命令

sudo yum install openssh-clients

推送key使用命令

sudo scp /etc/ceph/ceph.client.radosgw.keyring  ceph1:/etc/ceph
sudo ssh ceph1
chmod 777   /etc/ceph/ceph.client.radosgw.keyring 
exit
sudo scp /etc/ceph/ceph.client.radosgw.keyring  ceph2:/etc/ceph
sudo ssh ceph2
chmod 777   /etc/ceph/ceph.client.radosgw.keyring 
exit
sudo scp /etc/ceph/ceph.client.radosgw.keyring  ceph3:/etc/ceph
sudo ssh ceph3
chmod 777   /etc/ceph/ceph.client.radosgw.keyring 
exit
sudo scp /etc/ceph/ceph.client.radosgw.keyring  ceph4:/etc/ceph
sudo ssh ceph4
chmod 777   /etc/ceph/ceph.client.radosgw.keyring 
exit

创建资源池

由于RGW要求专门的pool存储数据,这里手动创建这些Pool,在任意ceph节点上执行:

ceph osd pool create .rgw 128 128
ceph osd pool create .rgw.root 128 128
ceph osd pool create .rgw.control 128 128
ceph osd pool create .rgw.gc 128 128
ceph osd pool create .rgw.buckets 128 128
ceph osd pool create .rgw.buckets.index 128 128
ceph osd pool create .rgw.buckets.extra 128 128
ceph osd pool create .log 128 128
ceph osd pool create .intent-log 128 128
ceph osd pool create .usage 128 128
ceph osd pool create .users 128 128
ceph osd pool create .users.email 128 128
ceph osd pool create .users.swift 128 128
ceph osd pool create .users.uid 128 128

列出pool信息确认全部成功创建:

rados lspools

输出如下:

.rgw
.rgw.root
.rgw.control
.rgw.gc
.rgw.buckets
.rgw.buckets.index
.rgw.buckets.extra
.log
.intent-log
.usage
.users
.users.email
.users.swift
.users.uid

确保集群健康

使用ceph -s命令查看检查集群健康情况,一般新建很多pool的时候集群容易出现异常,这样即使我们后面启动了网关,也无法使用。会报错libcurl doesn’t support curl_multi_wait()。

[zzq@localhost myceph]$  ceph -s
    cluster 5862afaf-4f46-4d6f-9d30-cb911ce335fc
     health HEALTH_WARN
            1928 pgs degraded
            1928 pgs stuck degraded
            1670 pgs stuck inactive
            2048 pgs stuck unclean
            1928 pgs stuck undersized
            1928 pgs undersized
            19 requests are blocked > 32 sec
            recovery 20/40 objects degraded (50.000%)
            too many PGs per OSD (512 > max 300)
            mon.ceph4 low disk space
     monmap e3: 4 mons at {ceph1=192.168.199.81:6789/0,ceph2=192.168.199.82:6789/0,ceph3=192.168.199.83:6789/0,ceph4=192.168.199.84:6789/0}
            election epoch 26, quorum 0,1,2,3 ceph1,ceph2,ceph3,ceph4
     mdsmap e13: 1/1/1 up {0=ceph4=up:active}
     osdmap e91: 4 osds: 4 up, 4 in; 120 remapped pgs
      pgmap v916: 2048 pgs, 17 pools, 2297 bytes data, 20 objects
            12589 MB used, 19418 MB / 32908 MB avail
            20/40 objects degraded (50.000%)
                1670 undersized+degraded+peered
                 258 active+undersized+degraded
                 120 active+remapped
[zzq@localhost myceph]$ 

具体解决调整参考
遇到问题—ceph—ceph的rbd命令没反应卡住

RGW配置

在规划作为网关的服务器/etc/ceph/ceph.conf上添加以下内容:
host需要与hostname对应,其他默认即可。

[client.radosgw.gateway]
rgw frontends=fastcgi socket_port=9000 socket_host=0.0.0.0
host=admin
keyring=/etc/ceph/ceph.client.radosgw.keyring
log file=/var/log/radosgw/client.radosgw.gateway.log
rgw socket path=/var/run/ceph/ceph.radosgw.gateway.fastcgi.sock
rgw print continue=false
rgw content length compat = true

新建日志目录

sudo mkdir -p /var/log/radosgw
sudo chown 777 /var/log/radosgw
sudo chown 777 /var/run/ceph

把配置分发到其他机子

ceph-deploy --overwrite-conf config push admin ceph1 ceph2 ceph3 ceph4

新建 Ceph 对象网关实例

在admin管理节点工作目录创建一个 Ceph rgw 实例,一旦对象网关开始运行,我们就可以通过 http://admin:7480 地址访问。

进入ceph-deploy的目录中,使用命令

cd /home/zzq/myceph
sudo ceph-deploy --overwrite-conf rgw create admin

正确输出如下:

[zzq@localhost myceph]$ sudo ceph-deploy --overwrite-conf rgw create admin
[ceph_deploy.conf][DEBUG ] found configuration file at: /root/.cephdeploy.conf
[ceph_deploy.cli][INFO  ] Invoked (1.5.37): /usr/bin/ceph-deploy --overwrite-conf rgw create admin
[ceph_deploy.cli][INFO  ] ceph-deploy options:
[ceph_deploy.cli][INFO  ]  username                      : None
[ceph_deploy.cli][INFO  ]  verbose                       : False
[ceph_deploy.cli][INFO  ]  rgw                           : [('admin', 'rgw.admin')]
[ceph_deploy.cli][INFO  ]  overwrite_conf                : True
[ceph_deploy.cli][INFO  ]  subcommand                    : create
[ceph_deploy.cli][INFO  ]  quiet                         : False
[ceph_deploy.cli][INFO  ]  cd_conf                       : <ceph_deploy.conf.cephdeploy.Conf instance at 0x265af38>
[ceph_deploy.cli][INFO  ]  cluster                       : ceph
[ceph_deploy.cli][INFO  ]  func                          : <function rgw at 0x25b8758>
[ceph_deploy.cli][INFO  ]  ceph_conf                     : None
[ceph_deploy.cli][INFO  ]  default_release               : False
[ceph_deploy.rgw][DEBUG ] Deploying rgw, cluster ceph hosts admin:rgw.admin
[admin][DEBUG ] connected to host: admin 
[admin][DEBUG ] detect platform information from remote host
[admin][DEBUG ] detect machine type
[ceph_deploy.rgw][INFO  ] Distro info: CentOS 6.9 Final
[ceph_deploy.rgw][DEBUG ] remote host will use sysvinit
[ceph_deploy.rgw][DEBUG ] deploying rgw bootstrap to admin
[admin][DEBUG ] write cluster configuration to /etc/ceph/{cluster}.conf
[admin][WARNIN] rgw keyring does not exist yet, creating one
[admin][DEBUG ] create a keyring file
[admin][DEBUG ] create path recursively if it doesn't exist
[admin][INFO  ] Running command: ceph --cluster ceph --name client.bootstrap-rgw --keyring /var/lib/ceph/bootstrap-rgw/ceph.keyring auth get-or-create client.rgw.admin osd allow rwx mon allow rw -o /var/lib/ceph/radosgw/ceph-rgw.admin/keyring
[admin][INFO  ] Running command: service ceph-radosgw start
[admin][DEBUG ] Starting client.rgw.admin...
[admin][WARNIN] /usr/bin/dirname: extra operand `-n'
[admin][WARNIN] Try `/usr/bin/dirname --help' for more information.
[admin][DEBUG ] 2018-05-27 17:55:20.895832 7f76e44c5820 -1 WARNING: libcurl doesn't support curl_multi_wait()
[admin][DEBUG ] 2018-05-27 17:55:20.895832 7f76e44c5820 -1 WARNING: cross zone / region transfer performance may be affected
[admin][DEBUG ] [  OK  ]
[admin][INFO  ] Running command: chkconfig ceph-radosgw on
[ceph_deploy.rgw][INFO  ] The Ceph Object Gateway (RGW) is now running on host admin and default port 7480
Error in sys.exitfunc:

查看radosgw服务状态:

ps -ef | grep radosgw
sudo systemctl status ceph-radosgw.service #centos7
sudo service  ceph-radosgw status  #centos6

重启RGW使用命令

sudo systemctl restart ceph-radosgw.service #centos7
sudo service  ceph-radosgw restart  #centos6

如果报错ibcurl doesn’t support curl_multi_wait()说明没有权限认证或者需要的资源池没有创建好。

[zzq@localhost myceph]$ sudo service  ceph-radosgw restart
Starting client.rgw.admin...                               [FAILED]
/usr/bin/dirname: extra operand `-n'
Try `/usr/bin/dirname --help' for more information.
2018-05-27 18:07:21.747065 7ff716144820 -1 WARNING: libcurl doesn't support curl_multi_wait()
2018-05-27 18:07:21.747066 7ff716144820 -1 WARNING: cross zone / region transfer performance may be affected

回到第二步第三步使用命令创建好key和资源池后重新执行create使用命令如下:

sudo ceph-deploy --overwrite-conf rgw create admin

如果确认RGW 已经运行起来了,我们来访问一下试下。

[zzq@localhost myceph]$ sudo service  ceph-radosgw status
/usr/bin/radosgw is running.

使用命令

curl http://admin:7480

运行成功输出如下:

[zzq@localhost ~]$ curl http://admin:7480
<?xml version="1.0" encoding="UTF-8"?><ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Owner><ID>anonymous</ID><DisplayName></DisplayName></Owner><Buckets></Buckets></ListAllMyBucketsResult>
[zzq@localhost ~]$ 

如果还是有异常则需要查看日志

cat /var/log/radosgw/ceph-client.rgw.admin.log

如果报错bind_and_listen: failed to bind the UNIX domain socket to ‘/var/run/ceph/ceph-client.rgw.admin.asok’: (17) File exists,
则需要确认是否指定 rgw socket path参数。

使用 Ceph 对象网关

为了使用 Ceph SGW REST 接口, 我们需要为 S3 接口初始化一个 Ceph 对象网关用户. 然后为 Swift 接口新建一个子用户,最后就可以通过创建的用户访问对象网关验证了。

创建 S3 网关用户

我们需要创建一个 RADOSGW 用户并且赋予访问权限,才可以正常访问 RGW,Ceph 提供了 radosgw-admin 命令行很方便完成。

使用命令如下:

sudo radosgw-admin user create --uid="rgwuser" --display-name="This is first rgw test user"

输出如下:

[zzq@localhost ~]$ sudo radosgw-admin user create --uid="rgwuser" --display-name="This is firstt user"s 
[sudo] password for zzq: 
{
    "user_id": "rgwuser",
    "display_name": "This is first rgw test user",
    "email": "",
    "suspended": 0,
    "max_buckets": 1000,
    "auid": 0,
    "subusers": [],
    "keys": [
        {
            "user": "rgwuser",
            "access_key": "ZN23EX4KO0GHN2O5E5F9",
            "secret_key": "OJRYntMXZv3u5xDZK8aPyAlLaChIxdvma9WPX9FO"
        }
    ],
    "swift_keys": [],
    "caps": [],
    "op_mask": "read, write, delete",
    "default_placement": "",
    "placement_tags": [],
    "bucket_quota": {
        "enabled": false,
        "max_size_kb": -1,
        "max_objects": -1
    },
    "user_quota": {
        "enabled": false,
        "max_size_kb": -1,
        "max_objects": -1
    },
    "temp_url_keys": []
}

注意:返回的 Json 值中,我们要记住两个 access_key 和 secret_key 值,因为下边我们测试访问 S3 接口时需要使用。

测试访问 S3 接口

参照官方文档,我们需要编写一个 Python 测试脚本,该脚本将会连接 radosgw,然后新建一个新的 bucket 再列出所有的 buckets。脚本变量 aws_access_key_id 和 aws_secret_access_key 的值就是上边返回值中的 access_key 和 secret_key。

首先,我们需要安装 python-boto 包,用于测试连接 S3。

sudo yum install python-boto

然后,编写 python 测试脚本。

vi s3.py

输入如下内容:

import boto
import boto.s3.connection
access_key = 'ZN23EX4KO0GHN2O5E5F9'
secret_key = 'OJRYntMXZv3u5xDZK8aPyAlLaChIxdvma9WPX9FO'
conn = boto.connect_s3(
    aws_access_key_id = access_key,
    aws_secret_access_key = secret_key,
    host = 'admin', port=7480,
    is_secure=False,
    calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn.create_bucket('my-first-s3-bucket')
for bucket in conn.get_all_buckets():
        print "{name}\t{created}".format(
                name = bucket.name,
                created = bucket.creation_date,
)

注意:这里使用了python-boto 包,使用认证信息连接 S3,然后创建了一个 my-first-s3-bucket 的 bucket,最后列出所有已创建的 bucket,打印名称和创建时间。

最后,执行脚本,看下结果是否正确。

[zzq@localhost ~]$ python s3.py 
my-first-s3-bucket  2018-05-31T15:09:02.000Z
[zzq@localhost ~]$

测试通过。

删除S3用户

radosgw-admin  user rm --uid=rgwuser

权限调整

允许rgwuser读写users信息:

radosgw-admin caps add --uid=rgwuser --caps="users=*"

允许admin读写所有的usage信息:

radosgw-admin caps add --uid=rgwuser --caps="usage=read,write"

创建 Swift 用户

要通过 Swift 访问对象网关,需要 Swift 用户,我们创建subuser作为子用户。
使用命令

sudo radosgw-admin subuser create --uid=rgwuser --subuser=rgwuser:swift --access=full
#创建密钥
sudo radosgw-admin key create --subuser=rgwuser:swift --key-type=swift --gen-secret

输出如下:

{
    "user_id": "rgwuser",
    "display_name": "This is first rgw test user",
    "email": "",
    "suspended": 0,
    "max_buckets": 1000,
    "auid": 0,
    "subusers": [
        {
            "id": "rgwuser:swift",
            "permissions": "full-control"
        }
    ],
    "keys": [
        {
            "user": "rgwuser:swift",
            "access_key": "DPLAX7KKAFTICHGIEQU4",
            "secret_key": ""
        },
        {
            "user": "rgwuser",
            "access_key": "ZN23EX4KO0GHN2O5E5F9",
            "secret_key": "OJRYntMXZv3u5xDZK8aPyAlLaChIxdvma9WPX9FO"
        }
    ],
    "swift_keys": [
        {
            "user": "rgwuser:swift",
            "secret_key": "ja3mxJTGD7tvFsK1tVL8qScWPp2Wn657Jo4h4vLv"
        }
    ],
    "caps": [],
    "op_mask": "read, write, delete",
    "default_placement": "",
    "placement_tags": [],
    "bucket_quota": {
        "enabled": false,
        "max_size_kb": -1,
        "max_objects": -1
    },
    "user_quota": {
        "enabled": false,
        "max_size_kb": -1,
        "max_objects": -1
    },
    "temp_url_keys": []
}

注意:返回的 Json 值中,我们要记住swift_keys中的secret_key 因为下边我们测试访问 Swift 接口时需要使用。

测试访问 Swift 接口

注意,以下命令需要python环境和可用的pip服务。

如果没有会报错。

可参考连接安装pip3.5,然后把以下的pip换成 pip3.5执行。

centos6安装python3.5、pip3.5、easy_install-3.5

访问 Swift 接口可以通过 swift 命令行客户端来完成,然后通过客户端命令访问 Swift 接口。

# 安装 Swift 命令行客户端
$ sudo yum install python-setuptools
$ sudo easy_install pip
$ sudo pip install --upgrade setuptools
$ sudo pip install --upgrade python-swiftclient

# 访问 Swift 接口
$ swift -A http://admin:7480/auth/1.0 -U rgwuser:swift -K 'ja3mxJTGD7tvFsK1tVL8qScWPp2Wn657Jo4h4vLv' list

输出如下:

[zzq@localhost ~]$ swift -A http://admin:7480/auth/1.0 -U rgwuser:swift -K 'ja3mxJTGD7tvFsK1tVL8qScWPp2Wn657Jo4h4vLv' list
my-first-s3-bucket
[zzq@localhost ~]$ 

注意:admin可以替换为 为网关服务器的外网访问 IP 地址,这里为 admin-node 节点 IP,端口默认 7480,若已修改端口号,这里也需要对应修改一下。密钥 Key 为上边返回值中的 secret_key。

同样,测试通过。

更多swift信息参考:
https://www.swiftstack.com/docs/integration/python-swiftclient.html

删除子用户

sudo radosgw-admin subuser rm  --subuser=rgwuser:swift

如果未正确生成swift的密码,而是用空密码访问的话会输出如下:

[zzq@localhost ~]$ swift -A http://admin:7480/auth/1.0 -U rgwuser:swift -K '' list
Auth version 1.0 requires ST_AUTH, ST_USER, and ST_KEY environment variables
to be set or overridden with -A, -U, or -K.

Auth version 2.0 requires OS_AUTH_URL, OS_USERNAME, OS_PASSWORD, and
OS_TENANT_NAME OS_TENANT_ID to be set or overridden with --os-auth-url,
--os-username, --os-password, --os-tenant-name or os-tenant-id. Note:
adding "-V 2" is necessary for this.

很多网上的帖子都是只创建了swift用户没有创建swift的key步骤,害人不浅,最终还是官网救了一命。
更多细节可参考官网:
http://docs.ceph.org.cn/radosgw/config/#id6

修改端口

如果我们想修改 7480 端口为其他值时,ceph 也是支持的,通过修改 Ceph 配置文件更改默认端口,然后重启 Ceph 对象网关即可。例如我们修改端口为 80。

修改 Ceph 配置文件

$ sudo vi /etc/ceph/ceph.conf

在 [global] 节点下增加

[client.rgw.admin]
rgw_frontends = "civetweb port=80"

重启 Ceph 对象网关

sudo systemctl restart ceph-radosgw.service #centos7
sudo service  ceph-radosgw restart  #centos6

猜你喜欢

转载自blog.csdn.net/q383965374/article/details/80532443