|NO.Z.00191|——————————|^^ 标准 ^^|——|KuberNetes&持久存储.V04|——|volume.v04|挂载NFS至容器|
一、挂载NFS至容器
### --- 在k8s-node01节点安装nfs服务端配置
~~~ 安装nfs-server服务
[root@k8s-node01 ~]# yum install -y nfs-utils
[root@k8s-node01 ~]# systemctl start nfs-server
[root@k8s-node01 ~]# ps aux |grep nfs
root 13345 0.0 0.0 0 0 ? S 15:18 0:00 [nfsd]
[root@k8s-node01 ~]# cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2
### --- nfs下创建一个共享目录
[root@k8s-node01 ~]# mkdir -p /data/nfs
### --- 授权可以访问该共享目录的用户
[root@k8s-node01 ~]# vim /etc/exports
/data/nfs/ 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
[root@k8s-node01 ~]# exportfs -r
[root@k8s-node01 ~]# systemctl reload nfs-server
### --- 在k8s-master01下直接挂载测试
~~~ 在每个节点安装nfs的插件,不然识别不了。
[root@k8s-master01 ~]# yum install -y nfs-utils
[root@k8s-master01 ~]# mount -t nfs 192.168.1.14:/data/nfs /mnt/
[root@k8s-master01 ~]# touch /mnt/yanqi_nfs_test
[root@k8s-node01 ~]# ll /data/nfs/
total 0
-rw-r--r-- 1 root root 0 May 11 11:33 yanqi_nfs_test
### --- 卸载
[root@k8s-master01 ~]# umount /mnt/
二、创建挂载nginx-deploy.nfs.yaml
### --- 在k8s-node01上创建共享目录
[root@k8s-node01 ~]# mkdir -p /data/nfs/yanqi_dp
### --- 创建yaml配置文件
[root@k8s-master01 ~]# vim nginx-deploy.nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /opt
name: share-volume
- mountPath: /etc/timezone
name: timezone
- image: nginx:1.15.2
imagePullPolicy: IfNotPresent
name: nginx2
command:
- sh
- -c
- sleep 3600
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /mnt
name: share-volume
- mountPath: /opt
name: nfs-volume
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: share-volume
emptyDir: {}
#medium: Memory
- name: timezone
hostPath:
path: /etc/timezone
type: File
- name: nfs-volume
nfs:
server: 192.168.1.14
path: /data/nfs/yanqi_dp
~~~ # 配置详解
~~~ nfs是支持多级目录的,而且是支持多个副本去读写的
volumeMounts:
name: nginx2
- mountPath: /mnt
name: share-volume
- mountPath: /opt
name: nfs-volume
~~~ 挂载到nginx2上面,也可以同时挂2个也是可以的
~~~ nfs配置参数
- name: nfs-volume
nfs:
server: 192.168.1.14
path: /data/nfs/yanqi_dp
三、replace容器,挂载nfs实例到容器中
### --- 创建容器
[root@k8s-master01 ~]# kubectl create -f nginx-deploy.nfs.yaml
deployment.apps/nginx created
[root@k8s-master01 ~]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 94 7d16h 172.25.244.211 k8s-master01 <none> <none>
nginx-78658dcf78-6nhg8 2/2 Running 0 101s 172.27.14.220 k8s-node02 <none> <none>
nginx-78658dcf78-fdw4k 2/2 Running 0 101s 172.25.92.82 k8s-master02 <none> <none>
四、验证容器是否可以正常使用nfs挂载卷
### --- 进入nginx2容器中,查看挂载情况
[root@k8s-master01 ~]# kubectl exec -ti nginx-78658dcf78-6nhg8 -c nginx2 -- bash
### --- 查看挂载到那个目录下
root@nginx-78658dcf78-6nhg8:/# df -Th
Filesystem Type Size Used Avail Use% Mounted on
192.168.1.14:/data/nfs/yanqi_dp nfs4 95G 3.8G 92G 4% /opt
### --- touch一个文件,查看是否可以创建成功
root@nginx-78658dcf78-6nhg8:/# touch /opt/yanqi_nfs
### --- 在k8s-node01下查看创建的文件是否成功
~~~ 创建的文件已经存在
[root@k8s-node01 ~]# ll /data/nfs/yanqi_dp/yanqi_nfs
-rw-r--r-- 1 root root 0 Apr 27 15:51 /data/nfs/yanqi_dp/yanqi_nfs
~~~ # echo一些内容到该文件下
[root@k8s-node01 ~]# echo yanqi_nfs_test > /data/nfs/yanqi_dp/yanqi_nfs
~~~ # 回到nginx2容器中查看文件读写同步情况
~~~ 书写内容已经同步
root@nginx-78658dcf78-6nhg8:/# cat /opt/yanqi_nfs
yanqi_nfs_test
### --- 在生产环境中:
~~~ # 在生产环境中,不建议直接使用nfs,因为nfs这个工具并没有什么保障,很难实现高可用的架构。
~~~ 若是在云环境的话,可以使用NAS平台,NAS和nfs协议是兼容的。
~~~ 一般都是不会直接在volumes下直接写入nnfs的配置
~~~ 一般都是使用PV的方式,创建一个PV,这个PV连接到nfs上面,
~~~ 然后挂载一个PVC,这个PVC和PV进行连接。然后PV在和nfs连接。
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了