微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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"
  1. 使用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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐