kubernetes存储创建以及操作
准备工作
kubectl create ns study
创建StorageClass
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: study-class
parameters:
archiveOnDelete: 'false'
provisioner: study-class
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer
创建 pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/bound-by-controller: 'yes'
finalizers:
- kubernetes.io/pv-protection
name: study-pv
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 10Gi
nfs:
path: /data/share/study
server: 192.168.200.200
persistentVolumeReclaimPolicy: Retain
storageClassName: study-class
volumeMode: Filesystem
创建 pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
annotations:
pv.kubernetes.io/bind-completed: 'yes'
finalizers:
- kubernetes.io/pvc-protection
name: study-pvc
namespace: study
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
volumeMode: Filesystem
volumeName: study-pv
操作
kubectl get pv
kubectl delete pv/study-pv
kubectl get pv | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done
kubectl get pv | grep Available | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done
kubectl get pv | grep Released | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done
kubectl get pvc --all-namespaces
kubectl get pvc -n study
kubectl describe pvc study-pvc -n study | grep Finalizers
kubectl delete pvc/study-pvc -n study
使用
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
name: nginx
name: nginx
namespace: study
spec:
replicas: 1
selector:
matchLabels:
name: nginx
app: nginx
template:
metadata:
labels:
name: nginx
app: nginx
spec:
affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nginx
topologyKey: "kubernetes.io/hostname"
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
imagePullPolicy: IfNotPresent
env:
- name: TZ
value: Asia/Shanghai
volumeMounts:
- name: study-pvc-cloud
mountPath: /var/log/nginx
subPath: logs/
volumes:
- name: study-pvc-cloud
persistentVolumeClaim:
claimName: study-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: study
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
nodePort: 30001
其它
nfs-server安装和端挂盘