Infrastructure Kubernetes Operating system Windows
Bell  

Windows WSL2 환경 Docker Desktop Kubernetes hostPath 마운트 방법

전제 조건

  • OS: Windows 10 or Windows 11
  • WSL2 기반 Ubuntu
  • Docker Desktop Kubernetes
  • 로컬 개발환경 구성 등을 위해 WSL2 기반 Ubuntu 내 디렉토리를 실시간으로 사용하기 위해 hostPath 마운트가 필요할 경우

1 단계

apiVersion: v1
kind: Pod
metadata:
  name: wsl2-debug-pod
spec:
  containers:
    - name: busybox
      image: busybox
      command:
        - sleep
        - "3600"
      volumeMounts:
        - mountPath: /mnt/host
          name: host-root
  restartPolicy: Always
  volumes:
    - name: host
      hostPath:
        path: /

위 디버깅용 pod yaml 을 적용

2 단계

kubectl exec wsl2-debug-pod -it /bin/sh

디버깅용 pod 에 접속

cd /mnt/host

마운트한 디렉토리로 이동하여 WSL2 우분투 디렉토리가 k8s 환경에서 어떻게 마운트 되는지 확인

ls -al run/desktop/mnt/host/wsl/ubuntu/

명령으로 확인하면 해당 디렉토리 하위가 WSL2 기반 우분투의 루트 디렉토리임을 확인할 수 있음

3 단계

위 과정으로 k8s 환경 내에서 /run/desktop/mnt/host/wsl/ubuntu/ 디렉토리가 WSL2 기반 우분투 환경의 루트 디렉토리임을 확인

ex) 로컬 개발환경 구성을 위해 프로젝트 루트 디렉토리를 k8s pod 내에서 hostPath 로 마운트하여 사용 시

WSL2 우분투 내 프로젝트 디렉토리: /home/bell/programming/git/intellij/sample-project

-> k8s 환경 hostPath: /run/desktop/mnt/host/wsl/ubuntu/home/bell/programming/git/intellij/sample-project

apiVersion: v1
kind: Pod
metadata:
  name: wsl2-realtime-sync-path-pod
spec:
  containers:
    - name: busybox
      image: busybox
      command:
        - sleep
        - "3600"
      volumeMounts:
        - mountPath: /mnt/project
          name: project-root
  restartPolicy: Always
  volumes:
    - name: project-root
      hostPath:
        path: /run/desktop/mnt/host/wsl/ubuntu/home/bell/programming/git/intellij/sample-project

위 형태로 hostPath 값을 설정하면 우분투 환경에서 프로젝트 파일 변경 시 k8s 환경 내 pod 에서 실시간으로 반영되어 gradle continuous build 또는 vue.js 의 yarn dev 같이 파일 변경 시 실시간으로 빌드되어 적용되는 기능을 사용할 수 있음

추가 정보

처음에는 개발 편의를 위해 프로젝트 파일을 윈도우 환경에 저장하고 k8s pod 에서 프로젝트 파일을 /run/desktop/mnt/host/c/~~~ 형태로 윈도우의 파일시스템에서 로드하도록 했었는데, 이런식으로 구성하니 I/O 속도가 사용할 수 없을 정도로 매우 저하되어 프로젝트 빌드하는데 한세월이 걸렸음

따라서 WSL2 기반 환경에 올라가있는 k8s 에서 프로젝트 파일을 실시간으로 사용해야 한다면 프로젝트 파일을 무조건 WSL 환경 안에 저장해야 함

위 hostPath 설정 정보는 WSL2 기본 설정일 경우를 가정하고 작성됨

https://learn.microsoft.com/ko-kr/windows/wsl/wsl-config#automount-settings

Visits: 20

댓글 남기기