您现在的位置是:首页 >技术杂谈 >k8s持久化存储--腾讯云对象存储cos存储网站首页技术杂谈

k8s持久化存储--腾讯云对象存储cos存储

lTimej 2025-04-01 00:01:02
简介k8s持久化存储--腾讯云对象存储cos存储

一、cos工具安装

参考:GooseFS-Lite 工具

5、将存储桶挂载到本地目录
./bin/goosefs-lite mount /data/nfs/cos cosn://<BucketName>/

二、安装 NFS

NFS 的简介:网络文件系统,英文Network File System(NFS),是由 SUN 公司研制的UNIX表示层协议(presentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样。

yum install -y nfs-utils
systemctl enable rpcbind
systemctl enable nfs-server
systemctl start rpcbind
systemctl start nfs-server

三、NFS配置

# 创建cos共享目录,并设置权限
mkdir -p /data/nfs/cos
chmod -R 777 /data/nfs
# * 表示暴露权限给所有主机
echo "/nfs/data/cos *(fsid=0,insecure,rw,sync,no_root_squash)" > /etc/exports
# 加载配置
exportfs -ra
# 查看配置是否生效
exportfs
# 验证NFS共享
showmount -e <NFS服务器IP>

四、在 Kubernetes 中创建 PV/PVC

4.1、示例yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: cos-pv
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 400M
  mountOptions:
  - lookupcache=pos
  nfs:
    path: /data/nfs/cos
    server: 10.10.27.120
  persistentVolumeReclaimPolicy: Retain
  storageClassName: cos-nfs
  volumeMode: Filesystem

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cos-pvc
  namespace: default
  labels:
    app: cos-pvc
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 400M
  storageClassName: cos-nfs
  volumeMode: Filesystem

---

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.20.2
    resources:
      limits:
        cpu: 200m
        memory: 500Mi
      requests:
        cpu: 100m
        memory: 200Mi
    ports:
    - containerPort:  80
      name:  http
    volumeMounts:
    - name: localtime
      mountPath: /etc/localtime
    - name: html
      mountPath: /usr/share/nginx/html/ 
  volumes:
    - name: localtime
      hostPath:
        path: /usr/share/zoneinfo/Asia/Shanghai
    - name: html    
      persistentVolumeClaim:
        claimName:  cos-pvc
        readOnly: false  
  restartPolicy: Always
4.2、验证挂载
kubectl exec -it nginx bash
df -h
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。