在 Kubernetes 集群中使用 NFS

在 Kubernetes 集群中使用 NFS

总结:我在 Kubernetes pod 内安装 NFS 时遇到问题

当我尝试/exports从 NFS 服务挂载时,出现与写保护相关的错误,当(rw)/etc/exports

首先,我创建了一个 GCP 磁盘,将其挂载为 PV gcloud compute disks create --size=50GB --zone=us-central1-a nfs-disk

然后我kubectl apply -f进行了以下两个配置。

NFS_部署.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nfs-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-server
  template:
    metadata:
      labels:
        app: nfs-server
    spec:
      containers:
      - name: nfs-server
        image: gcr.io/google_containers/volume-nfs:0.8
        ports:
          - name: nfs
            containerPort: 2049
          - name: mountd
            containerPort: 20048
          - name: rpcbind
            containerPort: 111
        securityContext:
          privileged: true
        volumeMounts:
          - mountPath: /exports
            name: nfs-disk
      volumes:
        - name: nfs-disk
          gcePersistentDisk:
            pdName: nfs-disk
            fsType: ext4

NFS_服务.yaml

apiVersion: v1
kind: Service
metadata:
  name: nfs-service
spec:
  clusterIP: 10.27.254.55
  ports:
    - name: nfs
      port: 2049
    - name: mountd
      port: 20048
    - name: rpcbind
      port: 111
  selector:
    app: nfs-server

/etc/exports这会在 nfs-server 容器中生成以下内容

[root@nfs-server-85546b8c7b-jlzc5 /]# cat /etc/exports
/exports *(rw,fsid=0,insecure,no_root_squash)
/ *(rw,fsid=0,insecure,no_root_squash)

之后,我运行一个临时容器,仅使用以下命令进行测试 kubectl run --rm -it --image=markeijsermans/debug:kitchen-sink debug /bin/bash * 正在使用的镜像只是我找到的故障排除镜像。我尝试的每个镜像都存在完全相同的问题,包括 Ubuntu 和 Fedora

当我尝试在调试容器内(在同一个集群中)挂载 nfs 时,出现以下错误

(22:54 debug-7dcc5cd59f-496fd:/) mkdir /test 
(22:54 debug-7dcc5cd59f-496fd:/) chmod -R 777 /test/
(22:54 debug-7dcc5cd59f-496fd:/) mount -t nfs nfs-service.default.svc.cluster.local:/exports /test/
mount: nfs-service.default.svc.cluster.local:/exports is write-protected, mounting read-only
mount: cannot mount nfs-service.default.svc.cluster.local:/exports read-only
(32 22:54 debug-7dcc5cd59f-496fd:/) mount -t nfs 10.27.254.55:/exports /test/                                 
mount: 10.27.254.55:/exports is write-protected, mounting read-only
mount: cannot mount 10.27.254.55:/exports read-only
(32 22:55 debug-7dcc5cd59f-496fd:/) mount -t nfs nfs-service:/exports /test/                                  
mount: nfs-service:/exports is write-protected, mounting read-only
mount: cannot mount nfs-service:/exports read-only
(32 22:55 debug-7dcc5cd59f-496fd:/) 

编辑:我应该在这里提一下,如果我能从本地主机成功挂载 /exports kubectl exec -it nfs-server-xxxxxxx /bin/bash

^^^^这就是我正在尝试解决的问题

奇怪的是,我能够将 nfs 服务器用作 PV 和 PVC。为了演示,以下 yaml 进行了部署,并且可以成功使用

NFS_Volume.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: nfs-service.default.svc.cluster.local
    path: "/"

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 10Gi

然后可以使用以下 ReplicationController 来验证和测试这一点

_NFS_测试_PV.yaml

# This mounts the nfs volume claim into /mnt and continuously
# overwrites /mnt/index.html with the time and hostname of the pod.

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-busybox
spec:
  replicas: 2
  selector:
    name: nfs-busybox
  template:
    metadata:
      labels:
        name: nfs-busybox
    spec:
      containers:
      - image: busybox
        command:
          - sh
          - -c
          - 'while true; do date > /mnt/index.html; hostname >> /mnt/index.html; sleep $(($RANDOM % 5 + 5)); done'
        imagePullPolicy: IfNotPresent
        name: busybox
        volumeMounts:
          - name: nfs
            mountPath: "/mnt"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: nfs

相关内容