Kubernetes NFS 服务器 Pod 挂载可以使用 Pod IP,但不能使用 Kubernetes 服务。

7

我创建了一个 nfs 服务器作为卷,并将其放入一个 pod 中使用。当创建另一个带卷的 pod 时,卷挂载使用 nfs pod 的 ip 工作。由于此 ip 不保证始终相同,我为我的 nfs pod 添加了一个服务并添加了一个固定的集群 ip。但是,在使用卷挂载启动容器时,它总是会因为以下错误而失败:

无法为 pod "nginx_default(35ecd8ec-a077-11e8-b7bc-0cc47a9aec96)" 挂载卷: 超时等待卷附加或安装 pod "default"/"nginx" 的挂载. 未挂载的卷列表=[nfs-demo]。未连接的卷列表=[nfs-demo nginx-test-account-token-2dpgg]

    apiVersion: v1
    kind: Pod
    metadata:
      name: nfs-server
      labels:
        name: nfs-server
    spec:
      containers:
      - name: nfs-server
        image: my-nfs-server:v1
        args: ["/exports"]
        securityContext:
          privileged: true
    ---
    kind: Service
    apiVersion: v1
    metadata:
      name: nfs-service
    spec:
      selector:
        name: nfs-server
      clusterIP: "10.96.0.3"
      ports:
        - name: nfs
          port: 2049
          protocol: UDP
        - name: mountd
          port: 20048
          protocol: UDP   
        - name: rpcbind
          port: 111
          protocol: UDP
        - name: nfs-tcp
          port: 2049
          protocol: TCP
        - name: mountd-tcp
          port: 20048
          protocol: TCP
        - name: rpcbind-tcp
          port: 111
          protocol: TCP

我的Pod正在尝试挂载服务器:

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - mountPath: "/exports"
          name: nfs-demo
        securityContext:
          privileged: true
      securityContext:
        supplementalGroups: [100003]
      serviceAccountName: nginx-test-account
      volumes:
      - name: nfs-demo
        nfs:
          server: 10.96.0.3
          path: "/exports"
          readOnly: false

我将这个作为我的NFS服务器镜像的基础:

https://github.com/cpuguy83/docker-nfs-server

https://medium.com/@aronasorman/creating-an-nfs-server-within-kubernetes-e6d4d542bbb9

有人知道为什么挂载在Pod IP上可以工作,但挂载在Service IP上却不行吗?

4个回答

2
我找到了一种新的解决方法,您可以将nfs服务器端口设置为固定值,然后通过服务挂载nfs服务器。您可以参考https://wiki.debian.org/SecuringNFS
图片链接如下:enter image description hereenter image description here

0

正如Bal Chua所提到的,您可能没有在nfs-server pod定义中导出nfs端口。

nfs-server-pod.yaml

apiVersion: v1beta1
kind: Pod
id: nfs-server
desiredState:
  manifest:
    version: v1beta1
    id: nfs-server
    containers:
      - name: nfs-server
        image: jsafrane/nfs-data
        privileged: true
        ports:
          - name: nfs
            containerPort: 2049
            protocol: tcp
labels:
  name: nfs-server

nfs-server-service.yaml

id: nfs-server
kind: Service
apiVersion: v1beta1
port: 2049
protocol: tcp
selector:
  name: nfs-server

摘自NFS卷示例页面。


0

尝试删除ClusterIP IP地址(让kube为nfs服务分配一个IP),并在卷挂载定义中使用名称“nfs-service”。确保nginx pod和nfs服务在同一命名空间中。


我尝试了一下,不幸的是遇到了和之前相同的问题。我认为服务配置可能是一个问题? - albrr
你在nfs-server pod中没有公开nfs端口。 - Bal Chua
我也尝试过,但它没有影响任何东西。这对服务有影响吗?因为挂载可以使用 Pod IP 本身,但不能使用服务。 - albrr
你在Kubernetes设置中使用哪个云平台? - d0bry
你能描述一下nfs-service的服务吗?我想查看它的终端点,也就是Pod IP地址。 - Alioua

0

我找到了解决问题的方法:

我的服务缺少端口,而不是pod。为了找到我需要的端口,我打开了一个控制台到我的pod(kubectl exec),并使用“rpcinfo -p”命令列出了服务所需的端口。

这确实解决了连接问题,但只是暂时的。这些端口不是静态的,因此与使用端口IP本身没有区别。我认为可以配置静态端口。

如果有类似问题的人需要进一步阅读:

http://tldp.org/HOWTO/NFS-HOWTO/security.html

https://wiki.debian.org/SecuringNFS

我遇到的第二个问题是:只有当nfs-server pod和挂载它的pod在同一节点上时,挂载才能正常工作。当我更新到Kubernetes版本1.11时,问题得到了解决。
既然我的原始问题已经解决,我认为我的问题已经得到了回答。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接