k8s StorageClass(NFS)

nfs服务器自行一键安装

1:创建ServiceAccount,为nfs-client授权。nfs-client-sa.yaml

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: nfs-client-provisioner-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: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: nfs-client-provisioner-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: nfs-client-provisioner
  namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-clusterrole
  apiGroup: rbac.authorization.k8s.io

2:通过上面得配置,设置nfs-client对PV,PVC,StorageClass等规则。接下来我们创建这个YAML文件:

kubectl apply -f nfs-client-sa.yaml

3:使用Deployment来创建nfs-client,配置如下:nfs-client.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # 替换为项目部署的命名空间
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: easzlab/nfs-subdir-external-provisioner:v4.0.1
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: rookieops/nfs
            - name: NFS_SERVER
              value: 192.168.16.51 #nfs地址
            - name: NFS_PATH
              value: /nfs      #nfs文件夹
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.16.51
            path: /nfs 

4:然后创建这个yaml文件

kubectl apply -f nfs-client.yaml 

5:上面创建完成后就可以创建StorageClass了,nfs-client-storageclass.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client-storageclass
provisioner: rookieops/nfs

注意provisioner必须和上面得Deployment的YAML文件中PROVISIONER_NAME的值保持一致。创建这个yaml文件:

kubectl apply -f nfs-client-storageclass.yaml 
[root@baozexu nfs]# kubectl get storageclasses.storage.k8s.io 
NAME                      PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
nfs-client-storageclass   rookieops/nfs           Delete          Immediate              false                  4h30m

6:创建pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  storageClassName: nfs-client-storageclass
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

创建这个yaml文件,观察其状态:

kubectl apply -f test-pvc.yaml
kubectl get pvc

7:创建一个pod,进行测试 pv-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: storageclass-pod
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command:
    - "/bin/sh"
    - "-c"
    args:
    - "sleep 3600"
    volumeMounts:
    - name: nfs-pvc
      mountPath: /mnt
  restartPolicy: Never
  volumes:
  - name: nfs-pvc
    persistentVolumeClaim:
      claimName: pvc1

查看pv是否生成

kubectl get pv

8:然后查看nfs目录中是否有文件生成

[root@baozexu public]# ls
default-pvc1-pvc-33d2aa7f-e52f-4865-a05e-ebd53fbec914  
posted @   朝阳1  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示