飞天使-k8s基础组件分析-持久化存储

文章目录

      • emptyDir
      • hostpath
      • pv和pvc介绍
      • nfs作为静态pv案例
      • nfs作为动态pv案例
      • 使用本地文件夹作为pv
      • 改变默认存储类及回收策略
      • 参考文档

emptyDir

重启文件还有,但是如果杀了进程,则会丢失文件
创建pod
# kubectl apply –f redis.yaml校验pod是否处于运行,并观察pod的改变
# kubectl get pod redis –-watch在其它终端上执行如下命令进入容器
# kubectl exec –it redis -- /bin/bash在shell中,进入/data/redis,然后创建文件
# cd /data/redis/
# echo Hello > test-file运行以下命令查找redis的进程
# apt-get update
# apt-get install procps
# ps aux杀掉redis进程,并观察redis pod的改变
# kill <pid>再次进入到redis的容器,查看文件是否存在。
# kubectl exec –it redis -- /bin/bash[root@k8s-01 chapter07]# cat redis.yaml 
apiVersion: v1
kind: Pod
metadata:name: redis
spec:containers:- name: redisimage: redisvolumeMounts:- name: redis-storagemountPath: /data/redisvolumes:- name: redis-storageemptyDir: {}

hostpath

只要在一个node 里面,就会找到文件
[root@k8s-01 chapter07]# cat hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:name: test-pod
spec:containers:- image: nginxname: test-containervolumeMounts:- name: test-volumemountPath: /usr/share/nginxvolumes:- name: test-volumehostPath:path: /data

pv和pvc介绍

Pv: 是集群中的一段存储,由管理员提供或使用存储类动态提供。
Pvc(PersistentVolumeClaim)是用户对存储资源的请求。

在这里插入图片描述

nfs作为静态pv案例

新增nfs
[root@k8s-01 data]# vim /etc/exports
[root@k8s-01 data]# exportfs -rv
exporting 192.168.100.0/24:/data/nfs5
exporting 192.168.100.0/24:/data/nfs4
exporting 192.168.100.0/24:/data/nfs3
exporting 192.168.100.0/24:/data/nfs2
exporting 192.168.100.0/24:/data/nfs1
[root@k8s-01 data]# cat /etc/exports
/data/nfs1 192.168.100.0/24(rw,async,insecure,no_root_squash)
/data/nfs2 192.168.100.0/24(rw,async,insecure,no_root_squash)
/data/nfs3 192.168.100.0/24(rw,async,insecure,no_root_squash)
/data/nfs4 192.168.100.0/24(rw,async,insecure,no_root_squash)
/data/nfs5 192.168.100.0/24(rw,async,insecure,no_root_squash)
[root@k8s-01 data]# yum install -y nfs-utils rpcbind客户端安装 
yum install -y utils创建pv并查看Pv
# showmount –e 192.168.20.88 
# kubectl create –f nfs-pv.yaml
# kubectl get pv创建pvc
# kubectl create –f nfs-pvc.yaml使用以下命令查看pv和pvc是否绑定
kubectl get pvc创建pod使用先前创建的pvc
# kubectl create –f nginx-pvc.yaml
# kubectl get pod nginx-vol-pvc –o yaml[root@k8s-01 chapter07]# cat nfs-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:name: pv001labels:name: pv001
spec:nfs:path: /data/nfs1server: 192.168.20.111accessModes: ["ReadWriteMany","ReadWriteOnce"]capacity:storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv002labels:name: pv002
spec:nfs:path: /data/nfs2server: 192.168.20.111accessModes: ["ReadWriteMany","ReadWriteOnce"]capacity:storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv003labels:name: pv003
spec:nfs:path: /data/nfs3server: 192.168.20.111accessModes: ["ReadWriteMany","ReadWriteOnce"]capacity:storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv004labels:name: pv004
spec:nfs:path: /data/nfs4server: 192.168.20.111accessModes: ["ReadWriteMany","ReadWriteOnce"]capacity:storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv005labels:name: pv005
spec:nfs:path: /data/nfs5server: 192.168.20.111accessModes: ["ReadWriteMany","ReadWriteOnce"]capacity:storage: 2Gi[root@k8s-01 chapter07]# 
[root@k8s-01 chapter07]# 
[root@k8s-01 chapter07]# cat nfs-pvc.yml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mypvcnamespace: default
spec:accessModes: ["ReadWriteMany"]resources:requests:storage: 2Gi[root@k8s-01 chapter07]# cat nginx-pvc.yml 
apiVersion: v1
kind: Pod
metadata:name: nginx-vol-pvcnamespace: default
spec:containers:- name: mywwwimage: nginxvolumeMounts:- name: wwwmountPath: /usr/share/nginx/htmlvolumes:- name: wwwpersistentVolumeClaim:claimName: mypvc[root@k8s-01 chapter07]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON   AGE
pv001   1Gi        RWO,RWX        Retain           Available                                           2m46s
pv002   1Gi        RWO,RWX        Retain           Available                                           2m46s
pv003   1Gi        RWO,RWX        Retain           Available                                           2m46s
pv004   2Gi        RWO,RWX        Retain           Bound       default/mypvc                           2m46s
pv005   2Gi        RWO,RWX        Retain           Available                                           2m46s
[root@k8s-01 chapter07]# kubectl get pvc
NAME    STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc   Bound    pv004    2Gi        RWO,RWX                       2m8s进去查看
[root@k8s-01 chapter07]# kubectl exec -it nginx-vol-pvc -- bash
root@nginx-vol-pvc:/# df -h   
Filesystem                 Size  Used Avail Use% Mounted on
overlay                     50G   11G   40G  21% /
tmpfs                       64M     0   64M   0% /dev
tmpfs                      1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda2                   50G   11G   40G  21% /etc/hosts
shm                         64M     0   64M   0% /dev/shm
192.168.100.30:/data/nfs4   50G  8.9G   41G  18% /usr/share/nginx/html
tmpfs                      1.9G   12K  1.9G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                      1.9G     0  1.9G   0% /proc/acpi
tmpfs                      1.9G     0  1.9G   0% /proc/scsi
tmpfs                      1.9G     0  1.9G   0% /sys/firmware

nfs作为动态pv案例

安装部署存储
创建服务帐户
# kubectl create –f serviceaccount.yaml
创建集群角色并与服务帐户绑定
# kubectl create –f clusterrole.yaml
# kubectl create –f clusterrolebinding.yaml创建角色,并与服务帐户绑定
# kubectl create –f role.yaml
# kubectl create –f rolebinding.yaml创建动态存储类
# kubectl create –f class.yaml部署
# kubectl create –f deployment.yaml]
注意:以上也可以直接执行 kubectl apply –f ./nfs-de创建pv,pod及查看pvc是否通过类创建了pv并且绑定
# kubectl create –f test-claim.yaml
# kubectl create –f test-pod.yaml
# kubectl get pvc –n aishangwei文件内容
[root@k8s-01 nfs-de]# ll
total 28
-rw-r--r--. 1 root root 247 Aug 22 10:32 class.yaml
-rw-r--r--. 1 root root 306 Aug 22 10:32 clusterrolebinding.yaml
-rw-r--r--. 1 root root 525 Aug 22 10:32 clusterrole.yaml
-rw-r--r--. 1 root root 901 Aug 24 13:58 deployment.yaml
-rw-r--r--. 1 root root 311 Aug 22 10:32 rolebinding.yaml
-rw-r--r--. 1 root root 228 Aug 22 10:32 role.yaml
-rw-r--r--. 1 root root  76 Aug 22 10:32 serviceaccount.yaml
[root@k8s-01 nfs-de]# cat class.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
reclaimPolicy: Retain
parameters:archiveOnDelete: "false"[root@k8s-01 nfs-de]# cat clusterrolebinding.yaml 
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: run-nfs-client-provisioner
subjects:- kind: ServiceAccountname: nfs-client-provisionernamespace: default
roleRef:kind: ClusterRolename: nfs-client-provisioner-runnerapiGroup: rbac.authorization.k8s.io[root@k8s-01 nfs-de]# cat clusterrole.yaml 
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: nfs-client-provisioner-runner
rules:- apiGroups: [""]resources: ["persistentvolumes"]verbs: ["get", "list", "watch", "create", "delete"]- apiGroups: [""]resources: ["persistentvolumeclaims"]verbs: ["get", "list", "watch", "update"]- apiGroups: ["storage.k8s.io"]resources: ["storageclasses"]verbs: ["get", "list", "watch"]- apiGroups: [""]resources: ["events"]verbs: ["create", "update", "patch"][root@k8s-01 nfs-de]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:name: nfs-client-provisioner
spec:replicas: 1selector:matchLabels:app: nfs-client-provisionerstrategy:type: Recreatetemplate:metadata:labels:app: nfs-client-provisionerspec:serviceAccountName: nfs-client-provisionercontainers:- name: nfs-client-provisionerimage: quay.io/external_storage/nfs-client-provisioner:latestvolumeMounts:- name: nfs-client-rootmountPath: /persistentvolumesenv:- name: PROVISIONER_NAMEvalue: fuseim.pri/ifs- name: NFS_SERVERvalue: 192.168.100.30- name: NFS_PATHvalue: /data/nfs1volumes:- name: nfs-client-rootnfs:server: 192.168.100.30path: /data/nfs1[root@k8s-01 nfs-de]# ls
class.yaml  clusterrolebinding.yaml  clusterrole.yaml  deployment.yaml  rolebinding.yaml  role.yaml  serviceaccount.yaml
[root@k8s-01 nfs-de]# ll
total 28
-rw-r--r--. 1 root root 247 Aug 22 10:32 class.yaml
-rw-r--r--. 1 root root 306 Aug 22 10:32 clusterrolebinding.yaml
-rw-r--r--. 1 root root 525 Aug 22 10:32 clusterrole.yaml
-rw-r--r--. 1 root root 901 Aug 24 13:58 deployment.yaml
-rw-r--r--. 1 root root 311 Aug 22 10:32 rolebinding.yaml
-rw-r--r--. 1 root root 228 Aug 22 10:32 role.yaml
-rw-r--r--. 1 root root  76 Aug 22 10:32 serviceaccount.yaml
[root@k8s-01 nfs-de]# cat rolebinding.yaml 
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: leader-locking-nfs-client-provisioner
subjects:- kind: ServiceAccountname: nfs-client-provisionernamespace: default
roleRef:kind: Rolename: leader-locking-nfs-client-provisionerapiGroup: rbac.authorization.k8s.io[root@k8s-01 nfs-de]# cat role.yaml 
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: leader-locking-nfs-client-provisioner
rules:- apiGroups: [""]resources: ["endpoints"]verbs: ["get", "list", "watch", "create", "update", "patch"][root@k8s-01 nfs-de]# cat serviceaccount.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:name: nfs-client-provisioner[root@k8s-01 nfs-de]# [root@k8s-01 chapter07]# cat test-claim.yaml 
apiVersion: v1
kind: Namespace
metadata:name: aishangwei
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: test-claimnamespace: aishangweiannotations:volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:accessModes:- ReadWriteManyresources:requests:storage: 10Mi[root@k8s-01 chapter07]# cat test-pod.yaml 
kind: Pod
apiVersion: v1
metadata:name: test-podnamespace: aishangwei
spec:containers:- name: test-podimage: busyboxcommand:- "/bin/sh"args:- "-c"- "touch /mnt/aishangwei-SUCCESS && exit 0 || exit 1"volumeMounts:- name: nfs-pvcmountPath: "/mnt"restartPolicy: "Never"volumes:- name: nfs-pvcpersistentVolumeClaim:claimName: test-claim

在这里插入图片描述

使用本地文件夹作为pv


创建文件夹,并创建文件
# mkdir /mnt/data
# echo ‘Hello from Kubernetes storage’ > /mnt/data/index.html执行如下命令创建pv,并查看创建的pv信息
# kubectl create –f pv-volume.yaml
# kubectl get pv task-pv-volume创建pvc并校验pv和pvc的信息
# kubectl create –f pvc-claim.yaml
# kubectl get pv task-pv-volume
# kubectl get pvc task-pv-claim创建pod,并引用使用的pvc
# kubectl create –f pv-pod.yaml
# kubectl get pod taks-pv-pod[root@k8s-01 chapter07]# cat pv-volume.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:name: task-pv-volumelabels:type: local
spec:storageClassName: manualcapacity:storage: 10GiaccessModes:- ReadWriteOncehostPath:path: "/mnt/data"[root@k8s-01 chapter07]# cat pv-claim.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: task-pv-claim
spec:storageClassName: manualaccessModes:- ReadWriteOnceresources:requests:storage: 3Gi[root@k8s-01 chapter07]# cat pv-
pv-claim.yaml   pv-pod.yaml     pv-volume.yaml  
[root@k8s-01 chapter07]# cat pv-
pv-claim.yaml   pv-pod.yaml     pv-volume.yaml  
[root@k8s-01 chapter07]# cat pv-pod.yaml 
apiVersion: v1
kind: Pod
metadata:name: task-pv-pod
spec:volumes:- name: task-pv-storagepersistentVolumeClaim:claimName: task-pv-claimcontainers:- name: task-pv-containerimage: nginxports:- containerPort: 80name: "http-server"volumeMounts:- mountPath: "/usr/share/nginx/html"name: task-pv-storage

改变默认存储类及回收策略

查看存储类
# kubectl get storageclass2. 将存储类设置为非默认的
# kubectl patch storageclass <your-class-name> -p ‘{“metadata”:{“annotations”:{“storageclass.Kubernetes.io/is-default-class”:”false”}}}’3. 标记存储类为默认的
# kubectl patch storageclass <your-class-name> -p ‘{“metadata”:{“annotations”:{“storageclass.Kubernetes.io/is-default-class”:”true”}}}’列出持久卷
# kubectl get pv选择一个持久卷来改变它的回收策略
# kubectl patch pv <your-pv-name> -p ‘{“spec”:{“persistentVolumeReclaimPolicy”:”Retain”}}’查看设置是否正确
# kubectl get pv

参考文档

https://edu.csdn.net/course/detail/27762?spm=1003.2449.3001.8295.3

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/82209.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于CentOS搭建私有仓库harbor

环境&#xff1a; 操作系统&#xff1a;CentOS Linux 7 (Core) 内核&#xff1a; Linux 3.10.0-1160.el7.x86_64 目录 安装搭建harbor &#xff08;1&#xff09;安装docker编排工具docker compose &#xff08;2&#xff09;下载Harbor 安装包 &#xff08;3&…

mysql使用flashback恢复数据

常在河边走&#xff0c;哪有不湿鞋。如果我们经常操作数据库&#xff0c;很有可能就会造成误操作&#xff0c;假如我们不幸误删了数据&#xff0c;有没有办法快速恢复呢&#xff1f; 这里&#xff0c;我们就以用的最多的mysql举例&#xff0c;聊聊如何快速恢复数据。mysql官方貌…

vue2 vue中的常用指令

一、为什么要学习Vue 1.前端必备技能 2.岗位多&#xff0c;绝大互联网公司都在使用Vue 3.提高开发效率 4.高薪必备技能&#xff08;Vue2Vue3&#xff09; 二、什么是Vue 概念&#xff1a;Vue (读音 /vjuː/&#xff0c;类似于 view) 是一套 **构建用户界面 ** 的 渐进式 …

nginx服务

一、nginx概述&#xff1a; 1.Nginx简介&#xff1a; Nginx是一个高性能的HTTP和反向代理服务器。是一款轻量级的高性能的web服务器/反向代理服务器/电子邮件&#xff08;IMAP/POP3&#xff09;代理服务器&#xff0c;单台物理服务器可支持30 000&#xff5e;50 000个并发请求…

回归预测 | MATLAB实现WOA-BP鲸鱼优化算法优化BP神经网络多输入单输出回归预测(多指标,多图)

回归预测 | MATLAB实现WOA-BP鲸鱼优化算法优化BP神经网络多输入单输出回归预测&#xff08;多指标&#xff0c;多图&#xff09; 目录 回归预测 | MATLAB实现WOA-BP鲸鱼优化算法优化BP神经网络多输入单输出回归预测&#xff08;多指标&#xff0c;多图&#xff09;效果一览基本…

IDEA远程开发

IDEA远程开发 前期准备 IDEA的远程开发是在本地去操昨远程服务器上的代码&#xff0c;所以我们先需要准备一台服务器,在此我使用vmware虚拟出ubuntu-20.04.6的Server版本,以便后面演示。 Ubuntu的Java环境配置 JDK8 sudo apt install openjdk-8-jdkmaven sudo apt instal…

无涯教程-PHP - preg_replace()函数

preg_replace() - 语法 mixed preg_replace (mixed pattern, mixed replacement, mixed string [, int limit [, int &$count]] ); preg_replace()函数的操作与POSIX函数ereg_replace()相同&#xff0c;不同之处在于可以在模式和替换输入参数中使用正则表达式。 可选的输…

linux安装JDK及hadoop运行环境搭建

1.linux中安装jdk &#xff08;1&#xff09;下载JDK至opt/install目录下&#xff0c;opt下创建目录soft&#xff0c;并解压至当前目录 tar xvf ./jdk-8u321-linux-x64.tar.gz -C /opt/soft/ &#xff08;2&#xff09;改名 &#xff08;3&#xff09;配置环境变量&#xf…

物种气候生态位动态量化与分布特征模拟

在全球气候快速变化的背景下&#xff0c;理解并预测生物种群如何应对气候变化&#xff0c;特别是它们的地理分布如何变化&#xff0c;已经变得至关重要。利用R语言进行物种气候生态位动态量化与分布特征模拟&#xff0c;不仅可以量化描述物种对环境的需求和适应性&#xff0c;预…

电商平台京东平台获得京东商品描述API接口演示案例

京东商品描述API接口可以获取京东商品描述&#xff1a; 详细介绍商品的特点和功能,让消费者能够了解商品的具体用途和效果。 使用简洁明了的语言,避免使用过于专业的术语和长句子,让消费者能够轻松理解。 重点突出商品的卖点和优势,让消费者能够更加清晰地了解商品的价值 …

Kvm配置ovs网桥

环境&#xff1a;部署在kvm虚拟环境上&#xff08;让虚拟机和宿主机都可以直接从路由器获取到独立ip&#xff09; 1、安装ovs软件安装包并启动服务&#xff08;一般采用源码安装&#xff0c;此处用yum安装&#xff09; yum install openvswitch-2.9.0-3.el7.x86_64.rpm syste…

[虚幻引擎] DTGlobalVariable 插件说明,蓝图全局变量访问,设置, Get, Set。

本插件可以在蓝图或者UMG中直接访问指定的全局变量值&#xff0c;方便编写。 支持Bool&#xff0c;Byte&#xff0c;Int&#xff0c;Int64&#xff0c;Float&#xff0c;Name&#xff0c;String&#xff0c;Text&#xff0c;Vector&#xff0c;Rotator&#xff0c;Transform&am…