Kubernetes中分布式存储Rook-Ceph部署快速演练

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Kubernetes中分布式存储Rook-Ceph部署快速演练
最近在项⽬中有涉及到Kubernetes的分布式存储部分的内容,也抽空多了解了⼀些。

项⽬主要基于Rook-Ceph运⾏,考虑到Rook-Ceph部署也不那么简单,的步骤起点也不算低,因此,在整合官⽅⽂档的某些步骤的基础上,写篇⽂章简单总结⼀下。

Rook-Ceph是Kubernetes中分布式存储的⼀种解决⽅案,Rook作为⼀种开源的、云原⽣的存储编排器,为各种存储服务在云原⽣的环境中实现⽆缝整合,提供了所必须的平台、框架和服务;⽽Ceph则是Rook所⽀持的众多存储⽅案的⼀种,在Kubernetes环境⾥,Ceph基于Rook能够为应⽤程序提供块存储(Block Storage),对象存储(Object Storage)以及共享⽂件系统(SFS)服务。

此处就不对Rook Ceph进⾏太多的介绍,直接步⼊正题,⼀步⼀步地演练Rook-Ceph从安装部署到使⽤的整个过程。

注意:本⽂所有的yaml⽂件都是为了配合整个演练过程⽽设计的,理论上不能直接⽤在⽣产环境。

如有需要,在⽤在⽣产环境之前,请确保所需的参数都已正确配置。

安装Kubernetes
安装Kubernetes。

安装⽅法有很多,不怕⿇烦可以徒⼿安装,也可以直接使⽤云供应商的托管服务,⽐如Azure AKS,也可以使⽤Rancher RKE,在此就不赘述了。

安装Rook Ceph Operator
这⾥我们基于Ceph来讨论。

事实上Rook⽀持Ceph、Cassandra、CockroachDB、EdgeFS、NFS以及YugabyteDB等多种存储供应商(Storage Provider),针对不同的存储供应商,Rook提供不同的Operator来进⾏资源的部署和管理。

使⽤下⾯的命令来安装Rook Ceph Operator:
helm repo add rook-release https://charts.rook.io/release
kubectl create namespace rook-ceph
helm install --namespace rook-ceph rook-ceph rook-release/rook-ceph
安装Ceph集群(Ceph Cluster)
可以使⽤下⾯的yaml⽂件:
# ceph-cluster-deploy.yaml
apiVersion: ceph.rook.io/v1
kind: CephCluster
metadata:
name: rook-ceph
namespace: rook-ceph
spec:
cephVersion:
image: ceph/ceph:v15.2.7
allowUnsupported: false
dataDirHostPath: /var/lib/rook
skipUpgradeChecks: false
continueUpgradeAfterChecksEvenIfNotHealthy: false
mon:
count: 3
allowMultiplePerNode: false
mgr:
modules:
- name: pg_autoscaler
enabled: true
dashboard:
enabled: true
ssl: true
monitoring:
enabled: false
rulesNamespace: rook-ceph
network:
crashCollector:
disable: false
cleanupPolicy:
confirmation: ""
sanitizeDisks:
method: quick
dataSource: zero
iteration: 1
allowUninstallWithVolumes: false
annotations:
labels:
resources:
removeOSDsIfOutAndSafeToRemove: false
useAllNodes: true
useAllDevices: false
deviceFilter: nvme1n1
config:
osdsPerDevice: "1"
disruptionManagement:
managePodBudgets: false
osdMaintenanceTimeout: 30
pgHealthCheckTimeout: 0
manageMachineDisruptionBudgets: false
machineDisruptionBudgetNamespace: openshift-machine-api
healthCheck:
daemonHealth:
mon:
disabled: false
interval: 45s
osd:
disabled: false
interval: 60s
status:
disabled: false
interval: 60s
livenessProbe:
mon:
disabled: false
mgr:
disabled: false
osd:
disabled: false
然后使⽤以下命令创建Ceph集群:
kubectl create -f ceph-cluster-deploy.yaml
命令执⾏成功之后,需要等待⼏分钟,以便OSD能够成功启动。

执⾏下⾯的命令可以查看所有容器的状态:
kubectl -n rook-ceph get pod
正常情况下,应该可以看到类似以下的结果:
NAME READY STATUS RESTARTS AGE
csi-cephfsplugin-provisioner-d77bb49c6-n5tgs 5/5 Running 0 140s
csi-cephfsplugin-provisioner-d77bb49c6-v9rvn 5/5 Running 0 140s
csi-cephfsplugin-rthrp 3/3 Running 0 140s
csi-rbdplugin-hbsm7 3/3 Running 0 140s
csi-rbdplugin-provisioner-5b5cd64fd-nvk6c 6/6 Running 0 140s
csi-rbdplugin-provisioner-5b5cd64fd-q7bxl 6/6 Running 0 140s
rook-ceph-crashcollector-minikube-5b57b7c5d4-hfldl 1/1 Running 0 105s
rook-ceph-mgr-a-64cd7cdf54-j8b5p 1/1 Running 0 77s
rook-ceph-mon-a-694bb7987d-fp9w7 1/1 Running 0 105s
rook-ceph-mon-b-856fdd5cb9-5h2qk 1/1 Running 0 94s
rook-ceph-mon-c-57545897fc-j576h 1/1 Running 0 85s
rook-ceph-operator-85f5b946bd-s8grz 1/1 Running 0 92m
rook-ceph-osd-0-6bb747b6c5-lnvb6 1/1 Running 0 23s
rook-ceph-osd-1-7f67f9646d-44p7v 1/1 Running 0 24s
rook-ceph-osd-2-6cd4b776ff-v4d68 1/1 Running 0 25s
rook-ceph-osd-prepare-node1-vx2rz 0/2 Completed 0 60s
rook-ceph-osd-prepare-node2-ab3fd 0/2 Completed 0 60s
rook-ceph-osd-prepare-node3-w4xyz 0/2 Completed 0 60s
需要注意⼏点:
ceph-cluster-deploy.yaml并没有包括所有的Ceph集群创建的参数,可以参考https://rook.io/docs/rook/v1.5/ceph-cluster-crd.html 来了解所有的配置信息
这⾥的deviceFilter: nvme1n1是⽤来指定在每个Kubernetes节点上应该使⽤的卷(Volume)的名称。

这个Volume不能格式化成任何⽂件系统,否则Ceph将不会使⽤它作为存储卷。

我在AWS上创建了独⽴的EBS卷,然后直接Attach到Kubernetes节点的机器上,通过lsblk命令即可获得卷的名称,将这个名称填⼊deviceFilter设置即可
安装Rook Toolbox
Rook Toolbox是⼀个运⾏在rook-ceph命名空间下的容器,通过它可以执⾏⼀些Ceph的管理任务,建议安装,还是挺实⽤的。

创建⼀个yaml⽂件:
# rook-toolbox.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: rook-ceph-tools
namespace: rook-ceph
labels:
app: rook-ceph-tools
spec:
replicas: 1
selector:
matchLabels:
app: rook-ceph-tools
template:
metadata:
labels:
app: rook-ceph-tools
spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: rook-ceph-tools
image: rook/ceph:v1.5.3
command: ["/tini"]
args: ["-g", "--", "/usr/local/bin/toolbox.sh"]
imagePullPolicy: IfNotPresent
env:
- name: ROOK_CEPH_USERNAME
valueFrom:
secretKeyRef:
name: rook-ceph-mon
key: ceph-username
- name: ROOK_CEPH_SECRET
valueFrom:
secretKeyRef:
name: rook-ceph-mon
key: ceph-secret
volumeMounts:
- mountPath: /etc/ceph
name: ceph-config
- name: mon-endpoint-volume
mountPath: /etc/rook
volumes:
- name: mon-endpoint-volume
configMap:
name: rook-ceph-mon-endpoints
items:
- key: data
path: mon-endpoints
- name: ceph-config
emptyDir: {}
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 5
然后:
kubectl create -f rook-toolbox.yaml
接着可以执⾏下⾯的命令,进⼊Rook Toolbox容器:
kubectl -n rook-ceph exec -it deploy/rook-ceph-tools -- bash
然后使⽤ceph status命令来查看集群的状态。

正常的话应该可以看到类似下⾯的结果:
$ ceph status
cluster:
id: a0452c76-30d9-4c1a-a948-5d8405f19a7c
health: HEALTH_OK
services:
mon: 3 daemons, quorum a,b,c (age 3m)
mgr: a(active, since 2m)
osd: 3 osds: 3 up (since 1m), 3 in (since 1m)
部署块存储(Provisioning Block Storage)
使⽤下⾯的yaml:
# ceph-block-deploy.yaml
apiVersion: ceph.rook.io/v1
kind: CephBlockPool
metadata:
name: replicapool
namespace: rook-ceph
spec:
failureDomain: host
replicated:
size: 3
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: rook-ceph-block
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner:
parameters:
clusterID: rook-ceph
pool: replicapool
imageFormat: "2"
imageFeatures: layering
csi.storage.k8s.io/provisioner-secret-name: rook-csi-rbd-provisioner
csi.storage.k8s.io/provisioner-secret-namespace: rook-ceph
csi.storage.k8s.io/controller-expand-secret-name: rook-csi-rbd-provisioner
csi.storage.k8s.io/controller-expand-secret-namespace: rook-ceph
csi.storage.k8s.io/node-stage-secret-name: rook-csi-rbd-node
csi.storage.k8s.io/node-stage-secret-namespace: rook-ceph
csi.storage.k8s.io/fstype: ext4
reclaimPolicy: Retain
然后:
Kubectl create -f ceph-block-deploy.yaml
在这个yaml中,同时定义了名为rook-ceph-block的StorageClass,⽤以在pods启动的时候能够动态创建基于Ceph的块存储(通过pool: replicapool的设置指定)。

此外,在这个
StorageClass中,设定了storageclass.kubernetes.io/is-default-class: "true"。

因此,在PersistentVolumeClaim中即使没有指定storageClassName,Kubernetes也会默认使⽤Ceph块存储来保存
app的数据。

部署对象存储(Provisioning Object Storage)
使⽤下⾯的yaml:
# ceph-s3-deploy.yaml
apiVersion: ceph.rook.io/v1
kind: CephObjectStore
metadata:
name: my-store
namespace: rook-ceph
spec:
metadataPool:
failureDomain: host
replicated:
size: 3
dataPool:
failureDomain: host
erasureCoded:
dataChunks: 2
codingChunks: 1
preservePoolsOnDelete: true
gateway:
type: s3
sslCertificateRef:
port: 80
# securePort: 443
instances: 3
healthCheck:
bucket:
disabled: false
interval: 60s
然后:
kubectl create -f ceph-s3-deploy.yaml
等待⼏分钟后,执⾏下⾯的命令:
kubectl -n rook-ceph get pod -l app=rook-ceph-rgw
此时应该可以在pod的列表中看到名字包含有rgw的pod处于Running状态。

接下来就是要在对象存储上创建Bucket。

官⽅提供了。

这⾥介绍另⼀种⽅式,就是借⽤MINIO的管理⼯具来创建。

使⽤下⾯的shell脚本:
# setup-s3-storage.sh
#! /bin/bash
echo "Creating Ceph User"
CREATE_USER_OUTPUT=`kubectl -n rook-ceph exec -it $(kubectl -n rook-ceph get pod -l "app=rook-ceph-tools" -o jsonpath='{.items[0]}') -- radosgw-admin user create --uid=system-user --display-name=system-user --system ACCESS_KEY=$(echo $CREATE_USER_OUTPUT | jq -r ".keys[0].access_key")
SECRET_KEY=$(echo $CREATE_USER_OUTPUT | jq -r ".keys[0].secret_key")
echo "User was created successfully"
echo "S3 ACCESS KEY = $ACCESS_KEY"
echo "S3 SECRET KEY = $SECRET_KEY"
echo "Creating Ceph S3 Bucket"
kubectl -n rook-ceph exec -it $(kubectl -n rook-ceph get pod -l "app=rook-ceph-tools" -o jsonpath='{.items[0]}') -- curl https://dl.min.io/client/mc/release/linux-amd64/mc --output mc
kubectl -n rook-ceph exec -it $(kubectl -n rook-ceph get pod -l "app=rook-ceph-tools" -o jsonpath='{.items[0]}') -- chmod +x mc
kubectl -n rook-ceph exec -it $(kubectl -n rook-ceph get pod -l "app=rook-ceph-tools" -o jsonpath='{.items[0]}') -- ./mc config host add mys3 http://rook-ceph-rgw-signals-store/ "$ACCESS_KEY" "$SECRET_KEY"
kubectl -n rook-ceph exec -it $(kubectl -n rook-ceph get pod -l "app=rook-ceph-tools" -o jsonpath='{.items[0]}') -- ./mc mb mys3/data
echo "Ceph S3 Bucket created successfully"
echo "S3 ACCESS KEY = $ACCESS_KEY"
echo "S3 SECRET KEY = $SECRET_KEY"
在确保了当前机器上安装了jq后,执⾏:
chmod +x setup-s3-storage.sh
./setup-s3-storage.sh
此时会输出S3的Access Key和Secret Key。

创建的Bucket名为data。

验证Object Storage是否部署成功,⾸先执⾏下⾯的命令进⼊Rook Toolbox:
kubectl -n rook-ceph exec -it deploy/rook-ceph-tools -- bash
然后执⾏:
export AWS_HOST=<host>
export AWS_ENDPOINT=<endpoint>
export AWS_ACCESS_KEY_ID=<accessKey>
export AWS_SECRET_ACCESS_KEY=<secretKey>
为rgw Service的DNS主机名。

如果你的Object Storage名为my-store,那么主机名就是rook-ceph-rgw-my-store.rook-ceph
为rgw Service的端点。

执⾏kubectl -n rook-ceph get svc rook-ceph-rgw-my-store,然后将ClusterIP和端⼝号拼接起来作为endpoint的值
accessKey:上⼀步获得的Access Key
secretKey:上⼀步获得的Secret Key
以下是⼀个例⼦:
export AWS_HOST=rook-ceph-rgw-my-store.rook-ceph
export AWS_ENDPOINT=10.104.35.31:80
export AWS_ACCESS_KEY_ID=XEZDB3UJ6X7HVBE7X7MA
export AWS_SECRET_ACCESS_KEY=7yGIZON7EhFORz0I40BFniML36D2rl8CQQ5kXU6l
接下来,安装⼀个s3cmd的⼯具:
yum --assumeyes install s3cmd
然后随便写⼀些内容到rookObj⽂件:
echo "Hello Rook" > /tmp/rookObj
然后通过s3cmd,将这个⽂件保存到S3:
s3cmd put /tmp/rookObj --no-ssl --host=${AWS_HOST} --host-bucket= s3://data
注意--host-bucket=后的空格。

然后,使⽤s3cmd从Bucket将⽂件下载并另存为另⼀个⽂件:
s3cmd get s3://data/rookObj /tmp/rookObj-download --no-ssl --host=${AWS_HOST} --host-bucket=
最后,通过cat命令,查看下载下来的⽂件的内容是否正确:
cat /tmp/rookObj-download
如果能够看到Hello Rook的输出字样,表⽰⼀切正常。

接下来就可以在app中使⽤Ceph Block Storage和Ceph Object Storage了。

下⼀步
之后我会使⽤Microsoft .NET 5,编写⼀个Web API应⽤程序并部署到Kubernetes环境,演⽰如何在app中使⽤本⽂所部署的Ceph Block Storage和Ceph Object Storage。

相关文档
最新文档