k8s 中存储之 hostPath 卷

news/2024/10/8 12:38:56 标签: kubernetes, 容器, 云原生

目录

1 hostPath 卷介绍

2 hostPath 卷实际应用操作

2.1 创建 pod 资源类型

2.2  修改清单文件增加 hostPath 对应的参数配置

2.3 查看是否创建 卷 和 pod

2.4 创建发布文件测试是否正常访问


1 hostPath 卷介绍

EmptyDir中数据不会被持久化,它会随着Pod的结束而销毁,如果想简单的将数据持久化到主机中,可以选择HostPath。

HostPath就是将Node主机中一个实际目录挂在到Pod中,以供容器使用,这样的设计就可以保证Pod销毁了,但是数据依据可以存在于Node主机上。

2 hostPath 卷实际应用操作

2.1 创建 pod 资源类型

[root@k8s-master volumes]# kubectl run hostpath \
--image nginx:latest --port 80 \
--dry-run=client -o yaml > hostpath.yml

2.2  修改清单文件增加 hostPath 对应的参数配置

[root@k8s-master volumes]# vim hostpath.yml 

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: hostpath
  name: hostpath
spec:
  volumes:
  # 定义一个名为 cache-vol 的 hostPath 卷
  # hostPath 类型的卷将主机文件系统的指定路径直接挂载到 Pod 中
  - name: cache-vol
    hostPath:
      path: /data  # 主机上的路径
      type: DirectoryOrCreate  # 指定路径类型,如果不存在则创建目录
  containers:
  # 容器 nginx-host
  - image: nginx:latest
    name: nginx-host
    volumeMounts:
    # 将 cache-vol 卷挂载到容器内的 /usr/share/nginx/html 路径
    # 这样容器可以访问主机上的 /data 目录
    - mountPath: /usr/share/nginx/html
      name: cache-vol
    ports:
    - containerPort: 80  # 容器内部监听的端口



关于 spec.volumes.hostPath.type 的值的一点说明:	
  DirectoryOrCreate 目录存在就使用,不存在就先创建后使用	
  Directory	目录必须存在	
  FileOrCreate  文件存在就使用,不存在就先创建后使用	
  File 文件必须存在	    
  Socket	unix套接字必须存在	
  CharDevice	字符设备必须存在	
  BlockDevice 块设备必须存在

2.3 查看是否创建 卷 和 pod

[root@k8s-master volumes]# kubectl get pods -o wide 
NAME                       READY   STATUS      RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
hostpath                   1/1     Running     0          59s     10.244.2.63   k8s-node2   <none>           <none>
nginx-v1-dbd4bc45b-49hhw   1/1     Running     0          3d18h   10.244.2.54   k8s-node2   <none>           <none>
nginx-v2-bd85b8bc4-nqpv2   1/1     Running     0          3d18h   10.244.1.35   k8s-node1   <none>           <none>
testpod                    0/1     Completed   0          3d5h    10.244.2.58   k8s-node2   <none>           <none>

[root@k8s-master volumes]# kubectl describe pod hostpath 
Name:             hostpath
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node2/192.168.239.120
Start Time:       Sun, 06 Oct 2024 17:05:19 +0800
Labels:           run=hostpath
Annotations:      <none>
Status:           Running
IP:               10.244.2.63
IPs:
  IP:  10.244.2.63
Containers:
  nginx-host:
    Container ID:   docker://416d1c3dfe66633c1c23519ddaa65f77d8157127c3583a79b47e57eca5913756
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:127262f8c4c716652d0e7863bba3b8c45bc9214a57d13786c854272102f7c945
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sun, 06 Oct 2024 17:05:20 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from cache-vol (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-64rsp (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True 
  Initialized                 True 
  Ready                       True 
  ContainersReady             True 
  PodScheduled                True 
Volumes:
  cache-vol:
    Type:          HostPath (bare host directory volume)
    Path:          /data
    HostPathType:  DirectoryOrCreate
  kube-api-access-64rsp:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  15m   default-scheduler  Successfully assigned default/hostpath to k8s-node2
  Normal  Pulling    15m   kubelet            Pulling image "nginx:latest"
  Normal  Pulled     15m   kubelet            Successfully pulled image "nginx:latest" in 89ms (89ms including waiting). Image size: 187694648 bytes.
  Normal  Created    15m   kubelet            Created container nginx-host
  Normal  Started    15m   kubelet            Started container nginx-host


# 测试发现找不到数据,这是因为在 node-2 主机中的/data 目录中没有index.html
[root@k8s-master volumes]# curl 10.244.2.63
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.27.1</center>
</body>
</html>

2.4 创建发布文件测试是否正常访问

# 查询 pod 调度到了哪台node上 
[root@k8s-master volumes]# kubectl get pods -o wide 
NAME                       READY   STATUS      RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
hostpath                   1/1     Running     0          59s     10.244.2.63   k8s-node2   <none>           <none>
nginx-v1-dbd4bc45b-49hhw   1/1     Running     0          3d18h   10.244.2.54   k8s-node2   <none>           <none>
nginx-v2-bd85b8bc4-nqpv2   1/1     Running     0          3d18h   10.244.1.35   k8s-node1   <none>           <none>
testpod                    0/1     Completed   0          3d5h    10.244.2.58   k8s-node2   <none>           <none>

# 在调度的 node 上增加发布文件
[root@k8s-node2 ~]# echo this is node-2 hostpath > /data/index.html

# 访问 pod 测试
[root@k8s-master volumes]# curl 10.244.2.63
this is node-2 hostpath


http://www.niftyadmin.cn/n/5694138.html

相关文章

常见的图像处理算法:均值滤波----mean filter

一、什么是均值滤波 均值滤波器是一种常见的图像滤波器&#xff0c;是典型的线性滤波算法。其基本原理是用一个给定的窗口覆盖图像中的每一个像素点&#xff0c;将窗口内的像素值求平均值&#xff0c;然后用这个平均值代替原来的像素值。均值滤波器可以去除噪声、平滑图像、减少…

swagger2.9.2 和 springboot3.3.4版本冲突问腿

swagger2.9.2 和 springboot3.3.4版本冲突问腿 问题描述&#xff1a;当我们使用 swagger 2.9.2版本的时候&#xff0c;如果恰好我们使用的 springboot 版本是3.x版本&#xff0c;会出现启动报错的问题 解决办法&#xff1a;直接使用swagger 3.x 版本和 springboot 3.x 版本 …

迷茫解惑(一)——面向就业

文章目录 一、前言二、关于实习三、关于私信四、关于发论文五、关于工程经验-产品经历六、效率七、关于导师的思想 一、前言 我总是是在不同的阶段会存在困惑&#xff0c;会问师兄、老师、亲戚、哥哥等等&#xff0c;今天做个小总结&#xff0c;以后应该会有不同的总结吧。 先…

AI绘画进阶工具 comfyUI好在哪?为啥最新的AI绘画技术都在使用它?一文带你解锁ComfyUI的优缺点

大家好&#xff0c;我是画画的小强 comfyUI自从面世以来&#xff0c;就以一种潜力股的姿态快速流行了起来&#xff0c;越来越多的小伙伴开始使用comfyUI。也许你一开始会被comfyUI密密麻麻的“线路”吓到&#xff0c;但其实comfyUI也没那么复杂&#xff0c;并且好处多多。 本…

C语言-指针变量,常量与数组名的细微区别辨析

本节根据两个选择题进行展开辨析 一、例1 本题答案&#xff1a;C 解析&#xff1a;强干扰选项是B&#xff0c;我相信大多数同学都会在B&#xff0c;C之间犹豫好久&#xff0c;那么为什么答案会最终选择C呢&#xff1f;因为本题在定义函数&#xff0c;所以a首先是一个数组名&a…

PostgreSQL 和Oracle 表压缩的对比

PostgreSQL 和Oracle 表压缩的对比 Oracle 和 PostgreSQL 在表压缩的性能方面存在显著差异&#xff0c;主要体现在实现方式、压缩效果、对系统性能的影响以及适用场景等方面。以下是对两者表压缩性能的详细对比&#xff1a; 1. 实现方式 Oracle 表压缩 Oracle 提供了多种压…

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07 目录 文章目录 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-07目录1. Evaluation of Large Language Models for Summarization Tasks in the Medical Domain: A Narrative Review摘要研究…

【玩转 JS 函数式编程_009】3.1.3 JavaScript 函数式编程筑基之:将函数视为一等对象

文章目录 3.1.3. 将函数用作对象 Functions as objects1. React-Redux 中的 reducer&#xff08;A React-Redux reducer&#xff09;2. 不必要的错误 An unnecessary mistake3. 正确处理“方法” Working with methods 3.1.3. 将函数用作对象 Functions as objects 所谓一等对…