Ceph RGW 部署

环境准备

在4台虚拟机上进行ceph集群的部署,这4台虚拟机上都安装了centos7.6的minimal版本,并为这4台虚机配置了静态IP,4台虚机的主机名和IP地址对应如下:

主机名 IP地址
node1 192.168.1.11
node2 192.168.1.12
node3 192.168.1.13
ceph-deploy 192.168.1.10

各个节点上准备安装:

  1. node1: osd节点和mon节点
  2. node2: osd节点和mon节点
  3. node3: osd节点和mon节点
  4. ceph-deploy: ceph部署节点

安装ceph

集群配置

1、首先在各个节点上创建一个部署ceph集群的用户,并授予其sudo权限

1
2
3
4
5
6
7
8
9
[root@ceph-deploy ~]# useradd cepher
[root@ceph-deploy ~]# passwd cepher
Changing password for user cepher.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@ceph-deploy ~]# echo "cepher ALL = (root,ceph) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/cepher
cepher ALL = (root,ceph) NOPASSWD:ALL
[root@ceph-deploy ~]# chmod 0440 /etc/sudoers.d/cepher

2、在各节点上关闭防火墙和selinux

1
2
3
4
5
6
[root@ceph-deploy ~]$ setenforce 0
[root@ceph-deploy ~]$ sed -i 's/enforcing/disabled/g' /etc/selinux/config
[root@ceph-deploy ~]$ systemctl stop firewalld.service
[root@ceph-deploy ~]$ systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

3、在ceph-deploy节点以cepher用户登陆,并配置host文件(其他节点也配置host文件)

1
2
3
4
5
6
7
[cepher@ceph-deploy ~]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.11 node1
192.168.1.12 node2
192.168.1.13 node3
192.168.1.10 ceph-deploy

4、在ceph-deploy节点配置免密登陆,使得部署节点可以免密登陆其他节点

1
2
3
4
5
[cepher@ceph-deploy ~]$ ssh-keygen
...
[cepher@ceph-deploy ~]$ ssh-copy-id node1
[cepher@ceph-deploy ~]$ ssh-copy-id node2
[cepher@ceph-deploy ~]$ ssh-copy-id node3

5、在ceph-deploy节点自建ceph源,这样其他节点可以直接从部署节点获取ceph,一劳永逸。在自建源之前先在ceph-deploy节点配置ceph源,使用阿里源,这里我选择的是ceph nautilus版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[Ceph]
name=Ceph packages for $basearch
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/$basearch
enabled=1
gpgcheck=0
type=rpm-md
gpgkey=https://mirrors.aliyun.com/ceph/keys/release.asc
priority=1
[Ceph-noarch]
name=Ceph noarch packages
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/noarch
enabled=1
gpgcheck=0
type=rpm-md
gpgkey=https://mirrors.aliyun.com/ceph/keys/release.asc
priority=1
[ceph-source]
name=Ceph source packages
baseurl=http://mirrors.aliyun.com/ceph/rpm-nautilus/el7/SRPMS
enabled=1
gpgcheck=0
type=rpm-md
gpgkey=https://mirrors.aliyun.com/ceph/keys/release.asc
priority=1

自建ceph源

1、首先在ceph-deploy节点yum安装epel-release

2、创建自建源文件夹,然后将ceph及相关软件包下载到自建源文件夹下

1
2
3
4
[cepher@ceph-deploy ~]$ sudo mkdir -p /yum/ceph/nautilus
[cepher@ceph-deploy ~]$ sudo yum -y install --downloadonly --downloaddir=/yum/ceph/nautilus/ ceph ceph-radosgw htop sysstat iftop iotop ntp ntpdate
[cepher@ceph-deploy ~]$ sudo yum install -y createrepo httpd
[cepher@ceph-deploy ~]$ sudo yum install -y ceph-deploy python-pip ntp ntpdate

3、创建内部源,源路径就是自建源路径/yum/ceph/nautilus/

1
2
3
4
5
6
7
8
[cepher@ceph-deploy ~]$ sudo createrepo -pdo /yum/ceph/nautilus/ /yum/ceph/nautilus/
Spawning worker 0 with 91 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

4、修改httpd配置文件/etc/httpd/conf/httpd.conf,设置根路径为/yum/ceph/,然后启动httpd,将内部源通过httpd服务提供给其他节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DocumentRoot "/yum/ceph/"

<Directory "/yum/ceph/">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

<Directory "/yum/ceph/">
...
</Directory>

[cepher@ceph-deploy ~]$ sudo systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[cepher@ceph-deploy ~]$ sudo systemctl start httpd.service

至此,内部源就创建好了

5、ssh免密登陆到待安装ceph节点,配置源指向内部源

1
2
3
4
5
6
7
8
9
[cepher@node1 ~]$ cd /etc/yum.repos.d/
[cepher@node1 yum.repos.d]$ sudo mkdir backup
[cepher@node1 yum.repos.d]$ sudo mv ./*.repo backup/
[cepher@node1 yum.repos.d]$ cat inter.repo
[http-repo]
name=internal-ceph-repo
baseurl=http://ceph-deploy/nautilus
enabled=1
gpgcheck=0

安装ceph

ssh免密登陆到各节点,安装ceph,命令如下:

1
2
3
[cepher@node1 ~]$ sudo yum -y install  ceph ceph-radosgw htop sysstat iftop iotop ntp ntpdate
[cepher@node1 ~]$ ceph --version
ceph version 14.2.4 (75f4de193b3ea58512f204623e6c5a16e6c1e1ba) nautilus (stable)

至此,各个待安装节点上均安装了ceph,版本是14.2.4,但是现在node1,node2,node3节点上还没有安装osd,mon,rgw等服务,因此还需进一步部署集群。

集群部署

集群的部署主要通过ceph-deploy命令进行。

1、首先,初始化集群

1
2
[cepher@ceph-deploy ~]$ mkdir -p deploy
[cepher@ceph-deploy deploy]$ ceph-deploy new node1 node2 node3

ceph-deploy new初始化集群的时候会生成三个文件,分别是配置文件ceph.conf、monitor key文件ceph.mon.keyring和日志文件ceph-deploy-ceph.log,我们需要手动修改配置文件ceph.conf,添加集群公网和集群内部网络的配置(这里只添加了集群公网配置)

1
2
3
4
5
6
7
8
9
[cepher@ceph-deploy deploy]$ cat ceph.conf 
[global]
fsid = 895357cd-1bd6-45c9-80cd-5d28d080efa4
mon_initial_members = node1, node2, node3
mon_host = 192.168.1.11,192.168.1.12,192.168.1.13
auth_cluster_required = cephx
auth_service_required = cephx
auth_client_required = cephx
public_network = 192.168.1.0/24

2、创建monitor

上步的初始化仅仅是初始化了monitor,还需要创建monitor

1
2
3
4
5
6
7
8
9
10
11
[cepher@ceph-deploy deploy]$ ceph-deploy mon create-initial
[cepher@ceph-deploy deploy]$ ll
total 156
-rw-------. 1 root root 113 Oct 9 22:31 ceph.bootstrap-mds.keyring
-rw-------. 1 root root 113 Oct 9 22:31 ceph.bootstrap-mgr.keyring
-rw-------. 1 root root 113 Oct 9 22:31 ceph.bootstrap-osd.keyring
-rw-------. 1 root root 113 Oct 9 22:31 ceph.bootstrap-rgw.keyring
-rw-------. 1 root root 151 Oct 9 22:31 ceph.client.admin.keyring
-rw-r--r-- 1 root root 318 Oct 22 18:25 ceph.conf
-rw-r--r--. 1 root root 128664 Oct 23 10:15 ceph-deploy-ceph.log
-rw-------. 1 root root 73 Oct 9 22:29 ceph.mon.keyring

mon create-initial会根据ceph.conf进行创建mon,判断monitor都创建成功后,会进行keyring的收集,这些keyring在后续创建其他成员的时候要用到。

3、分发集群keyring

1
[cepher@ceph-deploy deploy]$ ceph-deploy admin node1 node2 node3

这个操作是将集群的admin.keyring分发给指定的节点,这样这些节点就可以使用ceph命令了.

4、创建mgr

1
[cepher@ceph-deploy deploy]$ ceph-deploy mgr create node1 node2 node3

5、创建osd

1
2
3
[cepher@ceph-deploy deploy]$ ceph-deploy osd create --data /dev/sdb node1
[cepher@ceph-deploy deploy]$ ceph-deploy osd create --data /dev/sdb node2
[cepher@ceph-deploy deploy]$ ceph-deploy osd create --data /dev/sdb node3

我在node1,2,3三个节点上分别添加了sda和sdb两块硬盘,上述操作是将osd分别创建在三个节点的/dev/sdb上

6、创建rgw

1
[cepher@ceph-deploy deploy]$ ceph-deploy rgw create node1 node2

7、在三个node节点上nptdate时钟,实现时钟同步

1
2
3
ntpdate ntp1.aliyun.com #阿里云
systemctl enable ntpd.service
systemctl start ntpd.service

8、查看集群状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@node1 ~]# ceph -s
cluster:
id: 895357cd-1bd6-45c9-80cd-5d28d080efa4
health: HEALTH_OK

services:
mon: 3 daemons, quorum node1,node2,node3 (age 80s)
mgr: node3(active, since 2m), standbys: node1, node2
osd: 3 osds: 3 up (since 66s), 3 in (since 66s)
rgw: 3 daemons active (node1, node2, node3)

data:
pools: 7 pools, 176 pgs
objects: 247 objects, 112 KiB
usage: 3.1 GiB used, 24 GiB / 27 GiB avail
pgs: 176 active+clean

安装rgw

其实上面安装ceph的时候node1,node2,node3已经安装了rgw,直接可以创建rgw实例。rgw即RADOSGW,是一种服务,使客户端能够利用标准对象存储API来访问Ceph集群,支持Amazon S3和Openstack Swift API。rgw运行于librados之上,事实上就是一个称之为Civetweb的web服务器来响应api请求,客户端利用标准对象存储API与rgw通信,而rgw则使用librados与ceph集群通信。

创建rgw实例

1、在ceph部署节点,执行ceph-deploy rgw create <gateway-node>,比如ceph-deploy rgw create node1这样就在node1节点创建了rgw实例,默认rgw已经启动。

ceph v0.80之后,Ceph对象网关运行在Civetweb(嵌入到ceph-radosgw守护程序中)上,默认通过7480端口提供rgw对象服务。

1
http://client-node:7480

rgw实例会返回如下response

1
2
3
4
5
6
7
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>anonymous</ID>
<DisplayName/>
</Owner>
<Buckets/>
</ListAllMyBucketsResult>

2、更改默认端口

在ceph部署节点修改ceph.conf配置文件,以修改node1节点上的rgw默认端口为例,比如将端口修改为80,在[global]之下增加如下配置:

1
2
[client.rgw.node1]
rgw_frontends = civetweb port=80

port后面的=两端不要有空格

将更新的配置文件推送到您的Ceph对象网关节点(和其他Ceph节点):

1
ceph-deploy --overwrite-conf config push node1 [<other-nodes>]

最后重新启动Ceph对象网关,使新的端口设置生效:

1
2
ssh node1
sudo systemctl restart ceph-radosgw@rgw.node1.service

更多设置请查考Ceph Documentation

使用rgw

要使用REST接口,要首先为S3接口创建一个初始Ceph对象网关用户。然后,为Swift接口创建一个子用户。

1、创建用于S3访问的RADOSGW用户

命令如下:

1
sudo radosgw-admin user create --uid="testuser" --display-name="First User"

命令的输出将类似于以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"user_id": "testuser",
"display_name": "First User",
"email": "",
"suspended": 0,
"max_buckets": 1000,
"subusers": [],
"keys": [{
"user": "testuser",
"access_key": "I0PJDPCIYZ665MW88W9R",
"secret_key": "dxaXZ8U90SXydYzyS5ivamEP20hkLSUViiaR+ZDA"
}],
"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": []
}

NOTE:keys->access_keykeys->secret_key是用于访问验证的

2、创建一个SWIFT用户

1
2
ssh node1
sudo radosgw-admin subuser create --uid=testuser --subuser=testuser:swift --access=full

命令的输出将类似于以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
"user_id": "testuser",
"display_name": "First User",
"email": "",
"suspended": 0,
"max_buckets": 1000,
"subusers": [{
"id": "testuser:swift",
"permissions": "full-control"
}],
"keys": [
{
"user": "testuser",
"access_key": "I0PJDPCIYZ665MW88W9R",
"secret_key": "dxaXZ8U90SXydYzyS5ivamEP20hkLSUViiaR+ZDA"
}],
"swift_keys": [{
"user": "testuser:swift",
"secret_key": "244+fz2gSqoHwR3lYtSbIyomyPHf3i7rgSJrF\/IA"
}],
"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": [],
"type": "rgw",
"mfa_ids": []

}

3、查看用户信息(需要指定用户id)

1
radosgw-admin user info --uid=testuser

4、编写python脚本测试S3接口

编写并运行Python测试脚本来验证S3访问。S3访问测试脚本将连接到radosgw,创建一个新存储桶并列出所有存储桶。
aws_access_key_idaws_secret_access_key对应于keys->access_keykeys->secret_key

需要安装python-boto软件包:

1
2
ssh node1
sudo yum install python-boto

创建Python脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vi s3test.py

import boto.s3.connection

access_key = 'I0PJDPCIYZ665MW88W9R'
secret_key = 'dxaXZ8U90SXydYzyS5ivamEP20hkLSUViiaR+ZDA'
conn = boto.connect_s3(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
host='{hostname}', port={port},
is_secure=False, calling_format=boto.s3.connection.OrdinaryCallingFormat(),
)

bucket = conn.create_bucket('my-new-bucket')
for bucket in conn.get_all_buckets():
print "{name} {created}".format(
name=bucket.name,
created=bucket.creation_date,
)

其中{hostname}和{port}分别替换为rgw本机的主机名和端口号
执行该python脚本:

1
2
[cepher@node1 ~]# python s3test.py 
my-new-bucket 2019-10-23T01:45:13.627Z

得到上面的输出类似的输出,说明S3接口可以正常访问

5、使用s3cmd工具测试s3接口

  • 首先安装s3cmd,通过pip或者yum可以直接安装
    yum install s3cmd
    pip search s3cmd

  • 使用s3cmd前需要配置Access Key 和 Secret Key,这两个key便是创建s3用户时生成的,这个在前面提过。使用命令s3cmd --configure进行配置,下图是本机的配置过程:
    s3cmd-conf
    注意图中红框中标出的(因为之前已经配置过,所以[]中记录的是之前的配置),配置完成之后会保存在.s3cfg文件中。vim打开配置文件.s3cfg,将signature_v2 = False修改为signature_v2 = True,本文在实验过程中发现如果不将signature_v2设置为True,s3的读写文件操作返回403(Forbidden),这样修改参考了:
    s3cmd_issue
    但是还没有彻底解决,只是可以正常用了,只用签名v4该怎么做,这里还没解决。

  • s3cmd的基本使用

    1. 列举出所有buckets(bucket相当于根文件夹)
      s3cmd ls
    2. 创建 bucket,且 bucket 名称是唯一的,不能重复
      s3cmd mb s3://my-bucket-name
    3. 删除空 bucket
      s3cmd rb s3://my-bucket-name
    4. 列举 bucket 中的内容
      s3cmd ls s3://my-bucket-name
    5. 上传文件到某个 bucket
      s3cmd put file.txt s3://my-bucket-name/file.txt
    6. 上传并将权限设置为所有人可读
      s3cmd put --acl-public file.txt s3://my-bucket-name/file.txt,文件对应的可访问URL: http://192.168.1.11:80/my-bucket-name/file.txt
    7. 从某个bucket下载文件,并进行保存为某文件
      s3cmd get s3://my-bucket-name/file.txt xxx
    8. 删除某个文件
      s3cmd del s3://my-bucket-name/file.txt
    9. 设置bucket的public权限
      s3cmd setacl s3://myexamplebucket.calvium.com/ --acl-public --recursive
    10. 获得某个bucket所占用的空间大小
      s3cmd du -H s3://my-bucket-name

6、访问验证,测试SWIFT接口

可以通过swift命令行客户端验证快速访问,首先安装swift客户端:

1
2
3
4
sudo yum install python-setuptools
sudo easy_install pip
sudo pip install --upgrade setuptools
sudo pip install --upgrade python-swiftclient

要测试快速访问,执行以下操作:

1
swift -V 1 -A http://{IP ADDRESS}:{port}/auth -U testuser:swift -K '{swift_secret_key}' list

{IP ADDRESS}:{port}用rgw服务主机的ip地址和端口号替换(默认7480),{swift_secret_key}用创建swift用户时的输出swift_keys->secret_key替换。

输出应为:

1
my-new-bucket

输出结果说明swift接口可以正常访问


ceph部署参考:http://strugglesquirrel.com/2019/04/23/centos7%E9%83%A8%E7%BD%B2ceph/