繼上一篇介紹Ceph-CSI的RBD對接K8S的文章(K8S使用ceph-csi持久化存儲之RBD)后,本篇章介紹了Ceph-CSI的CephFS對接K8S的使用。請食用!
一、集群和組件版本
K8S集群:1.17.3+
Ceph集群:Octopus(stables)
Ceph-CSI:release-v3.1
snapshotter-controller:release-2.1
Linue kernel:3.10.0-1127.19.1.el7.x86_64 +
注意:1)CephFS的快照功能需要Ceph版本為O版;2)創建了快照的pvc掛載需要內核4.14版本。
-
鏡像版本:
docker pull quay.io/k8scsi/csi-snapshotter:v2.1.1
docker pull quay.io/k8scsi/csi-snapshotter:v2.1.0
docker pull quay.io/k8scsi/csi-resizer:v0.5.0
docker pull quay.io/k8scsi/csi-provisioner:v1.6.0
docker pull quay.io/k8scsi/csi-node-driver-registrar:v1.3.0
docker pull quay.io/k8scsi/csi-attacher:v2.1.1
docker pull quay.io/cephcsi/cephcsi:v3.1-canary
docker pull quay.io/k8scsi/snapshot-controller:v2.0.1
二、部署
1)部署Ceph-CSI
1.1)克隆代碼
# git clone https://github.com/ceph/ceph-csi.git
# cd ceph-csi/deploy/cephfs/kubernetes
1.2)修改yaml文件
1.2.1)配置csi-config-map.yaml文件鏈接ceph集群的信息
# cat csi-config-map.yaml
---
apiVersion: v1
kind: ConfigMap
data:
config.json: |-
[
{
"clusterID": "c7b4xxf7-c61e-4668-9xx0-82c9xx5e3696", // 通過ceph集群的ID
"monitors": [
"xxx.xxx.xxx.xxx:6789"
]
}
]
metadata:
name: ceph-csi-config
1.2.2)部署cephfs相關的CSI
# kubectl apply -f ceph-csi/deploy/cephfs/kubernetes/
# kubectl get pods | grep cephfs
csi-cephfsplugin-8mvmf 3/3 Running 0 2d21h
csi-cephfsplugin-provisioner-78f64778db-42xwc 6/6 Running 0 2d21h
csi-cephfsplugin-provisioner-78f64778db-jzpvd 6/6 Running 0 2d21h
csi-cephfsplugin-provisioner-78f64778db-ntcmd 6/6 Running 0 2d21h
csi-cephfsplugin-zlqzg 3/3 Running 0 2d21h
2)快照功能需要安裝快照控制器支持:
由於上篇 “K8S使用ceph-csi持久化存儲之RBD” 以及部署,這里不需要重復部署。至此,Ceph-CSI和snapshot-controller安裝完成。下面進行功能測試。測試功能前需要在ceph集群中創建對應的存儲池:
// 查看集群狀態
# ceph -s
cluster:
id: c7b43ef7-c61e-4668-9970-82c9775e3696
health: HEALTH_OK
services:
mon: 1 daemons, quorum cka-node-01 (age 24h)
mgr: cka-node-01(active, since 24h), standbys: cka-node-02, cka-node-03
mds: cephfs:1 {0=cka-node-01=up:active} 2 up:standby
osd: 3 osds: 3 up, 3 in
rgw: 1 daemon active (cka-node-01)
task status:
scrub status:
mds.cka-node-01: idle
data:
pools: 7 pools, 184 pgs
objects: 827 objects, 1.7 GiB
usage: 8.1 GiB used, 52 GiB / 60 GiB avail
pgs: 184 active+clean
io:
client: 32 KiB/s rd, 0 B/s wr, 31 op/s rd, 21 op/s wr
// 創建cephfs存儲池fs_metadata,fs_data
# ceph osd pool create fs_metadata 8 8
# ceph osd pool create fs_data 8 8
# ceph fs new cephfs fs_metadata fs_data
// 獲取集群信息和查看用戶key
# ceph mon dump
dumped monmap epoch 3
epoch 3
fsid c7b43ef7-c61e-4668-9970-82c9775e3696
last_changed 2020-09-11 11:05:25.529648
created 2020-09-10 16:22:52.967856
min_mon_release 14 (nautilus)
0: [v2:10.0.xxx.xxx0:3300/0,v1:10.0.xxx.xxx:6789/0] mon.cka-node-01
# ceph auth get client.admin
exported keyring for client.admin
[client.admin]
key = AQBg4llf+9CAGxdsx4tQzS+0OssssiTEQ==
caps mds = "allow *"
caps mgr = "allow *"
caps mon = "allow *"
caps osd = "allow *"
三、驗證
驗證如下功能:
1)創建cephfs類型pvc給pod使用;
2)創建cephfs類型pvc的快照,並驗證基於快照恢復的可用性(CephFS的快照功能需要Ceph版本為O版);
3)創建快照后的pvc重復創建掛載(創建了快照的pvc掛載需要內核4.14版本);
1、創建cephfs類型pvc給pod使用:
1.1) 創建連接ceph集群的秘鑰
# cat secret.yaml
---
apiVersion: v1
kind: Secret
metadata:
name: csi-cephfs-secret
namespace: default
stringData:
# Required for statically provisioned volumes
userID: admin
userKey: AQBg4llf+9CAGdsAds4tQzS+0O7dscB5ZTiTEQ==
# Required for dynamically provisioned volumes
adminID: admin
adminKey: AQBg4llf+9CAGdsAds4tQzS+0O7dscB5ZTiTEQ==
# kubectl apply -f secret.yaml
1.2) 創建storeclass
# cat storageclass.yaml
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: csi-cephfs-sc
provisioner: cephfs.csi.ceph.com
parameters:
clusterID: c7b43ef7-c61e-4668-9970-82c9775e3696
fsName: cephfs
pool: fs_data
rootPath: /test
csi.storage.k8s.io/provisioner-secret-name: csi-cephfs-secret
csi.storage.k8s.io/provisioner-secret-namespace: default
csi.storage.k8s.io/controller-expand-secret-name: csi-cephfs-secret
csi.storage.k8s.io/controller-expand-secret-namespace: default
csi.storage.k8s.io/node-stage-secret-name: csi-cephfs-secret
csi.storage.k8s.io/node-stage-secret-namespace: default
reclaimPolicy: Delete
allowVolumeExpansion: true
mountOptions:
- discard
# kubectl apply -f storageclass.yaml
1.3)基於storeclass創建pvc
# cat pvc.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-cephfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: csi-cephfs-sc
# kubectl apply -f pvc.yaml
# kubectl get pvc csi-cephfs-pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
csi-cephfs-pvc Bound pvc-72b5c1e0-b2d5-4f93-8779-96cb03efa733 1Gi RWX csi-cephfs-sc 2d21h
1.4)創建pod應用pvc
# cat pod.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: csi-cephfs-demo-pod
spec:
containers:
- name: web-server
image: nginx
volumeMounts:
- name: mypvc
mountPath: /var/lib/www
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: csi-cephfs-pvc
readOnly: false
# kubectl apply -f pod.yaml
# kubectl get pods csi-cephfs-demo-pod
NAME READY STATUS RESTARTS AGE
csi-cephfs-demo-pod 1/1 Running 0 2d21h
# kubectl exec -ti csi-cephfs-demo-pod -- bash
root@csi-cephfs-demo-pod:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 199G 7.9G 191G 4% /
tmpfs 64M 0 64M 0% /dev
tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 199G 7.9G 191G 4% /etc/hosts
shm 64M 0 64M 0% /dev/shm
ceph-fuse 1.0G 0 1.0G 0% /var/lib/www
tmpfs 7.9G 12K 7.9G 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs 7.9G 0 7.9G 0% /proc/acpi
tmpfs 7.9G 0 7.9G 0% /proc/scsi
tmpfs 7.9G 0 7.9G 0% /sys/firmware
# 寫入文件,用於后續快照驗證
root@csi-cephfs-demo-pod:/# cd /var/lib/www;echo "abce" > test
root@csi-cephfs-demo-pod:/var/lib/www# cat test
abce
2)創建cephfs類型pvc的快照,並驗證基於快照恢復的可用性(CephFS的快照功能需要Ceph版本為O版):
2.1)創建上一步pvc的快照
# cat snapshot.yaml
---
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
name: cephfs-pvc-snapshot
spec:
volumeSnapshotClassName: csi-cephfsplugin-snapclass
source:
persistentVolumeClaimName: csi-cephfs-pvc
# kubectl apply -f snapshot.yaml
# kubectl get VolumeSnapshot cephfs-pvc-snapshot
NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE
cephfs-pvc-snapshot true csi-cephfs-pvc 1Gi csi-cephfsplugin-snapclass snapcontent-6100977b-96e0-43ae-903a-e8136b0f6ced 2d13h 2d21h
2.2)創建基於快照恢復的pvc
# cat pvc-restore.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cephfs-pvc-restore
spec:
storageClassName: csi-cephfs-sc
dataSource:
name: cephfs-pvc-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
# kubectl apply -f pvc-restore.yaml
2.3)創建pod應用快照恢復的pvc
# cat pod-restore.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: csi-cephfs-restore-demo-pod
spec:
containers:
- name: web-server
image: nginx
volumeMounts:
- name: mypvc
mountPath: /var/lib/www/html
volumes:
- name: mypvc
persistentVolumeClaim:
claimName: cephfs-pvc-restore
readOnly: false
# kubectl apply -f pod-restore.yaml
# kubectl get pods csi-cephfs-restore-demo-pod
NAME READY STATUS RESTARTS AGE
csi-cephfs-restore-demo-pod 1/1 Running 0 2d21h
# kubectl exec -ti csi-cephfs-restore-demo-pod -- bash
root@csi-cephfs-restore-demo-pod:/# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 199G 7.9G 191G 4% /
tmpfs 64M 0 64M 0% /dev
tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 199G 7.9G 191G 4% /etc/hosts
shm 64M 0 64M 0% /dev/shm
ceph-fuse 1.0G 0 1.0G 0% /var/lib/www/html
tmpfs 7.9G 12K 7.9G 1% /run/secrets/kubernetes.io/serviceaccount
tmpfs 7.9G 0 7.9G 0% /proc/acpi
tmpfs 7.9G 0 7.9G 0% /proc/scsi
tmpfs 7.9G 0 7.9G 0% /sys/firmware
root@csi-cephfs-restore-demo-pod:/# cd /var/lib/www/html
root@csi-cephfs-restore-demo-pod:/var/lib/www/html# cat test
abce
//基於快照恢復數據功能正常
3)創建快照后的pvc重復創建掛載(創建了快照的pvc掛載需要內核4.14版本,RBD類型也需要升級):
3.1)刪除csi-cephfs-demo-pod這個pod后重建
# kubectl delete -f pod.yaml
# kubectl apply -f pod.yaml
# kubectl get pods csi-cephfs-demo-pod
NAME READY STATUS RESTARTS AGE
csi-cephfs-demo-pod 0/1 ContainerCreating 0 2d22h
# kubectl describe pods csi-cephfs-demo-pod
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned jonathan/csi-rbd-demo-pod to 10.21.xxx.xxx
Normal SuccessfulAttachVolume 25s attachdetach-controller AttachVolume.Attach succeeded for volume "pvc-d6ed5203-36c7-454b-8a8e-b49a084a0531"
Warning FailedMount 0s (x5 over 9s) kubelet, 10.21.xxx.xxx MountVolume.MountDevice failed for volume "pvc-d6ed5203-36c7-454b-8a8e-b49a084a0531" : rpc error: code = Internal desc = rbd: map failed with error an error (exit status 6) occurred while running rbd args: [--id csi -m 10.xxx.xxx.xxx --keyfile=***stripped*** map volumes/csi-vol-9f2dcd03-f97e-11ea-8104-a608005a6960 --device-type krbd], rbd error output: rbd: sysfs write failed
rbd: map failed: (6) No such device or address
3.2)經過分析,該報錯是因為node節點的Linux內核版本不支持,需要升級內核。此時的內核版本是3.10版本,升級到4.14版本后問題解決。如下:
# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
cka-node-01 Ready master 60d v1.17.3 10.0.xxx.xxx <none> CentOS Linux 7 (Core) 4.14.119-200.el7.x86_64 docker://19.3.12
cka-node-02 Ready <none> 60d v1.17.3 10.0.xxx.xxx <none> CentOS Linux 7 (Core) 4.14.119-200.el7.x86_64 docker://19.3.12
cka-node-03 Ready <none> 60d v1.17.3 10.0.xxx.xxx <none> CentOS Linux 7 (Core) 4.14.119-200.el7.x86_64 docker://19.3.12
# kubectl get pods csi-cephfs-demo-pod
NAME READY STATUS RESTARTS AGE
csi-cephfs-demo-pod 1/1 Running 0 2d01h
注意:目前,每個CephFS文件系統限制為400個快照,如果PVC有快照,也無法刪除,刪除PVC之前,請確保刪除PVC上的所有快照。
完
附:歡迎關注本人公眾號(內有其他分享):