Persistent Volume (PV)
之前提到的Volume是被定义在Pod上的,属于计算资源的一部分,而实际上,网络存
储是相对独立于计算资源而存在的一种实体资源。比如在使用虚拟机的情况下,我们通常
会先定义一个网络存储,然后从中划出一个“网盘”并挂接到虚拟机上。Persistent Vol
ume(PV)和与之相关联的Persistent Volume Claim(PVC)也起到了类似的作用。
PV可以被理解成Kubernetes集群中的某个网络存储对应的一块存储,它与Volume类似,
但有以下区别。
- PV只能是网络存储,不属于任何Node,但可以在每个Node上访问。
- PV并不是被定义在Pod上的,而是独立于Pod之外定义的。
- PV目前支持的类型包括:gcePersistentDisk、AWSElasticBlockStore、AzureFile、AzureDisk、FC(Fibre Channel)、Flocker、NFS、iSCSI、RBD(Rados Block Device)、CephFS、Cinder、GlusterFS、VsphereVolume、Quobyte Volumes、VMware Photon、Portworx Volumes、ScaleIO Volumes和HostPath(仅供单机测试)。
1、定义一个NFS 类型的 pv 的yaml 定义文件,声明 需要10Gi 的贮存空间:
apiVersion: v1
kind: PersistentVolume
metadata:
name: openldap-pv # pv 名称
spce:
capacity: # pv 大小贮存空间
storage: 10Gi
accessModes: # pv 属性
- ReadWriteOnce
nfs:
path: /tmp/puwf
server: 10.10.100.11
- 比较重要的是PV的accessModes属性:
- ReadWriteOnce:读写权限,并且只能被单个Node挂载。
- ReadOnlyMany:只读权限,允许被多个Node挂载。
- ReadWriteMany:读写权限,允许被多个Node挂载。
如果某个Pod想申请某种类型的PV,则首先需要定义一个PersistentVolumeClaim对象 (PVC):
apiVersion: V1
kind: PersistentVolumeClaim
metadata:
name: openldap-pv
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
然后在 pod 中的volume定义中引用上述的 pvc 即可:
volumes:
- name: data # 这里是pod 命名即可
persistentVolumeClaim:
claimName: openldap-pvc
PV的状态。PV是有状态的对象,它的状态有以下几种。
- Available:空闲状态。
- Bound:已经绑定到某个PVC上。
- Released:对应的PVC已经被删除,但资源还没有被集群收回。
- Failed:PV自动回收失败。
这里引用一个OpenLdap Deployment 例子说明:
apiVersion: apps/v1
kind: Deployment
metadata:
labels: openldap
name: openldap-server
namespace: openldap-server
spec:
replicas: 1
selector:
matchLabels:
k8s-app: openldap
template:
metadata:
labels:
k8s-app: openldap
spec:
# affinity:
# nodeAffinity:
# requiredDuringSchedulingIgnoredDuringExecution:
# nodeSelectorTerms:
# - matchExpressions:
# - key: kubernetes.io/hostname
# operator: In
# values:
# - pps-centos-14-jg
containers:
- env:
- name: KEEP_EXISTING_CONFIG
value: "false"
- name: LDAP_BACKEND
value: hdb
- name: LDAP_BASE_DN
- name: LDAP_DOMAIN
value: drifter.net
- name: LDAP_LOG_LEVEL
value: "256"
- name: LDAP_ORGANISATION
value: drifter Inc.
- name: LDAP_READONLY_USER
value: "false"
- name: LDAP_REMOVE_CONFIG_AFTER_SETUP
value: "true"
- name: LDAP_REPLICATION
value: "false"
- name: LDAP_RFC2307BIS_SCHEMA
value: "false"
- name: LDAP_SSL_HELPER_PREFIX
value: ldap
- name: LDAP_TLS
value: "false"
- name: LDAP_ADMIN_PASSWORD # secret
valueFrom:
secretKeyRef:
key: LDAP_ADMIN_PASSWORD
name: office-keys
optional: false
- name: LDAP_CONFIG_PASSWORD # secret
valueFrom:
secretKeyRef:
key: LDAP_CONFIG_PASSWORD
name: office-keys
optional: false
image: registry.drifter.net/osixia-openldap:1.1.9
imagePullPolicy: Always
name: openldap
ports:
- containerPort: 389
name: openldap
- containerPort: 636
name: openldap
imagePullSecrets:
- name: registry-drifter
restartPolicy: Always
volumeMounts:
- mountPath: /etc/ldap/slapd.d
name: openldap
subPath: openldap-config-new # 目录名
- mountPath: /etc/lib/ldap
name: openldap
subPath: openldap-data-new # 目录名
- mountPath: /etc/localtime
name: time
volumes:
- name: openldap
persistentVolumeClaim:
claimName: openldap-pvc # 引用前面创建好的 openldap-pvc
- hostPath:
path: /etc/localtime
type: ""
name: time