k8s-v1.25.0持久化存储ceph-csi方式(ceph-v17.2.3、rbd块存储)
ceph-v17.2.3对接k8s-v1.25.0(动态存储)
1、cephadm部署ceph-v17.2.3集群
2、k8s-v1.25.0部署ceph-csi-v3.7.0
ceph集群+k8s集群(ubuntu22.04)
ceph-v17.2.3
k8s-v1.25.0(containerd-1.6.8)
ceph-csi-v3.7.0
动态pvc
ceph集群操作
ceph osd pool create kubernetes 8 8ceph osd pool application enable kubernetes rbdrbd pool init -p kubernetesceph osd pool ls
ceph mon dump
ceph auth get client.admin 2>&1 |grep "key = " |awk '{print $3'}
ceph auth add client.kube mon 'allow r' osd 'allow rwx pool=kubernetes'ceph auth get-key client.kube
k8s集群操作
apt updateapt install ceph-common=17.2.0-0ubuntu0.22.04.1 -y
mkdir ~/{ceph-csi,000}git clone https://github.com/ceph/ceph-csi.gitcd ~/000 && git clone https://hub.fastgit.xyz/ceph/ceph-csi.git
kubectl create ns ceph-csikubectl describe node master |grep Taintkubectl taint node master node-role.kubernetes.io/master-
1、csi-config-map.yaml
# 1、csi-config-map.yamlcat > ~/ceph-csi/csi-config-map.yaml << 'EOF'---apiVersion: v1kind: ConfigMapdata:config.json: |-[{"clusterID": "4c4d9c82-267d-11ed-888e-000c29db6f93","monitors": ["192.168.1.204:6789","192.168.1.205:6789","192.168.1.206:6789","192.168.1.207:6789"]}]metadata:name: ceph-csi-confignamespace: ceph-csiEOF
clusterID使用
ceph mon dump命令在ceph集群上查看
2、csi-nodeplugin-rbac.yaml
3、csi-provisioner-rbac.yaml
4、csidriver.yaml
5、csi-rbdplugin-provisioner.yaml
6、csi-rbdplugin.yaml
cat > ceph.yml < 'eof'# 2、csi-nodeplugin-rbac.yamlcat > ~/ceph-csi/csi-nodeplugin-rbac.yaml << 'EOF'---apiVersion: v1kind: ServiceAccountmetadata:name: rbd-csi-nodepluginnamespace: ceph-csi---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: rbd-csi-nodepluginrules:- apiGroups: [""]resources: ["nodes"]verbs: ["get"]- apiGroups: [""]resources: ["secrets"]verbs: ["get"]- apiGroups: [""]resources: ["configmaps"]verbs: ["get"]- apiGroups: [""]resources: ["serviceaccounts"]verbs: ["get"]- apiGroups: [""]resources: ["persistentvolumes"]verbs: ["get"]- apiGroups: ["storage.k8s.io"]resources: ["volumeattachments"]verbs: ["list", "get"]- apiGroups: [""]resources: ["serviceaccounts/token"]verbs: ["create"]---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: rbd-csi-nodepluginsubjects:- kind: ServiceAccountname: rbd-csi-nodepluginnamespace: ceph-csiroleRef:kind: ClusterRolename: rbd-csi-nodepluginapiGroup: rbac.authorization.k8s.ioEOF# 3、csi-provisioner-rbac.yamlcat > ~/ceph-csi/csi-provisioner-rbac.yaml << 'EOF'---apiVersion: v1kind: ServiceAccountmetadata:name: rbd-csi-provisionernamespace: ceph-csi---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: rbd-external-provisioner-runnerrules:- apiGroups: [""]resources: ["nodes"]verbs: ["get", "list", "watch"]- apiGroups: [""]resources: ["secrets"]verbs: ["get", "list", "watch"]- apiGroups: [""]resources: ["events"]verbs: ["list", "watch", "create", "update", "patch"]- apiGroups: [""]resources: ["persistentvolumes"]verbs: ["get", "list", "watch", "create", "update", "delete", "patch"]- apiGroups: [""]resources: ["persistentvolumeclaims"]verbs: ["get", "list", "watch", "update"]- apiGroups: [""]resources: ["persistentvolumeclaims/status"]verbs: ["update", "patch"]- apiGroups: ["storage.k8s.io"]resources: ["storageclasses"]verbs: ["get", "list", "watch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshots"]verbs: ["get", "list", "patch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshots/status"]verbs: ["get", "list", "patch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotcontents"]verbs: ["create", "get", "list", "watch", "update", "delete", "patch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotclasses"]verbs: ["get", "list", "watch"]- apiGroups: ["storage.k8s.io"]resources: ["volumeattachments"]verbs: ["get", "list", "watch", "update", "patch"]- apiGroups: ["storage.k8s.io"]resources: ["volumeattachments/status"]verbs: ["patch"]- apiGroups: ["storage.k8s.io"]resources: ["csinodes"]verbs: ["get", "list", "watch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotcontents/status"]verbs: ["update", "patch"]- apiGroups: [""]resources: ["configmaps"]verbs: ["get"]- apiGroups: [""]resources: ["serviceaccounts"]verbs: ["get"]- apiGroups: [""]resources: ["serviceaccounts/token"]verbs: ["create"]---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: rbd-csi-provisioner-rolesubjects:- kind: ServiceAccountname: rbd-csi-provisionernamespace: ceph-csiroleRef:kind: ClusterRolename: rbd-external-provisioner-runnerapiGroup: rbac.authorization.k8s.io---kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:namespace: ceph-csiname: rbd-external-provisioner-cfgrules:- apiGroups: [""]resources: ["configmaps"]verbs: ["get", "list", "watch", "create", "update", "delete"]- apiGroups: ["coordination.k8s.io"]resources: ["leases"]verbs: ["get", "watch", "list", "delete", "update", "create"]---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: rbd-csi-provisioner-role-cfgnamespace: ceph-csisubjects:- kind: ServiceAccountname: rbd-csi-provisionernamespace: ceph-csiroleRef:kind: Rolename: rbd-external-provisioner-cfgapiGroup: rbac.authorization.k8s.ioEOF# 4、csidriver.yamlcat > ~/ceph-csi/csidriver.yaml << 'EOF'apiVersion: storage.k8s.io/v1kind: CSIDrivermetadata:name: "rbd.csi.ceph.com"namespace: ceph-csispec:attachRequired: truepodInfoOnMount: falseEOF# 5、csi-rbdplugin-provisioner.yamlcat > ~/ceph-csi/csi-rbdplugin-provisioner.yaml << 'EOF'---kind: ServiceapiVersion: v1metadata:name: csi-rbdplugin-provisionernamespace: ceph-csilabels:app: csi-metricsspec:selector:app: csi-rbdplugin-provisionerports:- name: http-metricsport: 8080protocol: TCPtargetPort: 8680---kind: DeploymentapiVersion: apps/v1metadata:name: csi-rbdplugin-provisionernamespace: ceph-csispec:replicas: 3selector:matchLabels:app: csi-rbdplugin-provisionertemplate:metadata:labels:app: csi-rbdplugin-provisionerspec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- csi-rbdplugin-provisionertopologyKey: "kubernetes.io/hostname"serviceAccountName: rbd-csi-provisionerpriorityClassName: system-cluster-criticalcontainers:- name: csi-provisionerimage: dyrnq/csi-provisioner:v3.2.1args:- "--csi-address=$(ADDRESS)"- "--v=1"- "--timeout=150s"- "--retry-interval-start=500ms"- "--leader-election=true"- "--feature-gates=Topology=false"- "--feature-gates=HonorPVReclaimPolicy=true"- "--prevent-volume-mode-conversion=true"- "--default-fstype=ext4"- "--extra-create-metadata=true"env:- name: ADDRESSvalue: unix:///csi/csi-provisioner.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- name: csi-snapshotterimage: dyrnq/csi-snapshotter:v6.0.1args:- "--csi-address=$(ADDRESS)"- "--v=1"- "--timeout=150s"- "--leader-election=true"- "--extra-create-metadata=true"env:- name: ADDRESSvalue: unix:///csi/csi-provisioner.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- name: csi-attacherimage: dyrnq/csi-attacher:v3.5.0args:- "--v=1"- "--csi-address=$(ADDRESS)"- "--leader-election=true"- "--retry-interval-start=500ms"env:- name: ADDRESSvalue: /csi/csi-provisioner.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- name: csi-resizerimage: dyrnq/csi-resizer:v1.5.0args:- "--csi-address=$(ADDRESS)"- "--v=1"- "--timeout=150s"- "--leader-election"- "--retry-interval-start=500ms"- "--handle-volume-inuse-error=false"- "--feature-gates=RecoverVolumeExpansionFailure=true"env:- name: ADDRESSvalue: unix:///csi/csi-provisioner.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- name: csi-rbdpluginimage: quay.io/cephcsi/cephcsi:canaryargs:- "--nodeid=$(NODE_ID)"- "--type=rbd"- "--controllerserver=true"- "--endpoint=$(CSI_ENDPOINT)"- "--csi-addons-endpoint=$(CSI_ADDONS_ENDPOINT)"- "--v=5"- "--drivername=rbd.csi.ceph.com"- "--pidlimit=-1"- "--rbdhardmaxclonedepth=8"- "--rbdsoftmaxclonedepth=4"- "--enableprofiling=false"- "--setmetadata=true"env:- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: NODE_IDvalueFrom:fieldRef:fieldPath: spec.nodeName- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: CSI_ENDPOINTvalue: unix:///csi/csi-provisioner.sock- name: CSI_ADDONS_ENDPOINTvalue: unix:///csi/csi-addons.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- mountPath: /devname: host-dev- mountPath: /sysname: host-sys- mountPath: /lib/modulesname: lib-modulesreadOnly: true- name: ceph-csi-configmountPath: /etc/ceph-csi-config/- name: keys-tmp-dirmountPath: /tmp/csi/keys- name: oidc-tokenmountPath: /run/secrets/tokensreadOnly: true- name: csi-rbdplugin-controllerimage: quay.io/cephcsi/cephcsi:canaryargs:- "--type=controller"- "--v=5"- "--drivername=rbd.csi.ceph.com"- "--drivernamespace=$(DRIVER_NAMESPACE)"- "--setmetadata=true"env:- name: DRIVER_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespaceimagePullPolicy: "IfNotPresent"volumeMounts:- name: ceph-csi-configmountPath: /etc/ceph-csi-config/- name: keys-tmp-dirmountPath: /tmp/csi/keys- name: liveness-prometheusimage: quay.io/cephcsi/cephcsi:canaryargs:- "--type=liveness"- "--endpoint=$(CSI_ENDPOINT)"- "--metricsport=8680"- "--metricspath=/metrics"- "--polltime=60s"- "--timeout=3s"env:- name: CSI_ENDPOINTvalue: unix:///csi/csi-provisioner.sock- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIPvolumeMounts:- name: socket-dirmountPath: /csiimagePullPolicy: "IfNotPresent"volumes:- name: host-devhostPath:path: /dev- name: host-syshostPath:path: /sys- name: lib-moduleshostPath:path: /lib/modules- name: socket-diremptyDir: {medium: "Memory"}- name: ceph-csi-configconfigMap:name: ceph-csi-config- name: keys-tmp-diremptyDir: {medium: "Memory"}- name: oidc-tokenprojected:sources:- serviceAccountToken:path: oidc-tokenexpirationSeconds: 3600audience: ceph-csi-kmsEOF# 6、csi-rbdplugin.yamlcat > ~/ceph-csi/csi-rbdplugin.yaml << 'EOF'---kind: DaemonSetapiVersion: apps/v1metadata:name: csi-rbdpluginnamespace: ceph-csispec:selector:matchLabels:app: csi-rbdplugintemplate:metadata:labels:app: csi-rbdpluginspec:serviceAccountName: rbd-csi-nodepluginhostNetwork: truehostPID: truepriorityClassName: system-node-criticaldnsPolicy: ClusterFirstWithHostNetcontainers:- name: driver-registrarsecurityContext:privileged: trueallowPrivilegeEscalation: trueimage: dyrnq/csi-node-driver-registrar:v2.5.1args:- "--v=1"- "--csi-address=/csi/csi.sock"- "--kubelet-registration-path=/var/lib/kubelet/plugins/rbd.csi.ceph.com/csi.sock"env:- name: KUBE_NODE_NAMEvalueFrom:fieldRef:fieldPath: spec.nodeNamevolumeMounts:- name: socket-dirmountPath: /csi- name: registration-dirmountPath: /registration- name: csi-rbdpluginsecurityContext:privileged: truecapabilities:add: ["SYS_ADMIN"]allowPrivilegeEscalation: trueimage: quay.io/cephcsi/cephcsi:canaryargs:- "--nodeid=$(NODE_ID)"- "--pluginpath=/var/lib/kubelet/plugins"- "--stagingpath=/var/lib/kubelet/plugins/kubernetes.io/csi/"- "--type=rbd"- "--nodeserver=true"- "--endpoint=$(CSI_ENDPOINT)"- "--csi-addons-endpoint=$(CSI_ADDONS_ENDPOINT)"- "--v=5"- "--drivername=rbd.csi.ceph.com"- "--enableprofiling=false"env:- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: NODE_IDvalueFrom:fieldRef:fieldPath: spec.nodeName- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: CSI_ENDPOINTvalue: unix:///csi/csi.sock- name: CSI_ADDONS_ENDPOINTvalue: unix:///csi/csi-addons.sockimagePullPolicy: "IfNotPresent"volumeMounts:- name: socket-dirmountPath: /csi- mountPath: /devname: host-dev- mountPath: /sysname: host-sys- mountPath: /run/mountname: host-mount- mountPath: /etc/selinuxname: etc-selinuxreadOnly: true- mountPath: /lib/modulesname: lib-modulesreadOnly: true- name: ceph-csi-configmountPath: /etc/ceph-csi-config/- name: plugin-dirmountPath: /var/lib/kubelet/pluginsmountPropagation: "Bidirectional"- name: mountpoint-dirmountPath: /var/lib/kubelet/podsmountPropagation: "Bidirectional"- name: keys-tmp-dirmountPath: /tmp/csi/keys- name: ceph-logdirmountPath: /var/log/ceph- name: oidc-tokenmountPath: /run/secrets/tokensreadOnly: true- name: liveness-prometheussecurityContext:privileged: trueallowPrivilegeEscalation: trueimage: quay.io/cephcsi/cephcsi:canaryargs:- "--type=liveness"- "--endpoint=$(CSI_ENDPOINT)"- "--metricsport=8680"- "--metricspath=/metrics"- "--polltime=60s"- "--timeout=3s"env:- name: CSI_ENDPOINTvalue: unix:///csi/csi.sock- name: POD_IPvalueFrom:fieldRef:fieldPath: status.podIPvolumeMounts:- name: socket-dirmountPath: /csiimagePullPolicy: "IfNotPresent"volumes:- name: socket-dirhostPath:path: /var/lib/kubelet/plugins/rbd.csi.ceph.comtype: DirectoryOrCreate- name: plugin-dirhostPath:path: /var/lib/kubelet/pluginstype: Directory- name: mountpoint-dirhostPath:path: /var/lib/kubelet/podstype: DirectoryOrCreate- name: ceph-logdirhostPath:path: /var/log/cephtype: DirectoryOrCreate- name: registration-dirhostPath:path: /var/lib/kubelet/plugins_registry/type: Directory- name: host-devhostPath:path: /dev- name: host-syshostPath:path: /sys- name: etc-selinuxhostPath:path: /etc/selinux- name: host-mounthostPath:path: /run/mount- name: lib-moduleshostPath:path: /lib/modules- name: ceph-csi-configconfigMap:name: ceph-csi-config- name: keys-tmp-diremptyDir: {medium: "Memory"}- name: oidc-tokenprojected:sources:- serviceAccountToken:path: oidc-tokenexpirationSeconds: 3600audience: ceph-csi-kms---apiVersion: v1kind: Servicemetadata:name: csi-metrics-rbdpluginnamespace: ceph-csilabels:app: csi-metricsspec:ports:- name: http-metricsport: 8080protocol: TCPtargetPort: 8680selector:app: csi-rbdpluginEOFeof
7、secret.yaml
# 7、secret.yamlcat > ~/ceph-csi/secret.yaml << 'EOF'---apiVersion: v1kind: Secretmetadata:name: csi-rbd-secretnamespace: ceph-csistringData:userID: kubeuserKey: AQD/4gpjDnt0KhAANfaKRnFlXIXVqPdf1IHOUA==EOF
userID就是ceph auth add client.kube mon ‘allow r’ osd ‘allow rwx pool=kubernetes’创建的
kubeuserKey使用
ceph auth get-key client.kube命令在ceph集群上查看
8、storageclass.yaml
# 8、storageclass.yamlcat > ~/ceph-csi/storageclass.yaml << 'EOF'---apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:name: csi-rbd-scprovisioner: rbd.csi.ceph.comparameters:clusterID: 4c4d9c82-267d-11ed-888e-000c29db6f93pool: kubernetesimageFeatures: layeringcsi.storage.k8s.io/provisioner-secret-name: csi-rbd-secretcsi.storage.k8s.io/provisioner-secret-namespace: ceph-csicsi.storage.k8s.io/controller-expand-secret-name: csi-rbd-secretcsi.storage.k8s.io/controller-expand-secret-namespace: ceph-csicsi.storage.k8s.io/node-stage-secret-name: csi-rbd-secretcsi.storage.k8s.io/node-stage-secret-namespace: ceph-csicsi.storage.k8s.io/fstype: ext4reclaimPolicy: DeleteallowVolumeExpansion: truemountOptions:- discardEOF
clusterID在ceph集群上用
ceph mon dump命令查看pool使用
ceph osd pool create kubernetes 8 8命令在ceph集群上创建的kubernetes
下载镜像
# 1、master上操作# 查看镜像find ./ -type f |xargs grep 'image: '|sort|uniq|awk '{print $3}'|grep ^[a-zA-Z]|grep -Evw 'error|kubeRbacProxy'|sort -rn|uniq |grep -n ".*"1:quay.io/cephcsi/cephcsi:canary2:dyrnq/csi-snapshotter:v6.0.13:dyrnq/csi-resizer:v1.5.04:dyrnq/csi-provisioner:v3.2.15:dyrnq/csi-node-driver-registrar:v2.5.16:dyrnq/csi-attacher:v3.5.0#在线预拉取镜像find ./ -type f |xargs grep 'image: '|sort|uniq|awk '{print $3}'|grep ^[a-zA-Z]|grep -Evw 'error|kubeRbacProxy'|sort -rn|uniq |xargs -i docker pull {}find ./ -type f |xargs grep 'image: '|sort|uniq|awk '{print $3}'|grep ^[a-zA-Z]|grep -Evw 'error|kubeRbacProxy'|sort -rn|uniq |xargs -i ctr -n k8s.io i pull {}#2、node上操作cat > pull.sh << 'EOF'quay.io/cephcsi/cephcsi:canarydyrnq/csi-snapshotter:v6.0.1dyrnq/csi-resizer:v1.5.0dyrnq/csi-provisioner:v3.2.1dyrnq/csi-node-driver-registrar:v2.5.1dyrnq/csi-attacher:v3.5.0EOFcat pull.sh |xargs -i docker pull {}cat pull.sh |xargs -i crictl pull {}
kubectl apply \-f csi-config-map.yaml \-f csi-nodeplugin-rbac.yaml \-f csi-provisioner-rbac.yaml \-f csidriver.yamlkubectl apply \-f csi-rbdplugin-provisioner.yaml \-f csi-rbdplugin.yamlkubectl apply \-f secret.yaml \-f storageclass.yaml
kubectl delete \-f storageclass.yaml \-f secret.yamlkubectl delete \-f csi-rbdplugin.yaml \-f csi-rbdplugin-provisioner.yamlkubectl delete \-f csidriver.yaml \-f csi-provisioner-rbac.yaml \-f csi-nodeplugin-rbac.yaml \-f csi-config-map.yaml
测试动态pvc
9、pvc.yaml
cat > ~/000/pvc.yaml << 'EOF'---apiVersion: v1kind: PersistentVolumeClaimmetadata:name: rbd-pvcspec:accessModes:- ReadWriteOnceresources:requests:storage: 1GistorageClassName: csi-rbd-scEOF
10、pod.yaml
cat > ~/000/pod.yaml << 'EOF'---apiVersion: v1kind: Podmetadata:name: csi-rbd-demo-podspec:containers:- name: web-serverimage: nginx:alpineimagePullPolicy: IfNotPresentvolumeMounts:- name: mypvcmountPath: /var/lib/www/htmlvolumes:- name: mypvcpersistentVolumeClaim:claimName: rbd-pvcreadOnly: falseEOF
