k8s之持久卷NFS
一、简介
NFS网络存储卷,Kubernetes原生支持NFS作为Kubernetes的持久存储卷之一。NFS可以实现Pod的跨界点的数据持久性。
- 首先需要创建一个nfs 服务器,作为存储服务器;
- 将nfs服务器上导出 (export)的文件系统用作存储卷;
- nfs是文件系统级共享服务,它支持多路挂载请求,可由多个Pod对象同时用作存储卷后端;
二、准备NFS服务器
1、安装NFS-server
root@k8s-node03:~# apt search ^nfs-ker
Sorting... Done
Full Text Search... Done
nfs-kernel-server/jammy-updates 1:2.6.1-1ubuntu1.2 amd64
support for NFS kernel server
root@k8s-node03:~# apt install nfs-kernel-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nfs-kernel-server
0 upgraded, 1 newly installed, 0 to remove and 65 not upgraded.
Need to get 140 kB of archives.
After this operation, 526 kB of additional disk space will be used.
Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu jammy-updates/main amd64 nfs-kernel-server amd64 1:2.6.1-1ubuntu1.2 [140 kB]
Fetched 140 kB in 2s (72.6 kB/s)
Selecting previously unselected package nfs-kernel-server.
(Reading database ... 94308 files and directories currently installed.)
Preparing to unpack .../nfs-kernel-server_1%3a2.6.1-1ubuntu1.2_amd64.deb ...
Unpacking nfs-kernel-server (1:2.6.1-1ubuntu1.2) ...
Setting up nfs-kernel-server (1:2.6.1-1ubuntu1.2) ...
Created symlink /etc/systemd/system/nfs-client.target.wants/nfs-blkmap.service → /lib/systemd/system/nfs-blkmap.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /lib/systemd/system/nfs-server.service.
nfs-mountd.service is a disabled or a static unit, not starting it.
nfsdcld.service is a disabled or a static unit, not starting it.
Creating config file /etc/exports with new version
Creating config file /etc/default/nfs-kernel-server with new version
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning linux images...
Restarting services...
Service restarts being deferred:
/etc/needrestart/restart.d/dbus.service
systemctl restart docker.service
systemctl restart getty@tty1.service
systemctl restart networkd-dispatcher.service
systemctl restart systemd-logind.service
systemctl restart unattended-upgrades.service
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
2、准备共享目录
root@k8s-node03:~# mkdir -pv /data/redis
mkdir: created directory '/data'
mkdir: created directory '/data/redis'
root@k8s-node03:~# vi /etc/exports
root@k8s-node03:~# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/data/redis 192.168.10.0/24(rw,no_root_squash,no_subtree_check)
3、启动NFS服务
root@k8s-node03:~# systemctl restart nfs-server
root@k8s-node03:~# systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
Active: active (exited) since Tue 2024-01-23 22:14:32 CST; 10s ago
Process: 1595248 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 1595250 ExecStart=/usr/sbin/rpc.nfsd (code=exited, status=0/SUCCESS)
Main PID: 1595250 (code=exited, status=0/SUCCESS)
CPU: 10ms
Jan 23 22:14:32 k8s-node03 systemd[1]: Starting NFS server and services...
Jan 23 22:14:32 k8s-node03 systemd[1]: Finished NFS server and services.
三、客户端安装NFS工具
1、安装NFS客户端工具
root@k8s-node02:~# apt install nfs-common
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
nfs-common is already the newest version (1:2.6.1-1ubuntu1.2).
0 upgraded, 0 newly installed, 0 to remove and 65 not upgraded.
root@k8s-node03:~#
2、验证挂载NFS共享目录
root@k8s-node02:~# mount -t nfs 192.168.10.13:/data/redis /mnt/
root@k8s-node02:~# cd /mnt/
root@k8s-node02:/mnt# ls
root@k8s-node02:/mnt# touch test
root@k8s-node02:/mnt# echo 666> test
3、NFS查看已经有数据
root@k8s-node03:~# cat /data/redis/test
666
root@k8s-node03:~#
四、Pod挂载NFS存储卷,验证数据存储的持久性
1、创建Pod并挂载NFS存储卷
root@k8s-master01:~/learning-k8s/examples/volumes# cat pod-with-nfs-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis-nfs-002
spec:
containers:
- name: redis
image: redis:7-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: redisdata
mountPath: /data
volumes:
- name: redisdata
nfs:
server: 192.168.10.13
path: /data/redis
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl apply -f pod-with-nfs-vol.yaml
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod
NAME READY STATUS RESTARTS AGE
redis-nfs-002 1/1 Running 0 19s
2、进入容器创建测试数据
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it redis-nfs-002 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/data # redis-c
redis-check-aof redis-check-rdb redis-cli
/data # redis-cli
127.0.0.1:6379> SET mykey test
OK
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379> EXIT
/data # ls
dump.rdb test
3、删除Pod,验证数据持久性
- 第一次nfs Pod被调度在k8s-node02 节点上。
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis-nfs-002 1/1 Running 0 7m49s 10.244.1.4 k8s-node02 <none> <none>
- 删除Pod,并重新创建,人为调度到k8s-node01节点上
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl delete -f pod-with-nfs-vol.yaml
pod "redis-nfs-002" deleted
root@k8s-master01:~/learning-k8s/examples/volumes# cat pod-with-nfs-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis-nfs-002
spec:
nodeName: k8s-node01
containers:
- name: redis
image: redis:7-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: redisdata
mountPath: /data
volumes:
- name: redisdata
nfs:
server: 192.168.10.13
path: /data/redis
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl apply -f pod-with-nfs-vol.yaml
pod/redis-nfs-002 created
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis-nfs-002 1/1 Running 0 4s 10.244.2.31 k8s-node01 <none> <none>
- 进入容器查看数据没有丢失,实现跨界点的数据持久性
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it redis-nfs-002 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/data # ls
dump.rdb test
/data # redis-cli
127.0.0.1:6379> GET mykey
"test"
127.0.0.1:6379>
k8s之持久卷NFS的更多相关文章
- 通过搭建MySQL掌握k8s(Kubernetes)重要概念(上):网络与持久卷
上一篇"通过实例快速掌握k8s(Kubernetes)核心概念"讲解了k8s的核心概念,有了核心概念整个骨架就完整了,应付无状态程序已经够了,但还不够丰满.应用程序分成两种,无状态 ...
- Kubernetes(k8s)存储管理之数据卷volumes(四):持久卷Persistent Volume
目录 一.系统环境 二.前言 三.持久卷(Persistent Volume) 3.1 持久卷(Persistent Volume)概览 3.2 持久卷和持久卷申领的生命周期 3.3 持久卷的类型 3 ...
- K8s的存储卷使用总结
K8s的存储卷: 它有四种存储卷: 1. emptyDir: 空目录,这种存储卷会随着Pod的删除而被清空,它一般作为缓存目录使用,或临时目录, 当做缓存目录时,通常会将一块内存空间映射到该目录上,让 ...
- k8s之存储卷及pvc
1.存储卷概述 因为pod是有生命周期的,pod一重启,里面的数据就没了,所以我们需要数据持久化存储,在k8s中,存储卷不属于容器,而是属于pod,也就是说同一个pod中的容器可以共享一个存储卷,存储 ...
- k8s volume存储卷
k8s volume存储卷 介绍 volume存储卷是Pod中能够被多个容器访问的共享目录,kubernetes的volume概念,用途和目的与docker的volume比较类似,但两者不能等价, ...
- kubernets之持久卷的动态配置
一 介绍持久卷的动态配置原理 前面介绍的pv以及pvc,都需要kubernets集群管理员来支持实际的底层存储,但是kubernets还支持动态配置持久卷来自动化完成这个任务集群管理员可以创建一个持 ...
- 使用 FIO 对 Kubernetes 持久卷进行 Benchmark:读/写(IOPS)、带宽(MB/s)和延迟
工具 Dbench https://github.com/leeliu/dbench 用法 编辑 dbench.yaml 文件中的 storageClassName 以匹配你自己的 Storage C ...
- Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs
Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...
- K8S 使用简单的NFS 作为 持久存储的 StorageClass 的简单测试.
Study From https://jimmysong.io/kubernetes-handbook/practice/using-nfs-for-persistent-storage.html 1 ...
- 如何在Kubernetes集群动态使用 NAS 持久卷
1. 介绍: 本文介绍的动态生成NAS存储卷的方案:在一个已有文件系统上,自动生成一个目录,这个目录定义为目标存储卷: 镜像地址:registry.cn-hangzhou.aliyuncs.com/a ...
随机推荐
- Nebula 在 Akulaku 智能风控的实践:图模型的训练与部署
本文整理自 Akulaku 反欺诈团队在 nMeetup·深圳场的演讲,B站视频见:https://www.bilibili.com/video/BV1nQ4y1B7Qd 这次主要来介绍下 Nebul ...
- Codeforces Round 787 (Div. 3)D. Vertical Paths
题目链接 题意:给定一棵树,将这棵树划分成几天互不相交的链,要求最小化链的数量 思路:每个叶子节点一定在一条链中,所以链的数量就是叶子节点的数量,从叶子节点往上跳直到根节点,边跳边标记,路径上所有点都 ...
- 最强本地缓存Caffeine
Caffeine 是基于 JAVA 8 的高性能缓存库.并且在 spring5 (springboot 2.x) 后spring 官方放弃了 Guava,而使用了性能更优秀的 Caffeine 作为默 ...
- git 全局用户名改为英文,中文生成的git记录文件 不能有中文,现场反馈 git config user.name
设置用户名和邮箱 git config --global user.name "username" git config --global user.email useremail ...
- Cordon、Drain、污点与容忍度、亲和性与反亲和性
在Kubernetes(K8s)中,Cordon.Drain.污点与容忍度.亲和性与反亲和性都是与资源管理和调度相关的概念.下面是对这些概念的详细解释: Cordon(封锁.警戒): Cordon是一 ...
- BES2500开发板介绍和入门
一 前记 BES2500是恒玄科技推出的款高端的TWS耳机芯片,该芯片的性能非常强悍.蓝牙5.2双模,1.8M的SRAM空间,ARM-M33的主核,绝对是音频耳机芯片中的高配,性能不是一般的强.该芯片 ...
- cpprestsdk移植到mingw,项目上传至github
如题 https://github.com/bbqz007/cpprestsdk4mingw 移植过程解决的问题,下面列出其中一些问题: 1. mingw对#pragma once支持不好. 须要在所 ...
- Eclipse之各个版本的区别
经常用到的是前五个版本: Eclipse IDE for Java EE Developers:是为J2EE开发的版本: Eclipse Classic:是Eclipse的经典版本,没有安装任何插件, ...
- Spring JDBCTemplate Query方法查询
queryspringtypessqldaoemail 近日系统有一个打印采购单的功能,发现连续打印多张后,主机宕机,看了下service和dao层的实现,很繁杂,估计原因主要出在组页面资料的时候,循 ...
- Web service是什么? (转载)
转载自 : Web service是什么?- 阮一峰的网络日志 作者: 阮一峰 日期: 2009年8月26日 我认为,下一代互联网软件将建立在Web service(也就是"云") ...