【原文链接】
一、基础准备
首先参照 Kubernetes----安装部署NFS服务器 在master节点搭建nfs服务器
然后在node节点执行如下命令安装nfs-utils工具包,注意node节点不需要启动nfs,安装工具包只是为了能驱动nfs
yum install -y nfs-utils
二、NFS实例演示
编辑nfs.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: volume-nfs
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
volumeMounts:
- name: logs-volume
mountPath: /var/log/nginx
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","tail -f /logs/access.log"]
volumeMounts:
- name: logs-volume
mountPath: /logs
volumes:
- name: logs-volume
nfs:
server: 192.168.16.40
path: /root/data/nfs
使用如下命令创建资源
[root@master Volume]# kubectl apply -f nfs.yaml
namespace/dev created
pod/volume-nfs created
[root@master Volume]#
使用如下命令查看创建的资源
[root@master Volume]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
volume-nfs 2/2 Running 0 13s 10.244.1.249 node1 <none> <none>
[root@master Volume]#
查看master节点即nfs共享目录下,发现此时已经有日志文件了,只不过此时日志文件内容为空
[root@master Volume]# ls /root/data/nfs/
access.log error.log
[root@master Volume]#
然后使用如下命令在master节点检测nfs共享目录下日志文件的内容变化
tail -f /root/data/nfs/access.log
然后在node节点使用如下命令请求三次nginx服务
curl 10.244.1.249:80
可以看到此时master节点nfs共享目录下日志文件的变化如下:
[root@master Volume]# tail -f /root/data/nfs/access.log
10.244.1.1 - - [07/Apr/2022:16:00:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.1.1 - - [07/Apr/2022:16:02:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.1.1 - - [07/Apr/2022:16:02:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
使用如下命令删除pod
[root@master Volume]# kubectl delete -f nfs.yaml
namespace "dev" deleted
pod "volume-nfs" deleted
[root@master Volume]#
当然此时日志仍然是存在的
[root@master Volume]# cat /root/data/nfs/access.log
10.244.1.1 - - [07/Apr/2022:16:00:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.1.1 - - [07/Apr/2022:16:02:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
10.244.1.1 - - [07/Apr/2022:16:02:07 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@master Volume]#