在 ceph 的使用上, 互聯網大規模的文件場景下, fs 并不能滿足生産的使用要求,rados 本地化操作也不便于服務的接入與使用, 這裡我們就要采用 Ceph Swift Api 來實現文件的存儲管理。
二、Ceph Swift Api 特點Swift 是由 Rackspace 開發,用來為雲計算提供可擴展存儲的項目。專注于對象存儲, 并提供一套 REST 風格的 Api 來訪問, 與 Ceph 強一緻性不同, 它是最終一緻性。兩者都是優秀的開源項目, 并無明顯優劣之分,在使用場景上有所不同, 如果是專注于對象存儲, 那麼可以選擇 swift 即可滿足需要, 如果還有塊存儲要求, 那麼選擇 Ceph 更為合适。這裡選擇 Ceph, 因為通過網關可以适配兼容 swift api, 同時在數據訪問上具有較強的擴展性:
Ceph 可以提供塊、文件和對象三種形态的存儲。RGW 就是提供對象存儲的網關,也即對象存儲網關。 所謂對象存儲網關,也就是對象存儲的入口,本質上是一個 HTTP 服務器,與 Nginx 和 Apache 無特殊差别。通過這個網關入口,用戶可以采用 HTTP 協議,以 RESTful 的方式訪問 Ceph 的對象存儲。
四、Ceph 存儲結構在使用對象存儲之前, 先要了解桶(container 容器)概念及其存儲結構:
Ceph Swift Api 的調用, 需要先創建相應用戶進行認證才能操作, 每個用戶下面可以創建多個桶, 桶裡面可以存儲對象,對象就是各種數據文件, 包括文檔, 圖片等。傳統上傳文件的使用, 我們往往會指 定路徑信息, 在這裡, 桶和對象的關系好比文件夾與文件的概念, 不同之處是桶不能再嵌套桶, 也就是沒有層級路徑的概念。
Ceph 存儲結構:
五、Ceph Swift Api 服務端的配置1、确保集群正常安裝并啟動:
如果 rgw 沒有顯示, 檢查服務狀态:
systemctl list-unit-Files|grep enabled|grep ceph
複制代碼
重啟 RGW 服務:
ceph-deploy --overwrite-conf --ceph-conf ceph.conf rgw create CENTOS7-1
systemctl restart ceph-radosgw@*.service
複制代碼
2、驗證網關是否正常
訪問地址,http://192.168.88.161:7480
出現以下提示代表正常
3、創建 Swift 用戶, 用于接口請求認證
sudo radosgw-admin user create --subuser="cephtester:subtester" --uid="cephtester" --display-name="cephtester" --key-type=swift --secret="654321" --access=full
複制代碼
uid 為主用戶, subuser 為子用戶信息, secret 指定密鑰, 不指定則随機生成, access 擁有權限設定。
返回結果:
[root@CENTOS7-1 ceph-cluster]# sudo radosgw-admin user create --subuser="cephtester:subtester" --uid="cephtester" --display-name="cephtester" --key-type=swift --secret="654321" --access=full
{
"user_id": "cephtester",
"display_name": "cephtester",
"email": "",
"suspended": 0,
"max_buckets": 1000,
"auid": 0,
"subusers": [
{
"id": "cephtester:subtester",
"permissions": "full-control"
}
],
"keys": [],
"swift_keys": [
{
"user": "cephtester:subtester",
"secret_key": "654321"
}
],
"caps": [],
"op_mask": "read, write, delete",
"default_placement": "",
"placement_tags": [],
"bucket_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"user_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"temp_url_keys": [],
"type": "rgw",
"mfa_ids": []
}
複制代碼
記住 swift_keys 下面的 user 和 secret_key 信息, 代碼中需使用。
4、激活管理後台的對象存儲模塊:5、創建一個管理用戶:
radosgw-admin user create --uid=mgruser --display-name=mgruser --system
複制代碼
返回結果:
[root@CENTOS7-1 ceph-cluster]# radosgw-admin user create --uid=mgruser --display-name=mgruser --system
{
"user_id": "mgruser",
"display_name": "mgruser",
"email": "",
"suspended": 0,
"max_buckets": 1000,
"auid": 0,
"subusers": [],
"keys": [
{
"user": "mgruser",
"access_key": "W462B8SE31NDCOQR4E6I",
"secret_key": "XPZcugMWRhYccFlEPIHKcyFVr73grVuiJgFD4rDR"
}
],
"swift_keys": [],
"caps": [],
"op_mask": "read, write, delete",
"system": "true",
"default_placement": "",
"placement_tags": [],
"bucket_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"user_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"temp_url_keys": [],
"type": "rgw",
"mfa_ids": []
}
複制代碼
根據生成的 access_key 與 secret_key, 執行:
ceph dashboard set-rgw-api-access-key W462B8SE31NDCOQR4E6I
ceph dashboard set-rgw-api-secret-key XPZcugMWRhYccFlEPIHKcyFVr73grVuiJgFD4rDR
複制代碼
打開管理界面,https://192.168.88.161:18843 可以查看到我們剛才創建的兩個用戶:
六、Ceph Swift Api 調用驗證1、創建 ceph-demo 工程
增加 SwiftOperator 接口:
@Component
public class SwiftOperator {
/**
* 用戶名信息, 格式: 主用戶名:子用戶名
*/
private String username ="cephtester:subtester";
/**
* 用戶密碼
*/
private String password = "654321";
/**
* 接口訪問地址
*/
private String authUrl = "http://192.168.88.161:7480/auth/1.0";
/**
* 默認存儲的容器名稱(bucket)
*/
private String defaultContainerName = "user_datainfo";
/**
* Ceph的賬戶信息
*/
private Account account = null;
/**
* Ceph的容器信息
*/
private Container container;
/**
* 進行Ceph的初始化配置
*/
public SwiftOperator() {
// 1. Ceph的賬戶信息配置
AccountConfig config = new AccountConfig();
config.setUsername(username);
config.setPassword(password);
config.setAuthUrl(authUrl);
config.setAuthenticationMethod(AuthenticationMethod.BASIC);
account = new AccountFactory(config).createAccount();
// 2.獲取容器信息
Container newContainer = account.getContainer(defaultContainerName);
if(!newContainer.exists()) {
container = newContainer.create();
System.out.println("container create ==> " defaultContainerName);
}else {
container = newContainer;
}
}
/**
* 文件上傳處理
* @param remoteName
* @param filePath
*/
public void createObject(String remoteName, String filePath) {
// 1. 從容器當中獲取遠程存儲對象信息
StoredObject object = container.getObject(remoteName);
// 2. 執行文件上傳處理
object.uploadObject(new File(filePath));
}
/**
* 文件的下載處理
* @param objectName
* @param outPath
*/
public void retrieveObject(String objectName, String outPath) {
// 1. 從容器當中獲取遠程存儲對象信息
StoredObject object = container.getObject(objectName);
// 2. 執行文件的下載方法
object.downloadObject(new File(outPath));
}
/**
* 獲取用戶下面的所有容器信息
* @return
*/
public List listContainer() {
List list = new ArrayList();
Collection<Container> containers = account.list();
for(Container container : containers) {
list.add(container.getName());
System.out.println("current container name : " container.getName());
}
return list;
}
}
複制代碼
這裡的用戶名和密碼填寫上面我們所生成的信息。注意路徑地址後綴為: /auth/1.0
CephDemoApplication 啟動類,測試驗證:
@SpringBootApplication
@ComponentScan(basePackages = {"cn.it"})
public class CephDemoApplication {
public static void main(String[] args) throws Exception {
// Swift Api接口調用驗證
swiftApi();
}
/**
* 通過Swift接口操作ceph集群
* @throws Exception
*/
public static void swiftApi() throws Exception {
ConfigurableApplicationContext appContext = SpringApplication.run(CephDemoApplication.class);
// 1. 先打印出用戶的容器信息
SwiftOperator swiftOperator = appContext.getBean(SwiftOperator.class);
swiftOperator.listContainer();
String objName = "test_ceph";
// 2. 上傳指定的文件
swiftOperator.createObject(objName, "E:\\test\\upload\\test_swift_ceph.txt");
// 3. 從ceph下載文件到指定的路徑下面
swiftOperator.retrieveObject(objName, "E:\\test\\download\\test_swift_ceph.txt");
System.out.println("complete");
}
}
複制代碼
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!