> 文章列表 > K8S持久化存储--- nfs 卷的存储

K8S持久化存储--- nfs 卷的存储

K8S持久化存储--- nfs 卷的存储

条件:需要有至少3台k8s集群节点

[root@k8s-master mqq]# kubectl get nodes -o wide
NAME         STATUS   ROLES                  AGE     VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION           CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   8d      v1.20.5   192.168.243.180   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://23.0.2
k8s-node1    Ready    <none>                 7d23h   v1.20.5   192.168.243.181   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://23.0.2
k8s-node2    Ready    <none>                 7d23h   v1.20.5   192.168.243.182   <none>        CentOS Linux 7 (Core)   3.10.0-1160.el7.x86_64   docker://23.0.2
[root@k8s-master mqq]# 

1. nfs介绍

hostPath 存储,存在单点故障,pod 挂载 hostPath 时,只有调度到同一个 节点,数据才不会丢失 。 那可以使用 nfs 作为持久化存储 。

nfs 卷能将 NFS (网络文件系统) 挂载到你的 Pod 中。 不像 emptyDir 那样会在删除 Pod 的同时也会被删除,nfs 卷的内容在删除 Pod 时会被保存,卷只是被卸载。 这意味着 nfs 卷可以被预先填充数据,并且这些数据可以在 Pod 之间共享。

2. 搭建 nfs 服务

2.1 以 k8s 的k8s-master节点作为 NFS 服务端

yum -y  install nfs-utils
mkdir  /data/nfs   -pv
systemctl start nfs
systemctl enable nfs
cat > /etc/exports <<EOF
/data/nfs  *(rw,no_root_squash)
EOF
exportfs -arv #使NFS配置生效
systemctl restart nfs
systemctl status nfs

2.2 在所有node节点上安装nfs客户端

yum -y  install nfs-utils
systemctl start nfs
systemctl enable nfs
systemctl status nfs

2.3 在k8s-node1上手动挂载

mkdir /nfs
mount 192.168.243.180:/data/nfs  /nfs/
df -h #查看挂在
#umount /nfs #主动卸载,一般不用,这里是告诉呢命令

3. 在master节点上编写yaml文件

cat > nfs.yaml << EOF
apiVersion: v1
kind: Pod
metadata:name: test-nfs-volume
spec:containers:- name: test-nfsimage: nginximagePullPolicy: IfNotPresentports:- containerPort: 80protocol: TCPvolumeMounts:- name: nfs-volumesmountPath: /usr/share/nginx/htmlvolumes:- name: nfs-volumesnfs:path: /data/nfs   #这里是安装nfs共享目录server: 192.168.243.180 #这里是安装nfs共享目录的服务器IP
EOF
[root@k8s-master mqq]# kubectl get pods -o wide #查看pod运行正常
NAME              READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
test-nfs-volume   1/1     Running   0          6m44s   10.244.36.78   k8s-node1   <none>           <none>
[root@k8s-master nfs]# cat > /data/nfs/index.html <<EOF #在共享目录下写入一个文件
My name is MQQ
My Chat is mqq759035366
EOF
[root@k8s-master nfs]# curl 10.244.36.78
My name is MQQ
My Chat is mqq759035366
[root@k8s-master nfs]# 

4. 在pod容器中验证

[root@k8s-master nfs]# kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
test-nfs-volume   1/1     Running   0          9m19s
[root@k8s-master nfs]# kubectl exec -it test-nfs-volume -- /bin/bash
root@test-nfs-volume:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@test-nfs-volume:/# cd /usr/share/nginx/html/
root@test-nfs-volume:/usr/share/nginx/html# ls -l
total 4
-rw-r--r-- 1 root root 39 Apr  9 04:21 index.html
root@test-nfs-volume:/usr/share/nginx/html# cat index.html #该文件和宿主机共享目录下一致
My name is MQQ
My Chat is mqq759035366
root@test-nfs-volume:/usr/share/nginx/html# 

5.在k8s-node1中验证

因为刚才对k8s-node1中对共享目录进行了挂在

[root@k8s-node1 ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
192.168.243.180:/data/nfs   36G  6.8G   29G  20% /nfs
[root@k8s-node1 ~]# cd /nfs/
[root@k8s-node1 nfs]# ls
index.html
[root@k8s-node1 nfs]# cat index.html #数据一致
My name is MQQ
My Chat is mqq759035366
[root@k8s-node1 nfs]#