当前位置:  首页>> 技术小册>> Kubernets合辑7-存储

准备NFS存储

  1. [root@hdss7-200 volume]# showmount -e
  2. Export list for hdss7-200.host.com:
  3. /tmp/data/websit/03 10.4.7.0/24
  4. /tmp/data/websit/02 10.4.7.0/24
  5. /tmp/data/websit/01 10.4.7.0/24

定义PV

  1. [root@hdss7-200 volume]# cat /data/k8s-yaml/base_resource/volume/pv.yaml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: nfs-pv001
  6. labels:
  7. type: SSD
  8. fs: nfs
  9. speed: fast
  10. spec:
  11. accessModes:
  12. - ReadWriteMany
  13. - ReadWriteOnce
  14. - ReadOnlyMany
  15. capacity:
  16. storage: 5Gi
  17. nfs:
  18. server: hdss7-200
  19. path: /tmp/data/websit/01
  20. ---
  21. apiVersion: v1
  22. kind: PersistentVolume
  23. metadata:
  24. name: nfs-pv002
  25. labels:
  26. type: SSD
  27. fs: nfs
  28. speed: slow
  29. spec:
  30. accessModes:
  31. - ReadWriteMany
  32. - ReadOnlyMany
  33. capacity:
  34. storage: 5Gi
  35. nfs:
  36. server: hdss7-200
  37. path: /tmp/data/websit/02
  38. ---
  39. apiVersion: v1
  40. kind: PersistentVolume
  41. metadata:
  42. name: nfs-pv003
  43. labels:
  44. type: SSD
  45. fs: nfs
  46. speed: slow
  47. spec:
  48. accessModes:
  49. - ReadWriteMany
  50. - ReadWriteOnce
  51. capacity:
  52. storage: 10Gi
  53. nfs:
  54. server: hdss7-200
  55. path: /tmp/data/websit/03

定义PVC和deployment

  1. [root@hdss7-200 volume]# cat /data/k8s-yaml/base_resource/volume/pvc-depolyment.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: website-root-pvc
  6. namespace: app
  7. labels:
  8. tier: website
  9. type: nfs
  10. spec:
  11. accessModes:
  12. - ReadWriteMany
  13. resources:
  14. requests:
  15. storage: 5Gi
  16. selector:
  17. matchLabels:
  18. fs: nfs
  19. speed: fast
  20. ---
  21. apiVersion: apps/v1
  22. kind: Deployment
  23. metadata:
  24. name: pvc-deploy
  25. namespace: app
  26. labels:
  27. tier: volume
  28. role: nfs
  29. spec:
  30. replicas: 2
  31. selector:
  32. matchLabels:
  33. tier: volume
  34. role: nfs
  35. template:
  36. metadata:
  37. labels:
  38. tier: volume
  39. role: nfs
  40. spec:
  41. containers:
  42. - name: main-container
  43. image: harbor.od.com/public/busybox:v1.31.1
  44. volumeMounts:
  45. - name: web-root
  46. mountPath: /data/web/html
  47. command:
  48. - httpd
  49. args:
  50. - -f
  51. - -h
  52. - "/data/web/html"
  53. volumes:
  54. - name: web-root
  55. persistentVolumeClaim:
  56. claimName: website-root-pvc

创建

  1. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/base_resource/volume/pv.yaml
  2. [root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/base_resource/volume/pvc-depolyment.yaml
  3. [root@hdss7-21 ~]# kubectl get pv --show-labels
  4. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE LABELS
  5. nfs-pv001 5Gi RWO,ROX,RWX Retain Bound app/website-root-pvc 25m fs=nfs,speed=fast,type=SSD
  6. nfs-pv002 5Gi ROX,RWX Retain Available 25m fs=nfs,speed=slow,type=SSD
  7. nfs-pv003 10Gi RWO,RWX Retain Available 25m fs=nfs,speed=slow,type=SSD
  8. [root@hdss7-21 ~]# kubectl get pvc -n app
  9. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
  10. website-root-pvc Bound nfs-pv001 5Gi RWO,ROX,RWX 13m
  11. [root@hdss7-21 ~]# kubectl get pod -l tier=volume -n app -o wide
  12. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  13. pvc-deploy-86847c7b6b-mqvm5 1/1 Running 0 14m 172.7.21.11 hdss7-21.host.com <none> <none>
  14. pvc-deploy-86847c7b6b-x4p6m 1/1 Running 0 8m56s 172.7.22.8 hdss7-22.host.com <none> <none>

测试

  1. [root@hdss7-21 ~]# kubectl exec pvc-deploy-86847c7b6b-mqvm5 -n app -- /bin/sh -c "echo 'hello world' > /data/web/html/index.html"
  2. [root@hdss7-21 ~]# curl -s 172.7.21.11;curl -s 172.7.22.8 # 主页文件已生成
  3. hello world
  4. hello world
  5. [root@hdss7-21 ~]# kubectl delete pod pvc-deploy-86847c7b6b-mqvm5 pvc-deploy-86847c7b6b-x4p6m -n app
  6. pod "pvc-deploy-86847c7b6b-mqvm5" deleted
  7. pod "pvc-deploy-86847c7b6b-x4p6m" deleted
  8. [root@hdss7-21 ~]# kubectl get pod -l tier=volume -n app -o wide
  9. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  10. pvc-deploy-86847c7b6b-7gqqs 1/1 Running 0 62s 172.7.21.12 hdss7-21.host.com <none> <none>
  11. pvc-deploy-86847c7b6b-drm28 1/1 Running 0 62s 172.7.22.11 hdss7-22.host.com <none> <none>
  12. [root@hdss7-21 ~]# curl -s 172.7.21.12; curl -s 172.7.22.11 # Pod删除重建后,文件依旧存在
  13. hello world
  14. hello world

该分类下的相关小册推荐: