k8s 中存储之 hostPath 卷

时间:2024-10-07 07:01:06

目录

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