简单使用
1、创建local-storage
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
查看storageclass的状态
kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
local-storage kubernetes.io/no-provisioner Delete WaitForFirstConsumer false 2m1s
2、创建pv,这里的pv是local pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv-sdc
spec:
capacity:
storage: 4Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /data/pv
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: node
operator: In
values:
- middleware
查看pv状态
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
local-pv-sdc 2Gi RWO Retain Available local-storage 7s
3、创建一个statefuleset应用测试
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
role: stateful
spec:
containers:
- name: nginx
image: cnych/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: local-storage
resources:
requests:
storage: 1Gi
查看pvc,pv,storageclass状态
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound local-pv-sdc 2Gi RWO local-storage 86s
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
local-pv-sdc 2Gi RWO Retain Bound default/www-web-0 local-storage 6m21s
4、扩容测试
# kubectl edit pvc www-web-0
...
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
storageClassName: local-storage
volumeMode: Filesystem
volumeName: local-pv-sdc
...
将storage修改为2Gi,保存退出,报错如下。
error: persistentvolumeclaims "www-web-0" could not be patched: persistentvolumeclaims "www-web-0" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize
这是说pvc必须支持修改大小,我们先修改storageclass,让其支持扩展,如下加allowVolumeExpansion: true
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
然后我们再修改pvc,提示改成功如下:
# kubectl edit pvc www-web-0
persistentvolumeclaim/www-web-0 edited
我们再修改pv的大小,修改为3G,修改后的状态如下:
# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
local-pv-sdc 3Gi RWO Retain Bound default/www-web-0 local-storage 28m
然后我们再扩容pvc,查看pvc的event如下:
Ignoring the PVC: didn't find a plugin capable of expanding the volume; waiting for an external controller to process this PVC.
报错信息为:没有找到可扩展的插件。
我上官方网站一看,原来人家已经说的很清楚了:Although the feature is enabled by default, a cluster admin must opt-in to allow users to resize their volumes. Kubernetes v1.11 ships with volume expansion support for the following in-tree volume plugins: AWS-EBS, GCE-PD, Azure Disk, Azure File, Glusterfs, Cinder, Portworx, and Ceph RBD.