K8s使用cephfs(静态和动态两种)

news/2024/9/20 23:22:16/文章来源:https://www.cnblogs.com/boradviews/p/18394180

一、K8s节点安装 ceph-common

CephFSCeph 中基于RADOS(可扩展分布式对象存储)构建,通过将文件数据划分为对象并分布到集群中的多个存储节点上来实现高可用性和可扩展性。

首先所有 k8s 节点都需要安装 ceph-common 工具:

yum -y install epel-release ceph-common

二、静态供给方式

参考链接: K8s 使用 CephFS 作为后端存储(静态供给、动态供给)

静态供给方式需要提前创建好 CephFS 给到 K8s 使用。

2.1 在 Ceph 中创建 FS 和 授权用户

创建存储池:

# 数据存储池
[root@k8s-master31 ~]# ceph osd pool create cephfs_data_pool 16# 元数据存储池
[root@k8s-master31 ~]# ceph osd pool create ceph_metadata_pool 8

创建 FS

[root@k8s-master31 ~]# ceph fs new k8s-cephfs cephfs_data_pool ceph_metadata_pool[root@k8s-master31 ~]# ceph orch apply mds cephfs --placement="3 k8s-master31 k8s-node34 k8s-node35"
Scheduled mds.cephfs update...--placement="3 k8s-master31 k8s-node34 k8s-node35":这部分指定了 MDS 服务的部署位置。具体解释如下:
3 表示你希望部署 3 个 MDS 实例。
k8s-master31 k8s-node34 k8s-node35 是你指定的节点,表示这 3 个 MDS 实例将分别部署在 k8s-master31, k8s-node34, 和 k8s-node35 这三台服务器上。

创建用户 fs-user 并授权存储池 cephfs_data_pool

查看 admin 用户秘钥:

[root@k8s-master31 ~]# ceph auth get-key client.admin

在这里插入图片描述

2.2 在 k8s 中创建 secret

[root@k8s-master31 cephfs]# ceph auth get-key client.admin
AQAeg9FmYRqZGxAACbp761MR8Uf+D3VQTu0nwQ==
[root@k8s-master31 cephfs]# export ADMIN_USER_SECRET='AQAeg9FmYRqZGxAACbp761MR8Uf+D3VQTu0nwQ=='
[root@k8s-master31 cephfs]# kubectl create secret generic ceph-admin-default-secret --type="kubernetes.io/rbd" \
--from-literal=key=$ADMIN_USER_SECRET \
--namespace=default

在这里插入图片描述

2.3 pod 直接使用 CephFS 存储

vi cephfs-test-pod.yml
apiVersion: v1
kind: Pod
metadata:name: cephfs-test-pod
spec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: data-volumemountPath: /usr/share/nginx/html/volumes:- name: data-volumecephfs:monitors:- 10.0.0.31:6789- 10.0.0.34:6789- 10.0.0.35:6789path: /user: adminsecretRef:name: ceph-admin-default-secret
kubectl apply -f cephfs-test-pod.yml

查看 pod

kubectl get pods

在这里插入图片描述

可以进到 pod 中查看分区情况:

kubectl exec -it cephfs-test-pod -- /bin/bashdf -hlPS: 无法创建文件

在这里插入图片描述

2.4 创建 PV 使用 CephFS 存储

vi cephfs-test-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:name: cephfs-test-pv
spec:accessModes: ["ReadWriteOnce"]capacity:storage: 2GipersistentVolumeReclaimPolicy: Retaincephfs:monitors:- 10.0.0.31:6789- 10.0.0.34:6789- 10.0.0.35:6789path: /user: adminsecretRef:name: ceph-admin-default-secret
[root@k8s-master31 cephfs]# kubectl apply -f cephfs-test-pv.yml
Warning: spec.cephfs: deprecated in v1.28, non-functional in v1.31+
persistentvolume/cephfs-test-pv created

Deprecated in v1.28: 从 Kubernetes 1.28 版本开始,spec.cephfs 配置项已被标记为不推荐使用,这意味着它将在未来的版本中被移除或替换。
Non-functional in v1.31+: 从 Kubernetes 1.31 版本起,这个配置将不再有效。

在这里插入图片描述

创建 PVC 绑定 PV

vi cephfs-test-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: cephfs-test-pvc
spec:accessModes: ["ReadWriteOnce"]resources:requests:storage: 2Gi
kubectl apply -f cephfs-test-pvc.yml

查看 pvcpv

kubectl get pvckubectl get pv

在这里插入图片描述

测试 pod 挂载 pvc

vi cephfs-test-pod1.yml
apiVersion: v1
kind: Pod
metadata:name: cephfs-test-pod1
spec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: data-volumemountPath: /usr/share/nginx/html/volumes:- name: data-volumepersistentVolumeClaim:claimName: cephfs-test-pvcreadOnly: false
kubectl apply -f cephfs-test-pod1.yml

查看 pod

kubectl get pods

在这里插入图片描述

进入pod创建文件

[root@k8s-master31 cephfs]# kubectl exec -it cephfs-test-pod1 -- bash
root@cephfs-test-pod1:/usr/share/nginx/html# df -hl /usr/share/nginx/html
Filesystem      Size  Used Avail Use% Mounted on
ceph-fuse       973G     0  973G   0% /usr/share/nginx/html
root@cephfs-test-pod1:/# cd /usr/share/nginx/html/
root@cephfs-test-pod1:/usr/share/nginx/html# echo www > rm.txt
root@cephfs-test-pod1:/usr/share/nginx/html# cat rm.txt
www
root@cephfs-test-pod1:/usr/share/nginx/html# ls -l rm.txt
-rw-r--r-- 1 root root 4 Sep  3 01:39 rm.txt

三、动态供给方式

参考链接: 带有 CSI 驱动程序的 Kubernetes CephFS 卷

首先我们需要为存储提供者创建一个命名空间:

kubectl create ns ceph-csi-cephfs

登录CEPH集群并获取配置:

[root@k8s-master31 ~]# ceph config generate-minimal-conf
# minimal ceph.conf for e3df76f6-66a9-11ef-b055-000c29e945c2
[global]fsid = e3df76f6-66a9-11ef-b055-000c29e945c2mon_host = [v2:10.0.0.31:3300/0,v1:10.0.0.31:6789/0] [v2:10.0.0.34:3300/0,v1:10.0.0.34:6789/0] [v2:10.0.0.35:3300/0,v1:10.0.0.35:6789/0][root@k8s-master31 ~]# ceph auth get-key client.admin
AQAeg9FmYRqZGxAACbp761MR8Uf+D3VQTu0nwQ==

创建子卷组

[root@k8s-master31 ~]# ceph fs subvolumegroup create cephfs csi
[root@k8s-master31 ~]# ceph fs subvolumegroup ls cephfs
[{"name": "csi"}
]

添加helm仓库

helm repo add ceph-csi https://ceph.github.io/csi-charts
helm show values ceph-csi/ceph-csi-cephfs > defaultValues.yaml

编辑 values

vi values.yaml
---
csiConfig:- clusterID: e3df76f6-66a9-11ef-b055-000c29e945c2 #集群IDmonitors:- 10.0.0.31:6789- 10.0.0.34:6789- 10.0.0.35:6789cephFS:subvolumeGroup: "csi"
secret:name: csi-cephfs-secretadminID: admin  #用户adminKey: AQAeg9FmYRqZGxAACbp761MR8Uf+D3VQTu0nwQ== #密钥create: true
storageClass:create: truename: csi-cephfs-scclusterID: e3df76f6-66a9-11ef-b055-000c29e945c2 #集群ID#(必选)创建卷的CephFS文件系统名称fsName: cephfsreclaimPolicy: DeleteallowVolumeExpansion: truevolumeNamePrefix: "poc-k8s-"provisionerSecret: csi-cephfs-secretcontrollerExpandSecret: csi-cephfs-secretnodeStageSecret: csi-cephfs-secret

helm安装

helm upgrade --install ceph-csi-cephfs ceph-csi/ceph-csi-cephfs --values ./values.yaml

部署pvc测试

vi pvc.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: csi-cephfs-pvc
spec:accessModes:- ReadWriteManyresources:requests:storage: 1GistorageClassName: csi-cephfs-sc

创建pvc查看

kubectl apply -f pvc.yaml[root@k8s-master31 ~]# kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS    AGE
csi-cephfs-pvc   Bound    pvc-ac8b431f-e807-4e7f-91ec-70dc55270e05   1Gi        RWX            csi-cephfs-sc   7m37s

部署两个pod挂载pvc测试

创建两个pod
cat <<'EOF'> pod.yaml
apiVersion: v1
kind: Pod
metadata:name: cephfs-test-pod1
spec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: data-volumemountPath: /usr/share/nginx/html/volumes:- name: data-volumepersistentVolumeClaim:claimName: csi-cephfs-pvcreadOnly: false
---
apiVersion: v1
kind: Pod
metadata:name: cephfs-test-pod2
spec:containers:- name: nginximage: nginximagePullPolicy: IfNotPresentvolumeMounts:- name: data-volumemountPath: /usr/share/nginx/html/volumes:- name: data-volumepersistentVolumeClaim:claimName: csi-cephfs-pvcreadOnly: false
EOF
kubectl apply -f pod.yaml
pod2创建文件
[root@k8s-master31 ~]# kubectl exec -it cephfs-test-pod2 -- bash
root@cephfs-test-pod2:/# cd /usr/share/nginx/html/
root@cephfs-test-pod2:/usr/share/nginx/html# ls
root@cephfs-test-pod2:/usr/share/nginx/html# touch 1{1..5}.txt
root@cephfs-test-pod2:/usr/share/nginx/html# ls
11.txt  12.txt  13.txt  14.txt  15.txt
pod1查看
[root@k8s-master31 ~]# kubectl exec -it cephfs-test-pod1 -- bash
root@cephfs-test-pod1:/#  cd /usr/share/nginx/html/
root@cephfs-test-pod1:/usr/share/nginx/html# ls
11.txt  12.txt  13.txt  14.txt  15.txt
root@cephfs-test-pod1:/usr/share/nginx/html#

测试使用 volumeClaimTemplates 动态创建 pv 和 pvc

vi mysql.yml
# headless service
apiVersion: v1
kind: Service
metadata:name: mysql-hllabels:app: mysql-hl
spec:clusterIP: Noneports:- name: mysql-portport: 3306selector:app: mysql---
# NodePort service
apiVersion: v1
kind: Service
metadata:name: mysql-nplabels:app: mysql-np
spec:clusterIP:ports:- name: master-portport: 3306nodePort: 31306targetPort: 3306selector:app: mysqltype: NodePortexternalTrafficPolicy: Cluster---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysql
spec:serviceName: "mysql-hl"replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- name: mysqlimage: mysql:8.0.20ports:- containerPort: 3306name: master-portenv:- name: MYSQL_ROOT_PASSWORDvalue: "root"- name: TZvalue: "Asia/Shanghai"volumeMounts:- name: mysql-datamountPath: /var/lib/mysqlvolumeClaimTemplates:- metadata:name: mysql-dataspec:accessModes: ["ReadWriteOnce"]storageClassName: csi-cephfs-scresources:requests:storage: 2Gi
kubectl apply -f mysql.yaml
查看
[root@k8s-master31 ~]# kubectl get pod mysql-0 -o wide
NAME      READY   STATUS    RESTARTS   AGE     IP              NODE         NOMINATED NODE   READINESS GATES
mysql-0   1/1     Running   0          3m32s   172.16.192.11   k8s-node35   <none>           <none>
[root@k8s-master31 ~]# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                        STORAGECLASS    REASON   AGE      csi-cephfs-sc            28m
pvc-cf0ea7e3-e320-4482-844e-c866190d1a2a   2Gi        RWO            Delete           Bound    default/mysql-data-mysql-0   csi-cephfs-sc            4m29s
[root@k8s-master31 ~]# kubectl get pvc
NAME                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS    AGE
mysql-data-mysql-0   Bound    pvc-cf0ea7e3-e320-4482-844e-c866190d1a2a   2Gi        RWO            csi-cephfs-sc   4m33s
登入mysql
[root@k8s-master31 ~]#  kubectl exec -it mysql-0 -- bash
root@mysql-0:/# mysql -u root -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.20 MySQL Community Server - GPLCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
# 查看库占用磁盘大小
mysql> SELECT table_schema AS "Database",->        ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"-> FROM information_schema.tables-> GROUP BY table_schema;
+--------------------+-----------+
| Database           | Size (MB) |
+--------------------+-----------+
| information_schema |      0.00 |
| mysql              |      7.69 |
| performance_schema |      0.00 |
| sys                |      0.02 |
+--------------------+-----------+
4 rows in set (0.27 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/791520.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3+NestJS+Vite4+TS4+Mysql8+Nginx全栈开发企业级管理后台

Vue3+NestJS+Vite4+TS4+Mysql8+Nginx全栈开发企业级管理后台vite打包快的原因:冷启动1.esbuild构建依赖,go语言编写多线程打包。2.原生的esm方式提供源码,浏览器分担了一部分工作。 HMR热更新1.缓存机制,利用浏览器http头部,源码模块请求根据304协商缓存和依赖模块请求通过…

CUDA常见驱动程序兼容性问题一览

CUDA常见驱动程序兼容性问题一览关注TechLead,复旦博士,分享云服务领域全维度开发技术。拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,复旦机器人智能实验室成员,国家级大学生赛事评审专家,发表多篇SCI核心期刊学术论文,阿里云认证的资深架构师,上亿营收AI产…

Vue3+TypeScript+Vite+Pinia+ElementPlus开发项目在线医疗服务平台

Vue3+TypeScript+Vite+Pinia+ElementPlus开发项目在线医疗服务平台 前言 随着vue3.0的越来越受欢迎,开始有许多公司和个人开始学习并使用vue3开发项目。我从接触学习并使用vue2,到现在的vue3,工作中也一直在使用vue。vue3也发布很长时间了,目前vue3+vite+ts再结合一些优秀…

idea 设置代码样式

设置类注释模板类注释:File-->Settings-->Editor-->File and CodeTemplates将如下代码拷贝到上图右侧空白区域即可(这个更为详细,具体用哪个自己决定) /**** @description * @author ${USER}* @date ${DATE}* @version 1.0* @email marydon20170307@163.com*/需要…

第1天-行业介绍和计算机基础

一、 简单总结计算机发展相关历史,详细总结服务器硬件和计算机分类相关知识。 第一代计算机(1946-1957) 电子管时代 第二代计算机(1958-1964) 晶体管时代 第三代计算机(1965-1970) 集成电路时代 第四代计算机(1971以后) 大规模集成电路时代 服务器硬件:内存、cpu、硬盘、raid卡…

莫言语录

走近一个人的时候,要慢一点,以免看不清。离开一个人的时候,要快一点,以免舍不得。人生海海,先有不甘,后有心安。年人的世界做筛选,不做教育。说服一个人的从来不是道理 ,而是南墙。能点醒一个人的,从来不是说教,而是磨难 。人生是一个漫长的修行,我们每个人都在为自…

产品经理与项目经理:职场双子星的深度解析与全面对比

在现代商业环境中,产品经理和项目经理是两个至关重要的角色(产品经理的英文缩写是 PM(Product Manager),而项目经理的英文缩写是 PM(Project Manager)。)。尽管他们都以“经理”为名,但在实际工作中却扮演着截然不同的角色。 本文将从日常工作、专业技能、职能划分、工…

P7技术专家30k前端架构-商用级产品架构,业务实现+开发提效双线并进

P7技术专家30k前端架构训练营课程-商用级产品架构,业务实现+开发提效双线并进P7技术专家30k前端架构-商用级产品架构,业务实现+开发提效双线并进最近部门招聘,很多工程师,包括我在内都参与了内推和面试的过程,经过这次招聘,我发现能够最终拿到offer的人,基本上在看到简历…

在 PbootCMS 首页上调用公司简介等单页内容

在 PbootCMS 首页上调用公司简介等单页内容 在 PbootCMS 中,可以在首页或其他页面上调用特定的单页内容,如公司简介。以下是如何使用标签 {pboot:content} 来调用单页内容的具体方法: 示例代码html{pboot:content id=1} [content:content drophtml=1 dropblank=1 len=300 mo…

最近写贪吃蛇有些上瘾,canvas版本贪吃蛇,贪吃蛇是逻辑最简单的游戏了

代码:<!Doctype html> <html lang="zh_cn"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>贪吃蛇</title><meta name="Keywords" content="&quo…

PLC结构化文本(ST)——方法(Method)

PLC Structured Text Object Oriented Programming PLC结构化文本(ST)——方法(Method) 什么是方法一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块。---C#方法方法是语句的集合,它们在一起执行一个功能。方法是解决一类问题的步骤的有序组合 方法包含于…