当前位置:   article > 正文

K8S部署nginx并且使用NFS存储数据_容器部署的nginx如何配置访问服务共享盘文件

容器部署的nginx如何配置访问服务共享盘文件

安装NFS

在master安装NFS

systemctl start nfs-server

修改配置 /etc/exports

/data *(rw,no_root_squash,no_all_squash,sync)

目录为 /data 允许所有地址访问

验证下

  1. [root@master nginx]# showmount -e 192.168.57.61
  2. Export list for 192.168.57.61:
  3. /data *

共享可以正常访问

K8S部署nginx

新建 nginx.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 3
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx:1.14.2
  20. ports:
  21. - containerPort: 80
  22. volumeMounts:
  23. - name: data
  24. mountPath: /usr/share/nginx/html
  25. volumes:
  26. - name: data
  27. nfs:
  28. path: /data
  29. server: 192.168.57.61
  30. ---
  31. apiVersion: v1
  32. kind: Service
  33. metadata:
  34. name: nginx-service
  35. labels:
  36. app: nginx
  37. spec:
  38. ports:
  39. - port: 9000
  40. name: nginx
  41. protocol: TCP
  42. targetPort: 80
  43. nodePort: 31090
  44. selector:
  45. app: nginx
  46. type: NodePort
  47. ---
  48. apiVersion: v1
  49. kind: PersistentVolume
  50. metadata:
  51. name: pv-nfs
  52. spec:
  53. capacity:
  54. storage: 10Gi
  55. accessModes:
  56. - ReadWriteMany
  57. nfs:
  58. path: /data
  59. server: 192.168.57.61
  60. ---
  61. kind: PersistentVolumeClaim
  62. apiVersion: v1
  63. metadata:
  64. name: pvc-nfs
  65. spec:
  66. accessModes:
  67. - ReadWriteMany
  68. resources:
  69. requests:
  70. storage: 10Gi

在deployment 配置中

  1. volumeMounts:
  2. - name: data
  3. mountPath: /usr/share/nginx/html
  4. volumes:
  5. - name: data
  6. nfs:
  7. path: /data
  8. server: 192.168.57.61

这部分代表 容器内的 /usr/share/nginx/html 目录

挂到NFS 的 /data目录

NFS服务器的地址为 192.168.57.61

在Service中

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: nginx-service
  5. labels:
  6. app: nginx
  7. spec:
  8. ports:
  9. - port: 9000
  10. name: nginx
  11. protocol: TCP
  12. targetPort: 80
  13. nodePort: 31090
  14. selector:
  15. app: nginx
  16. type: NodePort

ports:port:9000 代表pod之间通信的端口为9000

protocol:TCP 代表只允许TCP连接

targetPort:80 代表pod内的80端口

nodePort:31090 代表映射到集群外的31090端口,刚才提到的80端口

运行

kubectl apply -f  nginx.yaml 

查看pod状态(由于我没有设置namespace 就是默认的命名空间 不用指定 -n )

  1. [root@master nginx]# kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. nginx-deployment-5d7bfd6588-6g5mn 1/1 Running 0 48m
  4. nginx-deployment-5d7bfd6588-7sz4p 1/1 Running 0 48m
  5. nginx-deployment-5d7bfd6588-vkbvq 1/1 Running 0 48m

测试

挂载NFS

 mount -t nfs -o rw 192.168.57.61:/data /data1
echo "11111" >> /data1/index.html 

访问:http://192.168.57.76:31090/

 

可以看到1111 已经显示在浏览器中了

index.html已经在容器内了 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/986761
推荐阅读
相关标签
  

闽ICP备14008679号