1.使用Deployment绑定存储: 其中persistentVolumeClaim需要填写已经创建好的PVC。
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-eck-dynamic
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: eck-csi
volumeMode: Filesystem
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-eck-ttt
spec:
replicas: 1
selector:
matchLabels:
name: deployment-eck
template:
metadata:
name: deployment-eck
labels:
name: deployment-eck
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- name: deployment-eck
image: ehub.ctcdn.cn/eck/nginx
command:
- "/bin/bash"
- "-c"
- set -euo pipefail; while true; do echo $(hostname) $(date) >> /mnt/eck/outfile; sleep 1; done
volumeMounts:
- name: eck
mountPath: "/mnt/eck"
volumes:
- name: eck
persistentVolumeClaim:
claimName: pvc-eck-dynamic
2.使用StatefulSet绑定存储:
有状态的存储也是有状态的,最好使用volumeClaimTemplates,去创建PVC。没有容器实例会绑定一个特定PVC。
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset-eck
labels:
app: nginx
spec:
serviceName: statefulset-eck
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- name: statefulset-eck
image: ehub.ctcdn.cn/eck/nginx
command:
- "/bin/bash"
- "-c"
- set -euo pipefail; while true; do echo $(date) >> /mnt/eck/outfile; sleep 1; done
volumeMounts:
- name: persistent-storage
mountPath: /mnt/eck
updateStrategy:
type: RollingUpdate
selector:
matchLabels:
app: nginx
volumeClaimTemplates:
- metadata:
name: persistent-storage
annotations:
volume.beta.kubernetes.io/storage-class: eck-csi
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 20Gi