k8s 数据卷NFS共享卷

本文详细介绍了如何在Kubernetes中使用NFS作为数据卷,包括安装配置nfs服务器,创建共享目录,挂载到Pod,并通过示例展示了如何创建一个基于NFS的Deployment。通过步骤操作演示了如何实现Pod共享NFS文件夹,适合K8s开发者参考。

k8s-数据卷NFS共享卷

1. k8s数据卷NFS共享卷

  • NFS数据卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中

  • NFS:是一个主流的文件共享服务器。

  • 安装示例:

    # yum install nfs-utils -y
    # vi /etc/exports
    /ifs/kubernetes *(rw,no_root_squash)
    # mkdir -p /ifs/kubernetes
    # systemctl start nfs
    # systemctl enable nfs

    注:每个Node上都要安装nfs-utils包

  • NFS共享卷架构图image

  • 示例:将网站程序通过NFS数据卷共享,让所有Pod使用

  • 示例代码:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-nfs
    spec:
      selector:
        matchLabels:
          app: nginx-nfs
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx-nfs
        spec:
          containers:
          - name: nginx-nfs
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            nfs:
              server: 10.100.24.85
              path: /ifs/kubernetes

2. 案例

2.1 安装和配置nfs
  • 安装nfs

    [root@k8s-node1 ~]# yum install nfs-utils -y
    [root@k8s-node2 ~]# yum install nfs-utils -y
    [root@k8s-node3 ~]# yum install nfs-utils -y
  • 用node3做本次的nfs共享磁盘

    • 修改nfs配置文件

      [root@k8s-node3 ~]# vi /etc/exports
      [root@k8s-node3 ~]# cat /etc/exports
      /ifs/kubernetes *(rw,no_root_squash)
    • 创建nfs共享目录

      [root@k8s-node3 ~]# mkdir -p /ifs/kubernetes
    • 创建index.html文件

      [root@k8s-node3 ~]# cd /ifs/kubernetes/
      [root@k8s-node3 kubernetes]# vim index.html
      [root@k8s-node3 kubernetes]# cat index.html
      <h1>hello world!</h1>
  • 每个node都启动并设置开机启动

    [root@k8s-node3 ~]# systemctl start nfs      #启动nfs
    [root@k8s-node3 ~]# systemctl enable nfs     # 设置开机启动
    Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
2.2 挂载nfs盘到pod容器
  • 创建代码目录

    [root@k8s-master yaml]# mkdir -p nfs/
    [root@k8s-master yaml]# cd nfs/
  • 编写pod代码

    [root@k8s-master nfs]# vim nfs.yaml
    [root@k8s-master nfs]# cat nfs.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-nfs
    spec:
      selector:
        matchLabels:
          app: nginx-nfs
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx-nfs
        spec:
          containers:
          - name: nginx-nfs
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            nfs:
              server: 10.100.24.85
              path: /ifs/kubernetes
  • 启动服务

    [root@k8s-master nfs]# kubectl apply -f  nfs.yaml
    deployment.apps/web-nfs created
  • 检查服务是否启动

    [root@k8s-master nfs]# kubectl get pods,service -o wide
    NAME                           READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READINESS GATES
    pod/configmap-demo-pod         1/1     Running   0          2d4h    10.244.107.209   k8s-node3   <none>           <none>
    pod/my-hostpath                1/1     Running   0          4h45m   10.244.107.211   k8s-node3   <none>           <none>
    pod/secret-demo-pod            1/1     Running   0          46h     10.244.107.210   k8s-node3   <none>           <none>
    pod/web-nfs-84f8d7bf8d-6mj75   1/1     Running   0          2m42s   10.244.107.212   k8s-node3   <none>           <none>
    pod/web-nfs-84f8d7bf8d-n4tpk   1/1     Running   0          2m42s   10.244.169.144   k8s-node2   <none>           <none>
    pod/web-nfs-84f8d7bf8d-qvd2z   1/1     Running   0          2m42s   10.244.36.84     k8s-node1   <none>           <none>
    
    NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
    service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   13d   <none>
  • 检查内容是否存在

    [root@k8s-master nfs]# curl -I 10.244.107.212
    HTTP/1.1 200 OK
    Server: nginx/1.19.6
    Date: Mon, 28 Dec 2020 12:43:19 GMT
    Content-Type: text/html
    Content-Length: 22
    Last-Modified: Mon, 28 Dec 2020 12:17:10 GMT
    Connection: keep-alive
    ETag: "5fe9ccc6-16"
    Accept-Ranges: bytes
    
    [root@k8s-master nfs]# curl  10.244.107.212
    <h1>hello world!</h1>
### Kubernetes 多个 Pod 共享同一 为了实现多个 Pod 共享同一个存储,在 Kubernetes 中可以采用 PersistentVolume (PV) 和 PersistentVolumeClaim (PVC) 的方式来配置共享存储资源[^1]。 当创建 PV 时,指定合适的访问模式(Access Modes),如 `ReadWriteMany` 或者 `ReadOnlyMany` 来支持多写入或只读的并发访问。对于某些类型的文件系统来说,这允许不同节点上的多个 Pod 同时挂载该并进行数据交互[^2]。 下面是一个简单的例子展示如何设置两个独立部署的应用程序通过 NFS 类型的持久化来进行通信: #### 创建 NFS 持久化声明 PVC ```yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: shared-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi ``` #### 定义第一个应用使用的 Pod 配置 ```yaml apiVersion: v1 kind: Pod metadata: name: app-one spec: volumes: - name: shared-storage persistentVolumeClaim: claimName: shared-pvc containers: - name: container-app-one image: nginx volumeMounts: - mountPath: "/usr/share/nginx/html" name: shared-storage ``` #### 定义第二个应用使用的 Pod 配置 ```yaml apiVersion: v1 kind: Pod metadata: name: app-two spec: volumes: - name: shared-storage persistentVolumeClaim: claimName: shared-pvc containers: - name: container-app-two image: busybox command: ["sh", "-c", "while true; do sleep 30; done"] volumeMounts: - mountPath: "/data" name: shared-storage ``` 上述 YAML 文件定义了一个名为 `shared-pvc` 的持久请求,并将其绑定到两个不同的 Pods 上作为 `/usr/share/nginx/html` 和 `/data` 路径下的目录。这样就实现了这两个容器之间可以通过这个公共路径交换信息的目的[^3]。 需要注意的是,并不是所有的云服务提供商都支持 `ReadWriteMany` 访问模式;因此在实际操作前应当确认所选平台是否满足需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值