1. https://stackoverflow.com/questions/50761264/deployment-and-pvcs
    2. https://akomljen.com/kubernetes-persistent-volumes-with-deployment-and-statefulset/
    3. 我们貌似会被问到Deployment是否可以做数据持久化,是不是也可以给它挂载一个盘来存储数据:
    4. ---
    5. apiVersion: v1
    6. kind: PersistentVolumeClaim
    7. metadata:
    8. name: nginx-pvc
    9. spec:
    10. accessModes:
    11. - ReadWriteOnce
    12. resources:
    13. requests:
    14. storage: 256Mi
    15. storageClassName: fask
    16. ---
    17. # Deployment:
    18. apiVersion: apps/v1
    19. kind: Deployment
    20. metadata:
    21. name: nginx-deployment
    22. spec:
    23. replicas: 2
    24. selector:
    25. matchLabels:
    26. app: nginx
    27. template:
    28. metadata:
    29. labels:
    30. app: nginx
    31. spec:
    32. containers:
    33. - name: nginx
    34. image: nginx:1.7.9
    35. ports:
    36. - containerPort: 80
    37. volumeMounts:
    38. - name: data
    39. mountPath: "/var/www/html"
    40. volumes:
    41. - name: data
    42. persistentVolumeClaim:
    43. claimName: nginx-pvc
    44. ---
    45. If I run with a single replica Deployment my PV gets dynamically created by the vsphere StorageClass
    46. However, if I have more than 2 replicas it will fail to create the second PV:
    47. AttachVolume.Attach failed for volume "pvc-8facf319-6b1a-11e8-935b-00505680b1b8" : Failed to add disk 'scsi0:1'.
    48. Unable to mount volumes for pod "nginx-deployment-7886f48dcd-lzms8_default(b0e38764-6b1a-11e8-935b-00505680b1b8)": timeout expired waiting for volumes to attach or mount for pod "default"/"nginx-deployment-7886f48dcd-lzms8". list of unmounted volumes=[data]. list of unattached volumes=[data default-token-5q7kr]
    49. You should then probably use StatefulSet and volumeClaimTemplates within it instead of Deployment and PersistentVolumeClaim.
    50. In your case each deployment is having same PersistentVolumeClaim (that is ReadWriteOnly and can't be mounted on second request), while with volumeClaimTemplates you get different one provisioned per each replica.