一.系统环境

服务器版本 docker软件版本 Kubernetes(k8s)集群版本 CPU架构
CentOS Linux release 7.4.1708 (Core) Docker version 20.10.12 v1.21.9 x86_64

Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点

服务器 操作系统版本 CPU架构 进程 功能描述
k8scloude1/192.168.110.130 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master节点
k8scloude2/192.168.110.129 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点
k8scloude3/192.168.110.128 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点

二.前言

Kubernetes(k8s)数据卷volumes类型众多,本文介绍数据卷volumes之一NFS数据卷

使用数据卷volumes的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/16686769.html

三.NFS数据卷

3.1 NFS数据卷概览

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

说明:在使用 NFS 卷之前,你必须运行自己的 NFS 服务器并将目标 share 导出备用。

还需要注意,不能在 Pod spec 中指定 NFS 挂载可选项。 可以选择设置服务端的挂载可选项,或者使用 /etc/nfsmount.conf。 此外,还可以通过允许设置挂载可选项的持久卷挂载 NFS 卷。

3.2 配置NFS服务端以及共享目录

此次共享存储以nfs为例,在一台机器上安装NFS服务端,k8s的两个worker安装NFS客户端。

etcd1机器作为NFS的服务端,安装NFS

[root@etcd1 ~]# yum -y install nfs-utils

[root@etcd1 ~]# rpm -qa | grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64

启动NFS

#使nfs开机自启动并现在就启动
[root@etcd1 ~]# systemctl enable nfs-server --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. #查看nfs状态
[root@etcd1 ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since 二 2022-01-18 17:24:24 CST; 8s ago
Process: 1469 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl reload gssproxy ; fi (code=exited, status=0/SUCCESS)
Process: 1453 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 1451 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 1453 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service 1月 18 17:24:24 etcd1 systemd[1]: Starting NFS server and services...
1月 18 17:24:24 etcd1 systemd[1]: Started NFS server and services.

创建NFS共享目录,并把目录/sharedir共享出去

#创建/sharedir作为共享目录
[root@etcd1 ~]# mkdir /sharedir [root@etcd1 ~]# vim /etc/exports #把/sharedir目录共享出去
[root@etcd1 ~]# cat /etc/exports
/sharedir *(rw,async,no_root_squash) [root@etcd1 ~]# exportfs -arv
exporting *:/sharedir

3.3 配置NFS客户端

在k8s集群的worker节点安装nfs的客户端

[root@k8scloude3 ~]# yum -y install nfs-utils

 #安装nfs的客户端
[root@k8scloude2 ~]# yum -y install nfs-utils

查看etcd1(192.168.110.133)机器共享出来的目录是哪个?

[root@k8scloude2 ~]# showmount -e 192.168.110.133
Export list for 192.168.110.133:
/sharedir *

把192.168.110.133:/sharedir的目录挂载到/mnt

[root@k8scloude2 ~]# mount 192.168.110.133:/sharedir /mnt

[root@k8scloude2 ~]# df -hT /mnt
文件系统 类型 容量 已用 可用 已用% 挂载点
192.168.110.133:/sharedir nfs4 150G 2.5G 148G 2% /mnt

3.4 创建有NFS卷的pod

配置nfs卷,指定共享数据卷的类型为nfs,指定NFS服务器IP和共享目录

[root@k8scloude1 volume]# vim share-nfs.yaml 

[root@k8scloude1 volume]# cat share-nfs.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: hostpath
name: nfsshare
spec:
#nodeName指定该pod运行在k8scloude2节点
nodeName: k8scloude2
terminationGracePeriodSeconds: 0
volumes:
- name: v1
#数据卷的类型为nfs
nfs:
#nfs服务器地址
server: 192.168.110.133
#共享目录
path: /sharedir
#readOnly: true只读
#readOnly: true
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: h1
resources: {}
volumeMounts:
- name: v1
#把v1卷挂载到/xx目录
mountPath: /xx
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

创建pod

[root@k8scloude1 volume]# kubectl apply -f share-nfs.yaml
pod/nfsshare created [root@k8scloude1 volume]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfsshare 1/1 Running 0 3s 10.244.112.189 k8scloude2 <none> <none>

进入pod

[root@k8scloude1 volume]# kubectl exec -it nfsshare -- bash
root@nfsshare:/# ls /xx/ #往共享目录里写入数据
root@nfsshare:/# echo "well well well" >/xx/log.txt
root@nfsshare:/#
root@nfsshare:/# exit
exit

k8scloude2机器上有对应的文件

[root@k8scloude2 ~]# cat /mnt/log.txt
well well well

etcd1机器上也有对应的文件

[root@etcd1 ~]# cat /sharedir/log.txt
well well well

删除pod

[root@k8scloude1 volume]# kubectl delete pod nfsshare --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nfsshare" force deleted [root@k8scloude1 volume]# kubectl get pods -o wide
No resources found in volume namespace.

配置nfs卷,指定共享数据卷的类型为nfs,指定NFS服务器IP和共享目录,不过让pod运行在k8scloude3上

[root@k8scloude1 volume]# vim share-nfs.yaml 

#nodeName: k8scloude3  指定pod运行在k8scloude3上
[root@k8scloude1 volume]# cat share-nfs.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: hostpath
name: nfsshare
spec:
nodeName: k8scloude3
terminationGracePeriodSeconds: 0
volumes:
- name: v1
nfs:
server: 192.168.110.133
path: /sharedir
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: h1
resources: {}
volumeMounts:
- name: v1
mountPath: /xx
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

创建pod

[root@k8scloude1 volume]# kubectl apply -f share-nfs.yaml
pod/nfsshare created [root@k8scloude1 volume]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfsshare 1/1 Running 0 3s 10.244.251.251 k8scloude3 <none> <none>

因为使用的是NFS共享存储卷,进入pod,对应的目录都有文件

[root@k8scloude1 volume]# kubectl exec -it nfsshare -- bash
root@nfsshare:/# cat /xx/log.txt
well well well
root@nfsshare:/#
root@nfsshare:/# exit
exit

当pod调度在k8scloude3上,也出现了相应NFS挂载

[root@k8scloude3 ~]# df -h | grep 192.168.110.133
192.168.110.133:/sharedir 150G 2.5G 148G 2% /var/lib/kubelet/pods/4ebc5f6d-e13c-4bea-a323-3067c4a6e966/volumes/kubernetes.io~nfs/v1

删除pod

[root@k8scloude1 volume]# kubectl delete pod nfsshare --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nfsshare" force deleted [root@k8scloude1 volume]# kubectl get pods -o wide
No resources found in volume namespace.

当删除pod,则这个挂载消失

[root@k8scloude3 ~]# df -h | grep 192.168.110.133

NFS卷存在的问题:每个人都可能连接到存储服务器,都必须使用root权限,对服务器来说具有安全隐患,还要花时间学习NFS知识。

Kubernetes(k8s)存储管理之数据卷volumes(三):NFS数据卷的更多相关文章

  1. python网络爬虫数据中的三种数据解析方式

    一.正则解析 常用正则表达式回顾: 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字.字母.下划线 ...

  2. 简单操作:10分钟实现在kubernetes(k8s)里面部署服务器集群并访问项目(docker三)

    前言 经过docker安装.k8s开启并登录,我们终于到 "部署k8s服务器集群并访问项目" 这一步了,实现的过程中有太多坑,好在都填平了,普天同庆. 在进行当前课题之前,我们需要 ...

  3. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之flanneld网络介绍及部署(三)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.flanneld介绍 ...

  4. docker基础---数据卷volumes

    1.数据卷 数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性: 数据卷可以在容器之间共享和重用 对数据卷的修改会立马生效 对数据卷的更新,不会影响镜像 卷会一直存在 ...

  5. 《两地书》--Kubernetes(K8s)基础知识(docker容器技术)

    大家都知道历史上有段佳话叫“司马相如和卓文君”.“皑如山上雪,皎若云间月”.卓文君这么美,却也抵不过多情女儿薄情郎. 司马相如因一首<子虚赋>得汉武帝赏识,飞黄腾达之后便要与卓文君“故来相 ...

  6. Kubernetes(k8s) docker集群搭建

    原文地址:https://blog.csdn.net/real_myth/article/details/78719244 一.Kubernetes系列之介绍篇   •Kubernetes介绍 1.背 ...

  7. Kubernetes学习之路(十六)之存储卷

    目录 一.存储卷的概念和类型 二.emptyDir存储卷演示 三.hostPath存储卷演示 四.nfs共享存储卷演示 五.PVC和PV的概念 六.NFS使用PV和PVC 1.配置nfs存储 2.定义 ...

  8. Kubernetes(K8s)基础知识(docker容器技术)

    今天谈谈K8s基础知识关键词: 一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机 ...

  9. k8s集群,使用pvc方式实现数据持久化存储

    环境: 系统 华为openEulerOS(CentOS7) k8s版本 1.17.3 master 192.168.1.244 node1 192.168.1.245 介绍: 在Kubernetes中 ...

  10. Kubernetes:存储管理

    Blog:博客园 个人 参考:Volumes | Kubernetes.Persistent Volumes | Kubernetes.Kubernetes 基础入门实战 简单来说,存储卷是定义在Po ...

随机推荐

  1. 3.配置Grafana Dashboard

    本次我们通过部署 Grafana 来进行图形展示,Grafana 为我们提供了非常多的图形模板. Grafana 官网:https://grafana.com/ 1.下载安装 Grafana 我们使用 ...

  2. 在CentOS 8服务器上搭建FastDFS环境

    什么是FastDFS? 这里,我就摘录下百度百科上对于FastDFS的描述. FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下 ...

  3. Git Review + Gerrit 安装及使用完成 Code-Review

    转载自:https://cloud.tencent.com/developer/article/1010615 1.Code Review 介绍 Code Review 代码评审是指在软件开发过程中, ...

  4. JS 模块化- 05 ES Module & 4 大规范总结

    1 ES Module 规范 ES Module 是目前使用较多的模块化规范,在 Vue.React 中大量使用,大家应该非常熟悉.TypeScript 中的模块化与 ES 类似. 1.1 导出模块 ...

  5. C++自学笔记 头文件 (header file)关于 #include 和.h

    头文件 在C++中定义Definition一个类的时候 要用分别的.h和.cpp文件去定义这个类 .h和.cpp成对出现 类的声明declaration和函数原型放在头文件里(.h) 定义这些函数的结 ...

  6. Go微服务实战 - 从0到1搭建一个类Instagram应用(持续更新)

    概要 近几年各大移动应用基本都有社区Community(或动态Moments)的功能,展现形式各不相同,比如 国内的有:微博.朋友圈.抖音.小红书.keep.绿洲.即刻等 国外的有:Instagram ...

  7. 鸟哥linux私房菜习题总结

    第零章 1.请找出世界上跑得最快的超级计算机的相关信息? 系统名称:天河二号. 所在位置:中国广州中山大学超算中心. 使用的cpu型号与规格:采用的是Intel的Ivy Bridge处理器与Xeon ...

  8. Python地图栅格化实例

    Python地图栅格化实例 引言 shapefile是GIS中的一种非常重要的数据类型,由ESRI开发的空间数据开放格式,目前该数据格式已经成为了GIS领域的开放标准.目前绝大多数开源以及收费的GIS ...

  9. 11.MongoDB系列之连接副本集

    1. Python连接副本集 from pymongo import MongoClient from bson.codec_options import CodecOptions from retr ...

  10. Response对象页面重定向、时间的动态显示

    Response对象 response对象主要用于对客户端的请求进行回应,将web服务器处理后的结果发回给客户端,封装了jsp产生的响应,并发送到客户端响应客户端的请求,请求的数据可以是各种数据类型, ...