通过 PVC 请求到一定的存储空间也很有可能不足以满足应用对于存储设备的各种需求,而且不同的应用程序对于存储性能的要求可能也不尽相同,比如读写速度、并发性能等,为了解决这一问题,Kubernetes 又为我们引入了一个新的资源对象:StorageClass,通过 StorageClass 的定义,管理员可以将存储资源定义为某种类型的资源,比如快速存储、慢速存储等,用户根据 StorageClass 的描述就可以非常直观的知道各种存储资源的具体特性了,这样就可以根据应用的特性去申请合适的存储资源了。
1、下载镜像
docker pull jmgao1983/nfs-client-provisioner
docker tag jmgao1983/nfs-client-provisioner 192.168.100.87:80/nfs-client-provisioner
docker push 192.168.100.87:80/nfs-client-provisioner
2、创建provisioner
创建nfs-client-provisioner
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: 192.168.100.87:80/nfs-client-provisioner
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.100.87
- name: NFS_PATH
value: /data/k8s
volumes:
- name: nfs-client-root
nfs:
server: 192.168.100.87
path: /data/k8s
kubectl apply -f nfs-client.yaml
创建account并绑定角色
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
---
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: ["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
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
kubectl apply -f nfs-client-sa.yaml
创建storageclass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: course-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
kubectl apply -f nfs-client-class.yaml
查看
[root@k8s-node1 k8s]# kubectl get storageclass
NAME PROVISIONER AGE
course-nfs-storage fuseim.pri/ifs 47s
my-storage-class kubernetes.io/aws-ebs 5d5h
3、动态PV
创建一个PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pvc2
annotations:
volume.beta.kubernetes.io/storage-class: "course-nfs-storage"
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
kubectl apply -f test-pvc2.yaml
[root@k8s-node1 k8s]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-1 Bound pv1 1Gi RWO 5h5m
test-pvc Pending course-nfs-storage 2m49s
test-pvc2 Pending course-nfs-storage2 7s
www-web-0 Bound pv002 1Gi RWO 5d5h
www-web-1 Bound pv001 1Gi RWO 5d5h
4、测试使用
测试stateful应用使用
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nfs-web
spec:
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nfs-web
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: 172.16.10.190:8008/nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
imagePullSecrets:
- name: myregistrykey6
volumeClaimTemplates:
- metadata:
name: www
annotations:
volume.beta.kubernetes.io/storage-class: course-nfs-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
kubectl apply -f test-stateful-nfs-storageclass.yaml
查看
[root@k8s-node1 k8s]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-1 Bound pv1 1Gi RWO 5h11m
test-pvc2 Bound pvc-b859c7bb-0590-4ce4-9bd4-51ce9b238a50 1Gi RWO course-nfs-storage 3m7s
www-nfs-web-0 Bound pvc-89484183-dda6-4970-b6dc-f9289e629d75 1Gi RWO course-nfs-storage 20s
www-nfs-web-1 Bound pvc-9450bd0b-38ae-487e-b328-3d98c2661b07 1Gi RWO course-nfs-storage 17s
www-web-0 Bound pv002 1Gi RWO 5d5h
www-web-1 Bound pv001 1Gi RWO 5d5h
[root@k8s-node1 k8s]# cd /data/k8s
[root@k8s-node1 k8s]# ls
archived-default-test-pvc-pvc-2081210b-84d2-4f5f-bfc4-52f08a05c18a index.html pv-demo.yaml
archived-default-test-pvc-pvc-8b161329-ad17-48b1-bfe6-e4f86381b289 nfs-client-class.yaml test2.bbb
default-test-pvc2-pvc-b859c7bb-0590-4ce4-9bd4-51ce9b238a50 nfs-client-sa.yaml test.aaa
default-www-nfs-web-0-pvc-89484183-dda6-4970-b6dc-f9289e629d75 nfs-client.yaml test-pvc.yaml
default-www-nfs-web-1-pvc-9450bd0b-38ae-487e-b328-3d98c2661b07 pvc-demo.yaml