k8s 中存储之 hostPath 卷

目录

[1 hostPath 卷介绍](#1 hostPath 卷介绍)

[2 hostPath 卷实际应用操作](#2 hostPath 卷实际应用操作)

[2.1 创建 pod 资源类型](#2.1 创建 pod 资源类型)

[2.2 修改清单文件增加 hostPath 对应的参数配置](#2.2 修改清单文件增加 hostPath 对应的参数配置)

[2.3 查看是否创建 卷 和 pod](#2.3 查看是否创建 卷 和 pod)

[2.4 创建发布文件测试是否正常访问](#2.4 创建发布文件测试是否正常访问)


1 hostPath 卷介绍

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

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

2 hostPath 卷实际应用操作

2.1 创建 pod 资源类型

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

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

bash 复制代码
[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

bash 复制代码
[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 创建发布文件测试是否正常访问

bash 复制代码
# 查询 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
相关推荐
The_Second_Coming22 分钟前
容器应用学习笔记:containerd 篇
运维·笔记·学习·容器·containerd
一个帅气昵称啊24 分钟前
Docker命令大全:从基础到高级实战指南
docker·容器·eureka·架构·.net
zhysunny25 分钟前
51.不可变基础设施:云原生时代的「乐高城堡」建造法
java·云原生
*伤^情*27 分钟前
docker教程
运维·docker·容器
程序消消乐3 小时前
ZooKeeper Multi-op+乐观锁实战优化:提升分布式Worker节点状态一致性
分布式·zookeeper·云原生
rockmelodies4 小时前
【容器使用】如何使用 docker 和 tar 命令来操作容器镜像
运维·docker·容器
DO_Community5 小时前
DigitalOcean Kubernetes 现已支持 Gateway API 托管服务
容器·kubernetes·gateway
什么都想学的阿超7 小时前
【大语言模型 57】容器化训练环境:Docker + Kubernetes + Slurm
docker·语言模型·kubernetes
敲上瘾7 小时前
Docker网络实战:容器通信与隔离之道
linux·网络·docker·微服务·容器
@ chen8 小时前
kubectl常用命令
云原生·kubernetes·k8s