概念
PersistentVolume
PersistentVolume
(持久卷,简称PV)是集群内,由管理员提供的网络存储的一部分。就像集群中的节点一样,PV也是集群中的一种资源。它也像Volume一样,是一种volume插件,但是它的生命周期却是和使用它的Pod相互独立的。PV这个API对象,捕获了诸如NFS、ISCSI、或其他云存储系统的实现细节。
https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes
# kubectl explain pv.spec
# KIND: PersistentVolume
# VERSION: v1
# spec
# accessModes <[]string>
# capacity <map[string]string>
# mountOptions <[]string> 部分支持,比如NFS
# persistentVolumeReclaimPolicy <string>
# storageClassName <string>
# volumeMode <string>
apiVersion: v1
kind: PersistentVolume
metadata:
name: block-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: aliclood-nas
volumeMode: Block
PersistentVolumeClaim
PersistentVolumeClaim
(持久卷声明,简称PVC)是用户的一种存储请求。它和Pod类似,Pod消耗Node资源,而PVC消耗PV资源。Pod能够请求特定的资源(如CPU和内存)。PVC能够请求指定的大小和访问的模式(可以被映射为一次读写或者多次只读)。
PVC允许用户消耗抽象的存储资源,用户也经常需要各种属性(如性能)的PV。集群管理员需要提供各种各样、不同大小、不同访问模式的PV,而不用向用户暴露这些volume如何实现的细节。因为这种需求,就催生出一种StorageClass
资源。
# accessModes <[]string>
# storageClassName <string>
# volumeMode <string>
# volumeName <string>
# resources <Object>
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: myclaim
spec:
accessModes:
- ReadWriteOnce
storageClassName: slow
volumeMode: Filesystem
resources:
requests:
storage: 8Gi
StorageClass
StorageClass
提供了一种方式,使得管理员能够描述他提供的存储的等级。集群管理员可以将不同的等级映射到不同的服务等级、不同的后端策略。
https://kubernetes.io/docs/concepts/storage/storage-classes/
# mountOptions <[]string>
# parameters <map[string]string>
# provisioner <string> -required-
# reclaimPolicy <string>
# volumeBindingMode <string>
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alicloud-nas
mountOptions:
- vers=4.0
provisioner: alicloud/nas
reclaimPolicy: Retain
实例
Local Storage
首先要创建StorageClass。
cat <<EOF | kubectl apply -f -
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
EOF
kubectl get sc
NAME PROVISIONER AGE
local-storage kubernetes.io/no-provisioner 3s
创建PV
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-local-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /mnt/
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- master1
EOF
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
example-local-pv 1Gi RWO Retain Available local-storage 3s
如果hostname没有指定,则会报错。The PersistentVolume "example-local-pv" is invalid: metadata.annotations: Required value: Local volume requires node affinity
创建PVC
cat <<EOF | kubectl apply -f -
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: example-local-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-storage
EOF
使用PVC
cat <<EOF | kubectl apply -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
template:
metadata:
labels:
run: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
volumeMounts:
- mountPath: "/var/www/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: example-local-claim
EOF
NAS
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: alicloud-nas
mountOptions:
- vers=4.0
provisioner: alicloud/nas
reclaimPolicy: Retain
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: alicloud-nas-controller
namespace: kube-system
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: alicloud-nas-controller
spec:
tolerations:
- effect: NoSchedule
operator: Exists
key: node-role.kubernetes.io/master
- effect: NoSchedule
operator: Exists
key: node.cloudprovider.kubernetes.io/uninitialized
nodeSelector:
node-role.kubernetes.io/master: ""
serviceAccount: admin
containers:
- name: alicloud-nas-controller
image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-nas-controller:v3.1.0-k8s1.11
volumeMounts:
- mountPath: /persistentvolumes
name: nfs-client-root
env:
- name: PROVISIONER_NAME
value: alicloud/nas
- name: NFS_SERVER
value: xxx.cn-hangzhou.nas.aliyuncs.com
- name: NFS_PATH
value: /
volumes:
- name: nfs-client-root
flexVolume:
driver: alicloud/nas
options:
path: /
server: 0cd8b4a576-mmi32.cn-hangzhou.nas.aliyuncs.com
vers: "4.0"
使用VolumeTemplate
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
volumeMounts:
- mountPath: "/usr/share/nginx/html/"
name: html
volumeClaimTemplates:
- metadata:
name: html
spec:
accessModes:
- ReadWriteOnce
storageClassName: alicloud-nas
resources:
requests:
storage: 2Gi
# web-html-0 Bound pvc-487 2Gi RWO alicloud-nas 1h
# web-html-1 Bound pvc-60a 2Gi RWO alicloud-nas 1h
Ref
Kubernetes存储之Persistent Volumes简介
https://blog.csdn.net/styshoo/article/details/72235208
Local Persistent Volumes for Kubernetes Goes Beta
https://kubernetes.io/blog/2018/04/13/local-persistent-volumes-beta/
使用阿里云 NAS