Kubernetes | 存储卷(1/2)

网友投稿 814 2022-10-25

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

Kubernetes | 存储卷(1/2)

1. 存储卷概述

Pod本身是具有生命周期的,所以Pod内部运行的容器及相关的数据,在Pod中是无法持久化存储的。我们知道,Docker支持配置容器使用存储卷,已实现数据在容器之外的存储空间中持久化存储。相应的,Kubernetes也支持类似的存储卷功能,此处存储卷与Pod资源绑定。

简单来说,存储卷是定义在Pod资源之上、可被其他内容所有容器挂载的共享目录,它关联至外部的存储设备之上的存储空间,从而独立于容器自身的文件系统,而数据是否具有持久化存储能力取决于存储卷自身是否支持持久存储机制,与Pod无关。

2. Kubernetes支持的存储类型

Kubernetes支持非常丰富的存储卷类型,总体上来看,大致可以分为如下三种类型:

本地存储,例如emptyDir、HostPath;

网络存储,nfs、cinder、cephfs、AzureFile;

特殊存储资源,例如Secret、ConfigMap

对于本地存储,emptyDir的生命周期与Pod资源相同,所以Pod一旦删除,存储的数据同时被删除。HostPath的生命周期与节点一致,当Pod重新被调度到其他节点时,虽然原节点上的数据没有被删除,但是Pod再使用此前的数据。

网络存储系统是独立于Kubernetes集群之外的存储资源,数据存储的持久性与Kubernetes集群解耦合。Kubernetes在集群中设计了一种集群级别的资源PersistentVolume(简称PV)来关联网络存储,用户通过PersistentVolumeClaim(简称PVC)来申请使用存储资源。

Secret和ConfigMap算是Kubernetes集群中两种特殊类型的存储资源。Secret用于向Pod传递敏感信息,例如密码、私钥、证书等。这些信息直接定义在镜像中容易导致泄露,用户可以讲这些信息集中存储在Secret中,由Pod进行挂载,从而实现敏感数据与系统解耦。

ConfigMap资源用于向Pod注入非敏感数据,用户将一些非敏感的配置数据存储在ConfigMap对象中,然后在Pod中使用ConfigMap卷引用它即可,从而帮助实现容器配置文件集中化定义和管理。

3. 存储卷的使用方法

在Pod中定义存储卷的配置有两部分组成:

通过.spec.volumes字段定义存储卷列表;通过.spec.containers。volumeMounts字段在容器上定义存储卷挂载列表;如下面代码所示:

spec:# 定义存储卷列表 volumes: - name: XXXX # 定义存储卷的名称 emptyDir: {} # 在容器上定义存储卷挂载列表   containers: - name: nginx... volumeMounts:   - name: XXXX # 容器中需要挂载的存储卷的名称;     mountPath: /usr/share/nginx/html #挂载点路径,容器系统上的路径     readOnly: yes # 挂载是否为只读卷

容器只能挂载当前Pod资源中定义的具体存储卷,当多个容器挂载了同一个存储卷时,此时容器间的数据可以实现共享。

4. emptyDir存储卷

emptyDir存储卷的生命周期与其所属的Pod对象相同,因此emptyDir通常用于数据缓存或临时存储,类似于Docker上的卷挂载。下面我们通过以下的示例来测试emptyDir存储卷

apiVersion: v1kind: Podmetadata: name: vol-emptydir-podspec: volumes: # 定义emptyDir类型的存储卷 - name: html emptyDir: {} containers: - name: nginx image: 192.168.1.120/harbor/nginx:latest imagePullPolicy: IfNotPresent volumeMounts:   - name: html    # 在容器Nginx中,挂载存储卷     mountPath: /usr/share/nginx/html   # 容器里挂载存储卷位置 - name: pagegen image: 192.168.1.120/harbor/alpine:latest imagePullPolicy: IfNotPresent volumeMounts:   - name: html   # 在容器pagegen中,挂载存储卷     mountPath: /html  # 存储卷挂载位置   command: ["/bin/sh", "-c"]  # 在容器pagegen中,每隔10s向存储卷中写入数据 args: - while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10; done

编辑好以上的YAML文件后,我们创建该Pod,

[root@master volume]# kubectl apply -f vol-emptydir.yaml pod/vol-emptydir-pod created[root@master volume]# kubectl get podsNAME READY STATUS RESTARTS AGEvol-emptydir-pod   2/2     Running   0          33s[root@master volume]# kubectl get pods -A -o wideNAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESdefault                vol-emptydir-pod                            2/2     Running            0          117s   10.244.2.58     node2               

在Pod中存在两个容器 Nginx和Pagegen, 且两个容器共享存储html,如下图所示

通过describe可以查看pod详情

[root@master volume]# kubectl describe pods vol-emptydir-podName: vol-emptydir-pod...IP: 10.244.2.58IPs: IP: 10.244.2.58Containers: nginx:... Mounts: /usr/share/nginx/html from html (rw) /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bbg25 (ro) pagegen:  ... Mounts: /html from html (rw) /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bbg25 (ro)...Volumes: html: Type: EmptyDir (a temporary directory that shares a pod's lifetime) Medium: SizeLimit: kube-api-access-bbg25: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional:     DownwardAPI:             true

‍通过curl可以访问Nginx提供的服务,并输出pagegen容器写入的日志信息

[root@master volume]# curl 10.244.2.58vol-emptydir-pod Sun Oct 31 04:55:08 UTC 2021vol-emptydir-pod Sun Oct 31 04:55:18 UTC 2021vol-emptydir-pod Sun Oct 31 04:55:28 UTC 2021vol-emptydir-pod Sun Oct 31 04:55:38 UTC 2021...

5. HostPath存储卷

hostPath类型的存储卷是指将工作节点上某个文件系统的目录或文件,该再到Pod中的一种存储卷,它可以独立于Pod资源的生命周期,因而具有持久性。它是工作节点本地的存储空间,仅适用于特定情况下的存储卷的使用需求。当Pod重新被调度到其他节点时,虽然原节点上的数据没有被删除,但是Pod再使用此前的数据。我们以下面的例子来说明本地存储:

apiVersion: v1kind: Podmetadata: name: vol-hostpathspec: volumes: - name: html hostPath:    path: /root/html  # 主机上的目录,需要提前创建好 # the path of html on node type: Directory containers: - name: nginx image: 192.168.1.120/harbor/nginx:latest imagePullPolicy: IfNotPresent ports: - containerPort: 80 protocol: TCP volumeMounts: - name: html mountPath: /usr/share/nginx/html

编辑完上述的YAML文件后,我们创建该Pod进行测试

[root@master volume]# kubectl apply -f vol-hostpath.yaml pod/vol-hostpath created[root@master volume]# kubectl describe pods vol-hostpathName: vol-hostpathNamespace: defaultPriority: 0Node: node2/192.168.1.104...IP: 10.244.2.61IPs: IP: 10.244.2.61Containers: nginx:   ... Mounts: /usr/share/nginx/html from html (rw) /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bhfcw (ro)...Volumes: html: Type: HostPath (bare host directory volume) Path: /root/html HostPathType: Directory ...

将以下的index.html文件复制进节点的/root/html文件中。

[root@node2 html]# pwd/root/html[root@node2 html]# cat index.html Welcome to nginx 2 !

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

由于主机的目录和容器Nginx的目录是共享的,所以我们访问容器Nginx提供的服务,其显示结果即为在节点目录下存储的内容,结果如下:

[root@master volume]# curl 10.244.2.61Welcome to nginx 2 !

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.

Thank you for using nginx.

6. 网络存储卷

Kubernetes支持众多类型的网络存储卷,包括传统的NAS或SAN设备(如NFS、iSCSI)、分布式存储(如GlusterFS、RBD)、云存储(如Cinder、AzureDisk)等。我们以NFS存储为例,来演示Kubernetes的网络存储。

首先,简要介绍一下NFS存储,并搭建一个NFS的存储服务。

启动NFS服务,并设定共享存储目录为

[root@localhost /]# cat /etc/exports/public *(rw,no_root_squash,sync)

在Kubernetes节点中验证NFS服务

[root@node2 html]# showmount -e 192.168.1.130Export list for 192.168.1.130:/public *[root@node2 html]# [root@node2 html]# mount -t nfs 192.168.1.130:/public /mnt[root@node2 html]# df -h文件系统 容量 已用 可用 已用% 挂载点...192.168.1.130:/public     17G  1.3G   16G    8% /mnt

通过下面的输出结果,确认NFS服务可以使用。

通过 umount /mnt命令卸载之后,我们在Kubernetes中将本地存储修改为网络存储,实现NFS服务器的存储扣减挂载到Pod中以供容器使用。

apiVersion: v1kind: Podmetadata: name: vol-nfsspec: volumes: - name: html nfs:    path: /public      # NFS服务上,共享的文件系统路径 server: 192.168.1.130 # NFS服务器 containers: - name: nginx image: 192.168.1.120/harbor/nginx:latest imagePullPolicy: IfNotPresent volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: pagegen image: 192.168.1.120/harbor/alpine:latest imagePullPolicy: IfNotPresent volumeMounts: - name: html mountPath: /html command: ["/bin/sh", "-c"] args: - while true; do echo $(hostname) $(date) >> /html/index.html; sleep 10;     done

测试Nginx服务,通过输出结果可以看出,NFS提供的共享目录被挂载到容器上:

# 在Kubernetes节点上:[root@master volume]# curl 10.244.2.62vol-nfs Sun Oct 31 06:38:27 UTC 2021vol-nfs Sun Oct 31 06:38:37 UTC 2021vol-nfs Sun Oct 31 06:38:47 UTC 2021vol-nfs Sun Oct 31 06:38:57 UTC 2021...#在NFS服务器上[root@localhost public]# pwd/public[root@localhost public]# lsindex.html text.txt[root@localhost public]# [root@localhost public]# cat index.html vol-nfs Sun Oct 31 06:38:27 UTC 2021vol-nfs Sun Oct 31 06:38:37 UTC 2021vol-nfs Sun Oct 31 06:38:47 UTC 2021...

与HostPath和emptyDir挂载方式不同的是,此时当Pod被删除后,再次重新挂载,数据依然是保存的。

[root@master volume]# curl 10.244.2.63vol-nfs Sun Oct 31 06:38:27 UTC 2021vol-nfs Sun Oct 31 06:38:37 UTC 2021vol-nfs Sun Oct 31 06:38:47 UTC 2021...vol-nfs Sun Oct 31 06:40:27 UTC 2021vol-nfs Sun Oct 31 06:40:37 UTC 2021...vol-nfs Sun Oct 31 06:46:48 UTC 2021vol-nfs Sun Oct 31 06:46:58 UTC 2021vol-nfs Sun Oct 31 06:47:39 UTC 2021vol-nfs Sun Oct 31 06:47:49 UTC 2021vol-nfs Sun Oct 31 06:47:59 UTC 2021...

下一节将介绍Kubernetes的PV和PVC存储和Secret、ConfigMap存储卷。

相关历史文章:

Kubernetes |  基于角色的访问控制(RBAC)

Kubernetes | 从发布一个Pod开始

Kubernetes | Deployment 多样性发布(1/2)

Kubernetes | Deployment 多样性发布(2/2)

参考资料:

[1]  马永亮. Kubernetes进阶实战[M]. 机械工业出版社, 2019.

[2]  K8S中使用nfs

https://jianshu.com/p/65ed4bdf0e89

[3] NFS服务器搭建与配置

https://blog.csdn.net/qq_38265137/article/details/83146421

[4] Kubernetes之pv、pvc及使用nfs网络存储应用

https://cnblogs.com/you-men/p/13227155.html

[5] K8S中使用nfs

https://jianshu.com/p/65ed4bdf0e89

上一篇:达梦DM7使用mybatis示例
下一篇:kubernetes之headless service运用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~