Kubernetes集群使用Volumes实现宿主机与容器内部文件、目录共享
2022/4/21 6:23:26
本文主要是介绍Kubernetes集群使用Volumes实现宿主机与容器内部文件、目录共享,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文通过编写关于运行nginx pod的yaml文件介绍两种文件共享的方式
[root@master1 ~]# vim nginx.yaml --- apiVersion: v1 kind: Pod metadata: name: nginx namespace: test labels: app: nginx spec: containers: - name: nginx image: nginx:1.20 imagePullPolicy: IfNotPresent ports: - containerPort: 80 volumeMounts: - name: nginx-data mountPath: /usr/share/nginx/html - name: nginx-conf mountPath: /etc/nginx/conf.d/ volumes: - name: nginx-data hostPath: path: /www type: DirectoryOrCreate - name: nginx-conf nfs: server: 192.168.110.5 path: "/conf"
- 使用hostpath的方式实现
volumeMounts: - name: nginx-data #名称与下面对应 mountPath: /usr/share/nginx/html #容器内的路径 volumes: - name: nginx-data hostPath: path: /www #宿主机的路径 type: DirectoryOrCreate #当目录不存在时自动创建
注:宿主机的路径时该pod被调度在对应的服务器路径
2. 使用nfs远程挂载
首先在服务端与客户端都安装nfs
[root@master1 ~]# yum install -y nfs-utils
更改服务端的配置,客户端无须更改
[root@master1 ~]# cat /etc/exports /conf 192.168.110.0/24(rw,sync,no_root_squash)
客户端与服务端同时启动nfs
[root@master1 ~]# systemctl start nfs
volumeMounts: - name: nginx-conf #名称与下面相对应 mountPath: /etc/nginx/conf.d/ 容器内的文件路径 volumes: - name: nginx-conf nfs: server: 192.168.110.5 #服务端的ip地址 path: "/conf" #服务端宿主机的路径
最后运行yaml文件
[root@master1 ~]# kubectl apply -y nginx.yaml
这篇关于Kubernetes集群使用Volumes实现宿主机与容器内部文件、目录共享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08云原生周刊:Kubernetes v1.30 发布 | 2024.4.22
- 2024-05-08云原生周刊:K8s 中的服务和网络 | 2024.4.29
- 2024-03-29kubernetes crashloopbackoff
- 2024-03-13ubuntu install kubernetes
- 2024-03-07k8s coredns containercreating
- 2024-03-04k8s for dummies
- 2024-03-01pulumi kubernetes operator
- 2024-02-06云原生周刊:K8s 1.26 到 1.29 版本的更新 | 2024.1.29
- 2024-01-24K8s集群CoreDNS监控告警最佳实践
- 2024-01-23OpenKruise :Kubernetes背后的托底