在 Windows 10 | Docker Desktop | Kubernetes 环境 使用 hostPath / local 为 POD 配置本机目录

1. 进入 wsl

# 以下目录根据需要创建,不限制位置
mkdir /data/nginx
# 以下目录必须在 /mnt/wsl 中创建
mkdir /mnt/wsl/nginx
# 链接目录
sudo mount --bind /data/nginx /mnt/wsl/nginx

2. 配置 host-path-test POD

# kubectl create -f host-path-test.yaml
# kubectl delete -f host-path-test.yaml

---
kind: Pod
apiVersion: v1
metadata:
  namespace: arno
  name: host-path-test
spec:
  containers:
    - name: host-path-test-container
      image: nginx:1.23.1
      ports:
        - containerPort: 80
          name: nginx
      volumeMounts:
        - mountPath: /data
          name: data
  volumes:
    - name: data
      hostPath:
        # 必须以 /run/desktop/mnt/host 开头
        # /run/desktop/mnt/host/wsl/nginx 对应上面创建的 /mnt/wsl/nginx
        path: /run/desktop/mnt/host/wsl/nginx
        type: Directory
  nodeSelector:
    kubernetes.io/hostname: docker-desktop

3. 部署 host-path-test POD

echo test > /data/nginx/index.html

# 创建 POD
kubectl create -f host-path-test.yaml
DOCKER_ID=`docker ps -q -f name=host-path-test`
docker exec -it ${DOCKER_ID} ls -lah /data
docker exec -it ${DOCKER_ID} cat /data/index.html

# 删除 POD
kubectl delete -f host-path-test.yaml

4. 配置 local-test SC/PV/PVC/POD 文件

# kubectl create -f local-test.yaml
# kubectl delete -f local-test.yaml

---
# 定义 local-storage 存储类
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  namespace: arno
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

---
# 创建 data 静态卷
kind: PersistentVolume
apiVersion: v1
metadata:
  namespace: arno
  name: data
spec:
  capacity:
    storage: 50Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    # 必须以 /run/desktop/mnt/host 开头
    # /run/desktop/mnt/host/wsl/nginx 对应上面创建的 /mnt/wsl/nginx
    path: /run/desktop/mnt/host/wsl/nginx
  nodeAffinity:
    required:
      # 将 静态卷 固定在 docker-desktop 节点
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop

---
# 创建 nginx-pvc 申领
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: arno
  name: nginx-pvc
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

---
# 创建 local-test POD
kind: Pod
apiVersion: v1
metadata:
  namespace: arno
  name: local-test
spec:
  containers:
    - name: local-test-container
      image: nginx:1.23.1
      ports:
        - containerPort: 80
          name: nginx
      volumeMounts:
        - mountPath: /data
          name: data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: nginx-pvc
  nodeSelector:
    kubernetes.io/hostname: docker-desktop

5. 部署 local-test local-test SC/PV/PVC/POD

echo test2 > /data/nginx/index2.html

# 创建 SC/PV/PVC/POD
kubectl create -f local-test.yaml
kubectl get pv -n arno
kubectl get pvc -n arno
DOCKER_ID=`docker ps -q -f name=local-test`
docker exec -it ${DOCKER_ID} ls -lah /data
docker exec -it ${DOCKER_ID} cat /data/index2.html

# 删除 SC/PV/PVC/POD
kubectl delete -f local-test.yaml

6. 参考:
    1. [Unable to mount volume using hostPath
    2. [#5325 (comment)
    3. [Kubernetes Volumes not correctly mounted with WSL2

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注