目录

K8S学习之基础二十三k8s的持久化存储之nfs

K8S学习之基础二十三:k8s的持久化存储之nfs

K8S持久化存储之nfs

​ 在 Kubernetes (k8s) 中使用 NFS(Network File System)作为存储解决方案是一种常见的方式,特别是在需要共享存储的场景中。以下是关于如何在 Kubernetes 中使用 NFS 存储的详细说明:

1. 准备 NFS 服务器

首先,你需要一个 NFS 服务器来提供共享存储。NFS 服务器可以是一个独立的物理机、虚拟机,或者是一个云服务。

在 Linux 上设置 NFS 服务器
  1. 安装 NFS 服务器软件包

    yum install nfs-utils -y
  2. 创建共享目录

    mkdir -p /data/volumes
    chmod 777 /data/volumes
    echo 'Hello NFS' > /data/volumes/index.html
  3. 配置 NFS 导出

    编辑 /etc/exports 文件,添加以下内容:

    /data/volumes *(rw,sync,no_subtree_check)

    这表示将 /data/volumes 目录共享给所有客户端,并赋予读写权限。

  4. 重启 NFS 服务

    systemctl restart nfs

2. 在 Kubernetes 中使用 NFS 存储

  1. 创建 Pod

    创建一个 nfs-pod.yaml 文件:

    apiVersion: v1
    kind: Pod
    metadata:
      name: nfs-pod
    spec:
      containers:
      - name: nfs-container
        image: 172.16.80.140/nginx/nginx:1.26
    		imagePullPolicy: ifNotPresent
        volumeMounts:
        - name: nfs-volumes
          mountPath: /usr/share/nginx/html
      volumes:
      - name: nfs-volumes
        nfs:
          path: /data/volumes
          server: 172.16.80.131
  2. 应用 Pod

    kubectl apply -f nfs-pod.yaml
    kubectl get pods -owide

https://i-blog.csdnimg.cn/direct/6e90efd6559a4485a8d6a00e9cfddded.png#pic_center

3. 验证 NFS 存储

你可以通过以下步骤验证 NFS 存储是否正常工作:

  1. 查看 Pod 日志

    kubectl logs nfs-pod
  2. 在 NFS 服务器上验证

    在 NFS 服务器上,验证nfs是否生效 /data/volumes/index.html

    curl 10.244.196.133

https://i-blog.csdnimg.cn/direct/3cb405b0ec2e45f4a7c353e1661c5cc3.png#pic_left

通过以上步骤,你可以在 Kubernetes 中成功使用 NFS 作为存储解决方案。NFS 特别适用于需要多个 Pod 共享同一存储的场景,例如运行有状态应用或共享配置文件等。

​ nfs还可以在集群中作为共享存储使用,比如在另外一台节点创建目录,挂载到nfs服务,就可以实现

mkdir /test
mount 172.16.80.131:/data/volumes /test
systemctl restart nfs

https://i-blog.csdnimg.cn/direct/d238d0b481bc4b13bdd3b6193adfe9e4.png#pic_left