前言

    PersistentVolume(PV)是集群中已由管理员配置的一段网络存储。 集群中的资源就像一个节点是一个集群资源。 PV是诸如卷之类的卷插件,但是具有独立于使用PV的任何单个pod的生命周期。 该API对象捕获存储的实现细节,即NFS,iSCSI或云提供商特定的存储系统。

    PersistentVolumeClaim(PVC)是用户存储的请求。 它类似于pod。Pod消耗节点资源,PVC消耗存储资源。 pod可以请求特定级别的资源(CPU和内存)。 权限要求可以请求特定的大小和访问模式。

    StorageClass是Kubernetes提供一种自动创建PV的机制,它的作用就是创建PV的模板。


一、创建 StorageClass

1、查询

[root@k8s-master k8s]# docker search nfs-client-provisioner
NAME                                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
vbouchaud/nfs-client-provisioner              nfs-client-provisioner built from github off…   4
jmgao1983/nfs-client-provisioner              from quay.io/external_storage/nfs-client-pro…   3                                       [OK]
lizhenliang/nfs-client-provisioner                                                            3
...

2、下载

[root@k8s-master k8s]#  docker pull vbouchaud/nfs-client-provisioner
Using default tag: latest
latest: Pulling from vbouchaud/nfs-client-provisioner
Digest: sha256:fc6b7f5d62463ff0d0a09487fd822b25adbfb2dc7d2b67f01025db30f38560c0
Status: Downloaded newer image for vbouchaud/nfs-client-provisioner:latest
docker.io/vbouchaud/nfs-client-provisioner:latest

3、创建 nfs-client-provisioner

(1)nfs-client-provisioner-test.yaml

[root@k8s-master k8s]# cat nfs-client-provisioner-test.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner-test
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: 172.16.10.158:80/nfs-client-provisioner
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: iscas
            - name: NFS_SERVER
              value: 172.16.10.158
            - name: NFS_PATH
              value: /data/nfsdata
      volumes:
        - name: nfs-client-root
          nfs:
            server: 172.16.10.158
            path: /data/nfsdata

(2)创建

[root@k8s-master k8s]# kubectl create -f nfs-client-provisioner-test.yaml
deployment.apps/nfs-client-provisioner-test created

4、创建ServiceAccount、ClusterRole、ClusterRoleBinding

(1)service-account-test.yaml

[root@k8s-master k8s]# cat service-account-test.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner-test

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner-test
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"]

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

(2)创建

[root@k8s-master k8s]# kubectl create -f service-account-test.yaml
serviceaccount/nfs-client-provisioner-test created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner-test created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner-test created

5、创建 storageclass

(1)storage-test.yaml

[root@k8s-master k8s]# cat storage-test.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: course-nfs-storage-test
provisioner: iscas

(2)创建

nginx-deployment-03           3/3     3            3           2d23h
[root@k8s-master k8s]# kubectl create -f storage-test.yaml
storageclass.storage.k8s.io/course-nfs-storage-test created

(3)查看 storageclass

[root@k8s-master k8s]# kubectl get storageclass
NAME                      PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
course-nfs-storage-test   iscas            Delete          Immediate           false                  31s

二、创建PVC

1、pvc-test.yaml

[root@k8s-master k8s]# cat pvc-test.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-test
  annotations:
    volume.beta.kubernetes.io/storage-class: "course-nfs-storage-test"
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

2、创建

[root@k8s-master k8s]# kubectl create -f pvc-test.yaml
persistentvolumeclaim/pvc-test created

3、查看

[root@k8s-master k8s]# kubectl get pvc
NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS              AGE
pvc-test-01   Bound    pvc-4cdc0549-c6ce-4f77-8816-a49df5627d98   1Gi        RWO            course-nfs-storage-test   5s
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐