2023-07-21 08:42:59 +08:00
|
|
|
|
[#]: subject: "How to Setup Dynamic NFS Provisioning in Kubernetes Cluster"
|
|
|
|
|
[#]: via: "https://www.linuxtechi.com/dynamic-nfs-provisioning-kubernetes/"
|
|
|
|
|
[#]: author: "Pradeep Kumar https://www.linuxtechi.com/author/pradeep/"
|
|
|
|
|
[#]: collector: "lkxed"
|
|
|
|
|
[#]: translator: "geekpi"
|
2023-07-28 22:33:49 +08:00
|
|
|
|
[#]: reviewer: "wxy"
|
|
|
|
|
[#]: publisher: "wxy"
|
|
|
|
|
[#]: url: "https://linux.cn/article-16043-1.html"
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
如何在 Kubernetes 集群中设置动态 NFS 配置
|
|
|
|
|
======
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
![][0]
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
> 在这篇文章中,我们将向你展示如何在 Kubernetes(k8s)集群中设置动态 NFS 配置。
|
|
|
|
|
|
|
|
|
|
Kubernetes 中的动态 NFS 存储配置允许你按需自动为 Kubernetes 应用配置和管理 NFS(网络文件系统)卷。它允许创建持久卷(PV)和持久卷声明(PVC),而无需手动干预或预配置存储。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
NFS 配置程序负责动态创建 PV 并将其绑定到 PVC。它与 NFS 服务器交互,为每个 PVC 创建目录或卷。
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
### 先决条件
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
- 预装 Kubernetes 集群
|
|
|
|
|
- 具有 Kubernetes 集群管理员权限的普通用户
|
|
|
|
|
- 互联网连接
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
事不宜迟,让我们深入探讨步骤:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
### 步骤 1、准备 NFS 服务器
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
就我而言,我将在 Kubernetes 主节点(Ubuntu 22.04)上安装 NFS 服务器。登录主节点并运行以下命令:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo apt update
|
|
|
|
|
$ sudo apt install nfs-kernel-server -y
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
创建以下文件夹并使用 NFS 共享它:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo mkdir /opt/dynamic-storage
|
|
|
|
|
$ sudo chown -R nobody:nogroup /opt/dynamic-storage
|
|
|
|
|
$ sudo chmod 777 /opt/dynamic-storage
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
在 `/etc/exports` 文件中添加以下条目:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo vi /etc/exports
|
|
|
|
|
/opt/dynamic-storage 192.168.1.0/24(rw,sync,no_subtree_check)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
保存并关闭文件。
|
|
|
|
|
|
|
|
|
|
注意:不要忘记更改导出文件中适合你的部署的网络。
|
|
|
|
|
|
|
|
|
|
要使上述更改生效,请运行:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo exportfs -a
|
|
|
|
|
$ sudo systemctl restart nfs-kernel-server
|
|
|
|
|
$ sudo systemctl status nfs-kernel-server
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![NFS-Service-Status-Kubernetes-Master-Ubuntu][1]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
在工作节点上,使用以下 `apt` 命令安装 `nfs-common` 包。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ sudo apt install nfs-common -y
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
### 步骤 2、安装和配置 NFS 客户端配置程序
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
NFS 子目录外部配置程序在 Kubernetes 集群中部署 NFS 客户端配置程序。配置程序负责动态创建和管理由 NFS 存储支持的持久卷(PV)和持久卷声明(PVC)。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
因此,要安装 NFS 子目录外部配置程序,首先使用以下命令集安装 `helm`:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
|
|
|
|
|
$ chmod 700 get_helm.sh
|
|
|
|
|
$ ./get_helm.sh
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
运行以下命令来启用 `helm` 仓库:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
使用以下 `helm` 命令部署配置程序:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ helm install -n nfs-provisioning --create-namespace nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner --set nfs.server=192.168.1.139 --set nfs.path=/opt/dynamic-storage
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![helm-install-nfs-provisioning-kubernetes-cluster][2]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
上面的 `helm` 命令将自动创建 `nfs-provisioning` 命名空间,并安装 NFS 配置程序的容器荚/部署、名称为 `nfs-client` 的存储类,并将创建所需的 rbac。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl get all -n nfs-provisioning
|
|
|
|
|
$ kubectl get sc -n nfs-provisioning
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![kubectl-get-all-nfs-provisioning-kubernetes-cluster][3]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
完美,上面的输出确认了配置程序容器荚和存储类已成功创建。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
### 步骤 3、创建持久卷声明(PVC)
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
让我们创建 PVC 来为你的容器荚或部署请求存储。PVC 将从存储类 `nfs-client` 请求特定数量的存储:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi demo-pvc.yml
|
|
|
|
|
kind: PersistentVolumeClaim
|
|
|
|
|
apiVersion: v1
|
|
|
|
|
metadata:
|
|
|
|
|
name: demo-claim
|
|
|
|
|
namespace: nfs-provisioning
|
|
|
|
|
spec:
|
|
|
|
|
storageClassName: nfs-client
|
|
|
|
|
accessModes:
|
|
|
|
|
- ReadWriteMany
|
|
|
|
|
resources:
|
|
|
|
|
requests:
|
|
|
|
|
storage: 10Mi
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
保存并关闭文件。
|
|
|
|
|
|
|
|
|
|
![PVC-Yaml-Dynamic-NFS-Kubernetes][4]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
运行以下 `kubectl` 命令以使用上面创建的 YML 文件创建 PVC:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl create -f demo-pvc.yml
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
验证 PVC 和 PV 是否创建:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl get pv,pvc -n nfs-provisioning
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![Verify-pv-pvc-dynamic-nfs-kubernetes-cluster][5]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
太好了,上面的输出表明 PV 和 PVC 创建成功。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
### 步骤 4、测试并验证动态 NFS 配置
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
为了测试和验证动态 NFS 配置,请使用以下 YML 文件启动测试容器荚:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ vi test-pod.yml
|
|
|
|
|
kind: Pod
|
|
|
|
|
apiVersion: v1
|
|
|
|
|
metadata:
|
|
|
|
|
name: test-pod
|
|
|
|
|
namespace: nfs-provisioning
|
|
|
|
|
spec:
|
|
|
|
|
containers:
|
|
|
|
|
- name: test-pod
|
|
|
|
|
image: busybox:latest
|
|
|
|
|
command:
|
|
|
|
|
- "/bin/sh"
|
|
|
|
|
args:
|
|
|
|
|
- "-c"
|
|
|
|
|
- "touch /mnt/SUCCESS && sleep 600"
|
|
|
|
|
volumeMounts:
|
|
|
|
|
- name: nfs-pvc
|
|
|
|
|
mountPath: "/mnt"
|
|
|
|
|
restartPolicy: "Never"
|
|
|
|
|
volumes:
|
|
|
|
|
- name: nfs-pvc
|
|
|
|
|
persistentVolumeClaim:
|
|
|
|
|
claimName: demo-claim
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![Pod-Yml-Dynamic-NFS-kubernetes][6]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
使用以下 `kubectl` 命令部署容器荚:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl create -f test-pod.yml
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
验证 `test-pod` 的状态:
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl get pods -n nfs-provisioning
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![Verify-Test-Pod-Using-NFS-Volume-Kubernetes][7]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
登录到容器荚并验证 NFS 卷是否已安装。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl exec -it test-pod -n nfs-provisioning /bin/sh
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![Access-Dynamic-NFS-Inside-Pod-Kubernetes][8]
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
太棒了,上面容器荚的输出确认了动态 NFS 卷已安装且可访问。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
最后删除容器荚和 PVC,查看 PV 是否自动删除。
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ kubectl delete -f test-pod.yml
|
|
|
|
|
$ kubectl delete -f demo-pvc.yml
|
|
|
|
|
$ kubectl get pv,pvc -n nfs-provisioning
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
![Delete-Pod-PVC-Dynamic-NFS][9]
|
|
|
|
|
|
|
|
|
|
这就是这篇文章的全部内容,希望对你有所帮助。请随时在下面的评论部分发表你的疑问和反馈。
|
|
|
|
|
|
2023-07-28 22:33:49 +08:00
|
|
|
|
*(题图:MJ/75dae36f-ff68-4c63-81e8-281e2c239356)*
|
|
|
|
|
|
2023-07-21 08:42:59 +08:00
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.linuxtechi.com/dynamic-nfs-provisioning-kubernetes/
|
|
|
|
|
|
|
|
|
|
作者:[Pradeep Kumar][a]
|
|
|
|
|
选题:[lkxed][b]
|
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2023-07-28 22:33:49 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2023-07-21 08:42:59 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://www.linuxtechi.com/author/pradeep/
|
|
|
|
|
[b]: https://github.com/lkxed/
|
|
|
|
|
[1]: https://www.linuxtechi.com/wp-content/uploads/2023/06/NFS-Service-Status-Kubernetes-Master-Ubuntu.png
|
|
|
|
|
[2]: https://www.linuxtechi.com/wp-content/uploads/2023/06/helm-install-nfs-provisioning-kubernetes-cluster.png
|
|
|
|
|
[3]: https://www.linuxtechi.com/wp-content/uploads/2023/06/kubectl-get-all-nfs-provisioning-kubernetes-cluster.png
|
|
|
|
|
[4]: https://www.linuxtechi.com/wp-content/uploads/2023/06/PVC-Yaml-Dynamic-NFS-Kubernetes.png
|
|
|
|
|
[5]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Verify-pv-pvc-dynamic-nfs-kubernetes-cluster.png
|
|
|
|
|
[6]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Pod-Yml-Dynamic-NFS-kubernetes.png
|
|
|
|
|
[7]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Verify-Test-Pod-Using-NFS-Volume-Kubernetes.png
|
|
|
|
|
[8]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Access-Dynamic-NFS-Inside-Pod-Kubernetes.png
|
|
|
|
|
[9]: https://www.linuxtechi.com/wp-content/uploads/2023/06/Delete-Pod-PVC-Dynamic-NFS.png
|
2023-07-28 22:33:49 +08:00
|
|
|
|
[0]: https://img.linux.net.cn/data/attachment/album/202307/28/222834togtruhoeuh3gtr1.jpg
|