Storage

Kubernetes 節點上的 NFS 伺服器?

  • April 26, 2021

我們有一個在裸機上執行的內部 Kubernetes 集群,我可以在集群中的一個節點(工作節點或主節點)上設置 NFS 伺服器嗎?如果是,我需要更改集群中的任何內容嗎?

您可以設置一個pod充當 NFS 伺服器的設備。

Docker Hub cpuguy83/nfs-server上有一個現成的鏡像。

要使用它,您需要創建一個服務來將 NFS 伺服器公開給集群內的 pod:

kind: Service
apiVersion: v1
metadata:
 name: nfs-service
spec:
 selector:
   role: nfs
 ports:
   # Open the ports required by the NFS server
   # Port 2049 for TCP
   - name: tcp-2049
     port: 2049
     protocol: TCP

   # Port 111 for UDP
   - name: udp-111
     port: 111

還有一個pod將執行圖像:

kind: Pod
apiVersion: v1
metadata:
 name: nfs-server-pod
 labels:
   role: nfs
spec:
 containers:
   - name: nfs-server-container
     image: cpuguy83/nfs-server
     securityContext:
       privileged: true
     args:
       # Pass the paths to share to the Docker image
       - /exports

pod使用 NFS 卷的範例:

kind: Pod
apiVersion: v1
metadata:
 name: pod-using-nfs
spec:
 # Add the server as an NFS volume for the pod
 volumes:
   - name: nfs-volume
     nfs: 
       # URL for the NFS server
       server: 10.108.211.244 # Change this!
       path: /

 # In this container, we'll mount the NFS volume
 # and write the date to a file inside it.
 containers:
   - name: app
     image: alpine

     # Mount the NFS volume in the container
     volumeMounts:
       - name: nfs-volume
         mountPath: /var/nfs

     # Write to a file inside our NFS
     command: ["/bin/sh"]
     args: ["-c", "while true; do date >> /var/nfs/dates.txt; sleep 5; done"]

引用自:https://serverfault.com/questions/987935