Kubernetes(k8s)存储管理之数据卷volumes(三):NFS数据卷
一.系统环境
| 服务器版本 | 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数据卷的更多相关文章
- python网络爬虫数据中的三种数据解析方式
一.正则解析 常用正则表达式回顾: 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字.字母.下划线 ...
- 简单操作:10分钟实现在kubernetes(k8s)里面部署服务器集群并访问项目(docker三)
前言 经过docker安装.k8s开启并登录,我们终于到 "部署k8s服务器集群并访问项目" 这一步了,实现的过程中有太多坑,好在都填平了,普天同庆. 在进行当前课题之前,我们需要 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之flanneld网络介绍及部署(三)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.flanneld介绍 ...
- docker基础---数据卷volumes
1.数据卷 数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性: 数据卷可以在容器之间共享和重用 对数据卷的修改会立马生效 对数据卷的更新,不会影响镜像 卷会一直存在 ...
- 《两地书》--Kubernetes(K8s)基础知识(docker容器技术)
大家都知道历史上有段佳话叫“司马相如和卓文君”.“皑如山上雪,皎若云间月”.卓文君这么美,却也抵不过多情女儿薄情郎. 司马相如因一首<子虚赋>得汉武帝赏识,飞黄腾达之后便要与卓文君“故来相 ...
- Kubernetes(k8s) docker集群搭建
原文地址:https://blog.csdn.net/real_myth/article/details/78719244 一.Kubernetes系列之介绍篇 •Kubernetes介绍 1.背 ...
- Kubernetes学习之路(十六)之存储卷
目录 一.存储卷的概念和类型 二.emptyDir存储卷演示 三.hostPath存储卷演示 四.nfs共享存储卷演示 五.PVC和PV的概念 六.NFS使用PV和PVC 1.配置nfs存储 2.定义 ...
- Kubernetes(K8s)基础知识(docker容器技术)
今天谈谈K8s基础知识关键词: 一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机 ...
- k8s集群,使用pvc方式实现数据持久化存储
环境: 系统 华为openEulerOS(CentOS7) k8s版本 1.17.3 master 192.168.1.244 node1 192.168.1.245 介绍: 在Kubernetes中 ...
- Kubernetes:存储管理
Blog:博客园 个人 参考:Volumes | Kubernetes.Persistent Volumes | Kubernetes.Kubernetes 基础入门实战 简单来说,存储卷是定义在Po ...
随机推荐
- 使用logstash读取MySQL数据传输到es,并且@timestamp字段采用MySQL中的字段时间--建议采用这个
MySQL中数据样式 ES中数据样式 input { jdbc { jdbc_connection_string => "jdbc:mysql://192.168.0.145:3306 ...
- 面试突击86:SpringBoot 事务不回滚?怎么解决?
在 Spring Boot 中,造成事务不自动回滚的场景有很多,比如以下这些: 非 public 修饰的方法中的事务不自动回滚: 当 @Transactional 遇上 try/catch 事务不自动 ...
- Python实现改进后的Bi-RRT算法实例
Python实现改进后的Bi-RRT算法实例 1.背景说明 以下代码是参照上海交通大学海洋工程国家重点实验室<基于改进双向RRT的无人艇局部路径规划算法研究>的算法思想实现的. 2.算法流 ...
- Redis实现布隆过滤器解析
布隆过滤器原理介绍 [1]概念说明 1)布隆过滤器(Bloom Filter)是1970年由布隆提出的.它实际上是一个很长的二进制向量和一系列随机映射函数.布隆过滤器可以用于检索一个元素是否在一个集合 ...
- C++面向对象编程之虚指针、虚表
1.当编译器看到一个函数调用,有2个考量:静态绑定or动态绑定 静态绑定是"call xxx",xxx 是表示地址,call 是汇编语言的一个动作,它一定会调用到某个地址: 当符合 ...
- 多线程的使用(springboot)
预备知识 业务使用多线程的原因 目的是面对高并发的时候,提高运行速度 场景一: 一个业务逻辑有很多次的循环,每次循环之间没有影响,比如验证1万条url路径是否存在,正常情况要循环1万次,逐个去验证每一 ...
- Kafka之概述
Kafka之概述 一.消息队列内部实现原理 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消 ...
- Spring的同一个服务为什么会加载多次?
问题现象 最近在本地调试公司的一个Web项目时,无意中发现日志中出现了两次同一个服务的init记录,项目都是基于Spring来搭建的,按理说服务都是单例的,应该只有一次服务加载日志才对,本着对工作认真 ...
- 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
2.使用工厂方法模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
- JSP中使用response对象实现定时跳转网页
5秒后跳转到登录页面 <% response.setHeader("refresh","5;URL="login.jsp"); %>