本文通过编写关于运行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"
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