NFS 서버 구축하고 쿠버네티스 NFS 볼륨 마운트하기 (PV,PVC,NFS)

NFS 서버를 외부에 구축하고 쿠버네티스 환경에서 이를 통해 NFS 볼륨을 만들어 pod를 배포합니다.
jongcloud's avatar
Jul 02, 2024
NFS 서버 구축하고 쿠버네티스 NFS 볼륨 마운트하기 (PV,PVC,NFS)
 
 
본 테스트에서는 아래와 같이 설정합니다.
💡
192.168.1.130 (NFS 서버)
192.168.1.114 (NFS 클라이언트, 마스터노드) 192.168.1.115 (NFS 클라이언트, 워커노드) 192.168.1.116 (NFS 클라이언트, 워커노드) NFS 서버에서 공유할 디렉토리 경로 및 이름 : /mnt/shared
 
 

NFS 서버 세팅

  1. NFS 서버 구축하기
    1. sudo apt-get update
      sudo apt-get install -y nfs-common nfs-kernel-server rpcbind portmap
      root@k8s-masterjw:~# sudo apt-get install -y nfs-common nfs-kernel-server rpcbind portmap Reading package lists... Done Building dependency tree... Done Reading state information... Done Note, selecting 'rpcbind' instead of 'portmap' The following additional packages will be installed: keyutils libnfsidmap1 Suggested packages: watchdog The following NEW packages will be installed: keyutils libnfsidmap1 nfs-common nfs-kernel-server rpcbind 0 upgraded, 5 newly installed, 0 to remove and 126 not upgraded. Need to get 521 kB of archives. After this operation, 1,973 kB of additional disk space will be used. Get:1 http://kr.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libnfsidmap1 amd64 1:2.6.1-1ubuntu1.2 [42.9 kB] Get:2 http://kr.archive.ubuntu.com/ubuntu jammy/main amd64 rpcbind amd64 1.2.6-2build1 [46.6 kB] Get:3 http://kr.archive.ubuntu.com/ubuntu jammy/main amd64 keyutils amd64 1.6.1-2ubuntu3 [50.4 kB] Get:4 http://kr.archive.ubuntu.com/ubuntu jammy-updates/main amd64 nfs-common amd64 1:2.6.1-1ubuntu1.2 [241 kB] Get:5 http://kr.archive.ubuntu.com/ubuntu jammy-updates/main amd64 nfs-kernel-server amd64 1:2.6.1-1ubuntu1.2 [140 kB] Fetched 521 kB in 2s (300 kB/s) Selecting previously unselected package libnfsidmap1:amd64. (Reading database ... 110340 files and directories currently installed.) Preparing to unpack .../libnfsidmap1_1%3a2.6.1-1ubuntu1.2_amd64.deb ... Unpacking libnfsidmap1:amd64 (1:2.6.1-1ubuntu1.2) ... Selecting previously unselected package rpcbind. Preparing to unpack .../rpcbind_1.2.6-2build1_amd64.deb ... Unpacking rpcbind (1.2.6-2build1) ... Selecting previously unselected package keyutils. Preparing to unpack .../keyutils_1.6.1-2ubuntu3_amd64.deb ... Unpacking keyutils (1.6.1-2ubuntu3) ... Selecting previously unselected package nfs-common. Preparing to unpack .../nfs-common_1%3a2.6.1-1ubuntu1.2_amd64.deb ... Unpacking nfs-common (1:2.6.1-1ubuntu1.2) ... Selecting previously unselected package nfs-kernel-server. Preparing to unpack .../nfs-kernel-server_1%3a2.6.1-1ubuntu1.2_amd64.deb ... Unpacking nfs-kernel-server (1:2.6.1-1ubuntu1.2) ... Setting up libnfsidmap1:amd64 (1:2.6.1-1ubuntu1.2) ... Setting up rpcbind (1.2.6-2build1) ... Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service. Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket. Setting up keyutils (1.6.1-2ubuntu3) ... Setting up nfs-common (1:2.6.1-1ubuntu1.2) ... Creating config file /etc/idmapd.conf with new version Creating config file /etc/nfs.conf with new version Adding system user `statd' (UID 115) ... Adding new user `statd' (UID 115) with group `nogroup' ... Not creating home directory `/var/lib/nfs'. Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target. Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target. auth-rpcgss-module.service is a disabled or a static unit, not starting it. nfs-idmapd.service is a disabled or a static unit, not starting it. nfs-utils.service is a disabled or a static unit, not starting it. proc-fs-nfsd.mount is a disabled or a static unit, not starting it. rpc-gssd.service is a disabled or a static unit, not starting it. rpc-statd-notify.service is a disabled or a static unit, not starting it. rpc-statd.service is a disabled or a static unit, not starting it. rpc-svcgssd.service is a disabled or a static unit, not starting it. rpc_pipefs.target is a disabled or a static unit, not starting it. var-lib-nfs-rpc_pipefs.mount is a disabled or a static unit, not starting it. Setting up nfs-kernel-server (1:2.6.1-1ubuntu1.2) ... Created symlink /etc/systemd/system/nfs-client.target.wants/nfs-blkmap.service → /lib/systemd/system/nfs-blkmap.service. Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /lib/systemd/system/nfs-server.service. nfs-mountd.service is a disabled or a static unit, not starting it. nfsdcld.service is a disabled or a static unit, not starting it. Creating config file /etc/exports with new version Creating config file /etc/default/nfs-kernel-server with new version Processing triggers for man-db (2.10.2-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.7) ... Scanning processes... Scanning linux images... Running kernel seems to be up-to-date. No services need to be restarted. No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host.
       
  1. shared 폴더 생성 후 적절한 권한 부여
    1. root@k8s-masterjw:~# cd /mnt root@k8s-masterjw:/mnt# ls root@k8s-masterjw:/mnt# mkdir shared root@k8s-masterjw:/mnt# sudo chmod 777 shared root@k8s-masterjw:/mnt# ls shared
 
  1. /mnt/shared에 접글할 IP정보를 설정
    1. 접근할 IP = 192.168.1.0/24 대역에 있는 모든 IP
      sudo echo '/mnt/shared 192.168.1.0/24(rw,sync,no_subtree_check)' >> /etc/exports
       
 
  1. NFS 서비스 적용 및 재시작
    1. $ sudo exportfs -a $ sudo systemctl restart nfs-kernel-server
 

쿠버네티스 환경에서 NFS pod 만들기

본 테스트에서는 NFS 서버를 구축하고 동일 네트웍에 다른 환경(쿠버네티스)에서 NFS 볼륨을 만들어 Pod를 생성합니다.
 

사전 준비 사항

NFS 볼륨을 만들어 pod를 생성하기 위해서는, 쿠버네티스 환경(NFS 클라이언트)에서는 nfs-common 를 설치해야 합니다.
각 노드마다 apt-get install nfs-common 진행하여 설치를 합니다.
 

PV 생성

apiVersion: v1 kind: PersistentVolume metadata: name: nfs-storage labels: type: nfs spec: capacity: storage: 10Gi accessModes: ["ReadWriteMany"] nfs: server: 192.168.1.130 path: /mnt/shared # NFS server's shared path
 

PVC 생성

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-storage-claim spec: storageClassName: "" accessModes: ["ReadWriteMany"] resources: requests: storage: 4Gi selector: matchExpressions: - key: type operator: In values: - nfs
 

Nginx Pod 생성 (NFS Volume)

간단한 테스트 파일은 아래와 같이 구성해볼 수 있습니다.
apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: nfs-volume mountPath: /usr/share/nginx/html volumes: - name: nfs-volume persistentVolumeClaim: claimName: nfs-storage-claim
 

NFS 확인하기

  1. 192.168.1.130(NFS서버) 의 /mnt/shared 경로에 index.html 파일 생성
    1. notion image
      notion image
 
  1. 192.168.1.114 (쿠버네티스 마스터노드) 에서 kubectl exec pod/nginx-pod -it -- /bin/bash 로 접속 하여 생성한 index.html 파일 확인
    1. notion image
Nginx pod의 /usr/share/nginx/html 경로가 130번 서버의 /mnt/shared 경로와 공유된 것을 확인할 수 있습니다.
 
Share article

jongcloud