k8s设置nfs为default StorageClass

nfs服务器搭建

yum -y install nfs-utils rpcbind
验证: 
rpm -qa nfs-utils

在服务端创建一个共享目录 /nfs(目录可自定义)

mkdir /nfs
chmod 666 /nfs

修改 NFS 配置文件 vim /etc/exports

vim /etc/exports
/nfs *(rw,sync,insecure,no_subtree_check,no_root_squash)
#重载数据
exportfs -rv

参数说明:

/nfs    欲共享出去的目录,也就是想共享到网络中的文件系统
*       可设置网段、IP、* 表示允许挂在的客户端
(ro,sync,insecure,no_root_squash) 设置 /nfs目录允许所有客户端只读挂载。

更多参数说明可参考:https://blog.csdn.net/sj349781478/article/details/79970739

# 启动rpc
systemctl start rpcbind
systemctl start nfs
#设置开机启动
systemctl enable rpcbind
systemctl enable nfs

状态查看

systemctl status rpcbind
systemctl status nfs

部署StorageClass

下载部署文件

for file in class.yaml deployment.yaml rbac.yaml test-claim.yaml ; do wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/$file ; done

以下为原文件

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"   # 设置为default StorageClass
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
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: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  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: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs    # nfs服务地址
            - name: NFS_SERVER
              value: 192.168.194.129   # nfs服务地址
            - name: NFS_PATH
              value: /nfs  # nfs共享目路径
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.194.129   # nfs服务地址
            path: /nfs   # nfs共享目路径

结果查看

image.png

测试

创建pvc

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: myclaim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 200Mi
  storageClassName: # 不指定会调用默认StorageClass
image.png

你可能感兴趣的:(k8s设置nfs为default StorageClass)