Local PV是從kuberntes 1.10開始引入,本質目的是爲了解決hostPath的缺陷。經過PV控制器與Scheduler的結合,會對local PV作針對性的邏輯處理,從而,讓Pod在屢次調度時,可以調度到同一個Node上。node
此次,測試了一下將local PV掛載到一個httpd的應用上。docker
要注意,pvc是按namespace提供的。還有,matchExpressions做匹配的主機名規則。apache
一,local-pv.yamlapi
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv-sda
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local: path: /data/pv
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- spark-docker
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: local-pvc-sda
namespace: in-demo
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 10Gi
二,httpd.yamlapp
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd
namespace: in-demo
spec:
replicas: 2
revisionHistoryLimit: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: httpd-dm
spec:
terminationGracePeriodSeconds: 60
restartPolicy: Always
containers:
- name: httpd
image: httpd:alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: storage-localpv
mountPath: "/usr/local/apache2/htdocs"
volumes:
- name: storage-localpv
persistentVolumeClaim: claimName: local-pvc-sda
---
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
namespace: in-demo
spec:
ports:
- name: http-port
port: 80
targetPort: 80
selector:
app: httpd-dm