posts - 177,comments - 14,views - 40万

参考文献:https://yq.aliyun.com/articles/613036

相对于静态存储, 动态存储的优势:

                ● 管理员无需预先创建大量的PV作为存储资源;

                ● 静态存储需要用户申请PVC时保证容量和读写类型与预置PV的容量及读写类型完全匹配, 而动态存储则无需如此.

首先创建好nfs服务

1、创建ServiceAccount资源

$ vim serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner         # serviceaccount名称,与下文对应
  namespace: testing            # serviceaccount属于名称空间级别的资源  

2、创建ClusterRole资源

复制代码
$ vim clusterrole.yaml

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-provisioner-runner              # clusterrole名称,clusterrole属于集群级别的资源
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["services", "endpoints"]
    verbs: ["get"]
  - apiGroups: ["extensions"]
    resources: ["podsecuritypolicies"]
    resourceNames: ["nfs-provisioner"]
    verbs: ["use"]
复制代码

3、创建ClusterRoleBinding资源,将clusterrole与serviceaccount二者绑定

复制代码
$ vim clusterrolebinding.yaml

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner               #clusterrolebinding的名称,后文会使用
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
    namespace: testing
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
复制代码

4、创建provisioner

复制代码
$ vim deployment-provisioner.yaml 

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-client-provisioner
  namespace: testing
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccount: nfs-provisioner
      containers:
        - name: nfs-client-provisioner
          image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner                            # 此处使用阿里云镜像
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes           #此处写死
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs                        # 此处名称自定义,需与下文统一
            - name: NFS_SERVER
              value: 192.168.186.81                     # nfs服务主机
            - name: NFS_PATH
              value: /data/nfs                              # nfs共享路径
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.186.81                    # nfs服务主机
            path: /data/nfs                               # nfs共享路径
复制代码

5、创建StorageClass资源

$ vim storageclass-nfs.yaml

apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
  name: managed-nfs-storage          #存储类的名称,后文使用
provisioner: fuseim.pri/ifs

6、创建pvc资源

复制代码
$ vim pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-claim                      #存储类的名称
  namespace: testing                  #StorageClass属于名称空间级别资源
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"   # 此处注解与之前创建的存储类关联
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
复制代码

7、创建pod资源,测试使用情况

复制代码
$ vim pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: vol-sc-pod
  namespace: testing
spec:
  containers:
  - name: nginx
    image: nginx:1.12-alpine
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  - name: alpine
    image: alpine
    volumeMounts:
    - name: html
      mountPath: /html
    command: ["/bin/sh","-c"]
    args:
    - while true; do
        echo $(hostname) $(date) >> /html/index.html;
        sleep 10;
      done
  terminationGracePeriodSeconds: 30
  volumes:
  - name: html
    persistentVolumeClaim:
      claimName: test-claim              # 此处为pvc的名称
复制代码

 

posted on   自然洒脱  阅读(538)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示