0


k8s 目录和文件挂载到宿主机

k8s生产中常用的volumes挂载方式有:hostPath、pv,pvc、nfs

1.hostPath挂载
hostPath是将主机节点文件系统上的文件或目录挂载到Pod 中,同时pod中的目录或者文件也会实时存在宿主机上,如果pod删除,hostpath中的文件不会被删除。(生成的pod只能在同一个节点上,调度到其他节点就不会挂载)

配置文件:

  1. [root@master1 k8s-nginx]# cat nginx-test.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: nginx-service
  6. name: nginx-service
  7. namespace: default
  8. spec:
  9. ports:
  10. #对外暴露端口30003
  11. - nodePort: 30003
  12. port: 8010
  13. protocol: TCP
  14. targetPort: 8010
  15. selector:
  16. app: nginx-web
  17. #NodePort对外暴露端口
  18. type: NodePort
  19. ---
  20. apiVersion: apps/v1
  21. kind: Deployment
  22. metadata:
  23. labels:
  24. app: nginx-web
  25. name: nginx-web
  26. namespace: default
  27. spec:
  28. replicas: 1
  29. selector:
  30. matchLabels:
  31. app: nginx-web
  32. template:
  33. metadata:
  34. labels:
  35. app: nginx-web
  36. namespace: default
  37. spec:
  38. imagePullSecrets:
  39. - name: secret-key
  40. containers:
  41. - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2
  42. name: nginx
  43. env:
  44. - name: TZ
  45. value: Asia/Shanghai
  46. imagePullPolicy: Always
  47. ports:
  48. - containerPort: 8010
  49. resources:
  50. requests:
  51. cpu: 100m
  52. memory: 512Mi
  53. limits:
  54. cpu: 1000m
  55. memory: 1Gi
  56. volumeMounts:
  57. - name: nginx-volume-dir
  58. mountPath: /var/log/nginx
  59. - name: nginx-volume-file
  60. mountPath: /var/log/nginx/access2.log
  61. volumes:
  62. - name: nginx-volume-dir
  63. hostPath:
  64. path: /root/k8s-nginx/nginx/log
  65. type: DirectoryOrCreate #如果目录不存在就创建
  66. - name: nginx-volume-file
  67. hostPath:
  68. path: /root/k8s-nginx/nginx/log/access2.log
  69. type: FileOrCreate ## 如果文件不存在则创建

这个是master1节点创建的,pod是在node1节点上运行的,所以日志是存储在node1节点上
需要登录到node1节点上查看挂载的情况:

在node1节点上查看是否目录和日志文件:

2.nfs挂载

  1. nfs挂载是hostPath挂载的升级版,优点是在不同的node节点上的日志,文件都可以挂载到nfs的机器上,只需要配置上nfs挂载的机器ip和挂载的路径就行。
  1. 安装nfs,建立共享服务器(单独服务器安装nfs挂载,ip10.10.10.25
  2. [root@localhost ~]# yum -y install nfs-utils
  3. ...
  4. 创建存储目录:
  5. [root@localhost ~]# mkdir -p /data/nfs/{conf,dist,log} #可以创建多个存储目录
  6. [root@localhost ~]# vim /etc/exports
  7. /data/nfs 10.10.10.24(rw,no_root_squash) #可以添加多个存储目录
  8. #将共享目录以读写权限给node1机器,因为pod是跑在node1节点上:10.10.10.24
  9. 启动nfs应用:
  10. [root@localhost ~]# systemctl start nfs
  11. 查看进程:
  12. [root@localhost ~]# ps -ef |grep nfs
  13. root 104715 2 0 15:56 ? 00:00:00 [nfsd4_callbacks]
  14. root 104721 2 0 15:56 ? 00:00:00 [nfsd]
  15. root 104722 2 0 15:56 ? 00:00:00 [nfsd]
  16. root 104723 2 0 15:56 ? 00:00:00 [nfsd]
  17. root 104724 2 0 15:56 ? 00:00:00 [nfsd]
  18. root 104725 2 0 15:56 ? 00:00:00 [nfsd]
  19. root 104726 2 0 15:56 ? 00:00:00 [nfsd]
  20. root 104727 2 0 15:56 ? 00:00:00 [nfsd]
  21. root 104728 2 0 15:56 ? 00:00:00 [nfsd]
  22. root 104750 103971 0 15:56 pts/0 00:00:00 grep --color=auto nfs
  23. 修改/etc/exports后,使文件生效:
  24. [root@localhost ~]# exportfs -r
  25. /data/nfs 10.10.10.24
  26. 查看挂载目录:
  27. [root@localhost nfs]# exportfs
  28. /data/nfs/conf 10.10.10.24
  29. /data/nfs/log 10.10.10.24
  30. /data/nfs/dist 10.10.10.24
  31. [root@localhost nfs]# exportfs -v
  32. /data/nfs/conf 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
  33. /data/nfs/log 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
  34. /data/nfs/dist 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

** 编写pod的yaml文件:**

  1. vim nginx-nfs.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: nginx-service
  6. name: nginx-service
  7. namespace: default
  8. spec:
  9. ports:
  10. #对外暴露端口30003
  11. - nodePort: 30003
  12. port: 8010
  13. protocol: TCP
  14. targetPort: 8010
  15. selector:
  16. app: nginx-web
  17. #NodePort对外暴露端口
  18. type: NodePort
  19. ---
  20. apiVersion: apps/v1
  21. kind: Deployment
  22. metadata:
  23. labels:
  24. app: nginx-web
  25. name: nginx-web
  26. namespace: default
  27. spec:
  28. replicas: 1
  29. selector:
  30. matchLabels:
  31. app: nginx-web
  32. template:
  33. metadata:
  34. labels:
  35. app: nginx-web
  36. namespace: default
  37. spec:
  38. imagePullSecrets:
  39. - name: secret-key
  40. containers:
  41. - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2
  42. name: nginx
  43. env:
  44. - name: TZ
  45. value: Asia/Shanghai
  46. imagePullPolicy: Always
  47. ports:
  48. - containerPort: 8010
  49. resources:
  50. requests:
  51. cpu: 100m
  52. memory: 512Mi
  53. limits:
  54. cpu: 1000m
  55. memory: 1Gi
  56. volumeMounts:
  57. - name: nginx-volume-dir
  58. mountPath: /var/log/nginx
  59. #- name: nginx-volume-file
  60. # mountPath: /var/log/nginx/access2.log
  61. #- name: nginx-config
  62. # mountPath: /etc/nginx/conf.d
  63. volumes:
  64. - name: nginx-volume-dir
  65. nfs:
  66. server: 10.10.10.25
  67. path: /data/nfs
  68. #- name: nginx-volume-file
  69. # server: 10.10.10.25
  70. # path: /data/nfs
  71. #- name: nginx-config
  72. # nfs:
  73. # server: 10.10.10.25
  74. # path: /data/nfs

验证:到安装nfs机器上的/data/nfs/log目录查看是否有文件

问题:单独nfs挂载好像只能挂载一个目录,挂载多个目录不生效并且导致部分文件消失?

/etc/exports 配置:

yaml配置:

  1. volumeMounts:
  2. - name: nginx-dir
  3. mountPath: /etc/nginx/dist
  4. - name: nginx-log
  5. mountPath: /var/log/nginx
  6. - name: nginx-config
  7. mountPath: /etc/nginx/conf.d
  8. volumes:
  9. - name: nginx-dir
  10. nfs:
  11. server: 10.10.10.25
  12. path: /data/nfs/dist
  13. - name: nginx-log
  14. nfs:
  15. server: 10.10.10.25
  16. path: /data/nfs/log
  17. - name: nginx-config
  18. nfs:
  19. server: 10.10.10.25
  20. path: /data/nfs/conf

待续...

3.pv、pvc挂载

  1. pvpvc挂载是基于nfs挂载的高级方式(如果不搭配nfs使用,侧配置的pvpvc默认是pod所在node节点上),通过PVPVCKubernetes可以实现存储资源的动态供给、自动扩展和缩减,以及共享和负载均衡等高级特性。PVPVC的出现使得应用容器可以随时地挂载或卸载存储资源,而无需手动管理存储卷的创建、挂载和卸载等操作。

1.创建pv(相当于存储设备)

  1. vim pv.yaml
  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4. name: pv
  5. labels:
  6. pv: pv-nfs
  7. spec:
  8. capacity:
  9. storage: 10Gi
  10. accessModes:
  11. - ReadWriteMany
  12. volumeMode: Filesystem
  13. persistentVolumeReclaimPolicy: Retain
  14. storageClassName: nfs
  15. nfs:
  16. server: 10.10.10.25
  17. path: /data/nfs
  1. kubectl apply -f pv.yaml

2.创建pvc(相当于调度存储设备资源的)

  1. vim nginx-pvc.yaml
  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteMany
  8. volumeMode: Filesystem
  9. resources:
  10. requests:
  11. storage: 2Gi
  12. storageClassName: nfs
  13. selector:
  14. matchLabels:
  15. pv: pv-nfs
  1. kubectl apply -f pvc.yaml

3.创建pod(去请求pvc的)

  1. vim nginx-pod.yaml
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. app: nginx-service
  6. name: nginx-service
  7. namespace: default
  8. spec:
  9. ports:
  10. #对外暴露端口30003
  11. - nodePort: 30003
  12. port: 8010
  13. protocol: TCP
  14. targetPort: 8010
  15. selector:
  16. app: nginx-web
  17. #NodePort对外暴露端口
  18. type: NodePort
  19. ---
  20. apiVersion: apps/v1
  21. kind: Deployment
  22. metadata:
  23. labels:
  24. app: nginx-web
  25. name: nginx-web
  26. namespace: default
  27. spec:
  28. replicas: 1
  29. selector:
  30. matchLabels:
  31. app: nginx-web
  32. template:
  33. metadata:
  34. labels:
  35. app: nginx-web
  36. namespace: default
  37. spec:
  38. imagePullSecrets:
  39. - name: secret-key
  40. containers:
  41. - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2
  42. name: nginx
  43. env:
  44. - name: TZ
  45. value: Asia/Shanghai
  46. imagePullPolicy: Always
  47. ports:
  48. - containerPort: 8010
  49. resources:
  50. requests:
  51. cpu: 100m
  52. memory: 512Mi
  53. limits:
  54. cpu: 1000m
  55. memory: 1Gi
  56. volumeMounts:
  57. - name: nginx-log
  58. mountPath: /var/log/nginx
  59. #- name: nginx-conf
  60. # mountPath: /etc/nginx/conf.d
  61. #- name: nginx-dist
  62. # mountPath: /etc/nginx/dist
  63. volumes:
  64. - name: nginx-log
  65. persistentVolumeClaim:
  66. claimName: pvc
  67. #- name: nginx-conf
  68. # persistentVolumeClaim:
  69. # claimName: pvc
  70. #- name: nginx-dist
  71. # persistentVolumeClaim:
  72. # claimName: pvc
  1. kubectl apply -f nginx-pod.yaml

查看运行状态:

  1. kubectl describe pod nginx-web-6665c66698-fxhzl

验证:

登录到nfs的服务器,进到挂载路径下看是否有文件


本文转载自: https://blog.csdn.net/weixin_46627652/article/details/134272325
版权归原作者 小邋遢2.0 所有, 如有侵权,请联系我们删除。

“k8s 目录和文件挂载到宿主机”的评论:

还没有评论