k8s存储-nfs
一、搭建nfs服务器
CentOS系统
yum install -y nfs-utils rpcbind
Debian系统
apt-get install -y nfs-utils rpcbind
创建nfs目录,设置目录权限
mkdir -p /data/nfs chmod -R 777 /data/nfs
编辑/etc/exports
cat << EOF > /etc/exports /data/nfs *(rw,async) EOF
重启服务并设置开机自启
systemctl restart nfs systemctl enable nfs systemctl restart rpcbind systemctl enable rpcbind
本机验证
showmount -e 127.0.0.1
验证客户机挂载nfs
安装nfs客户端
yum install -y nfs-utils
验证挂载nfs信息,能正常显示挂载信息即成功。
showmount -e nfs服务端地址
二、k8s 自动创建pv,pod使用pvc,pvc绑定pv,pv绑定nfs。
rbac.yaml
apiVersion: v1 kind: ServiceAccount metadata: name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: nfs-client-provisioner-runner rules: - apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["persistentvolumes"] verbs: ["get", "list", "watch", "create", "delete"] - apiGroups: [""] resources: ["persistentvolumeclaims"] verbs: ["get", "list", "watch", "update"] - apiGroups: ["storage.k8s.io"] resources: ["storageclasses"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["events"] verbs: ["create", "update", "patch"] --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: run-nfs-client-provisioner subjects: - kind: ServiceAccount name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default roleRef: kind: ClusterRole name: nfs-client-provisioner-runner apiGroup: rbac.authorization.k8s.io --- kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: leader-locking-nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default rules: - apiGroups: [""] resources: ["endpoints"] verbs: ["get", "list", "watch", "create", "update", "patch"] --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: leader-locking-nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default subjects: - kind: ServiceAccount name: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default roleRef: kind: Role name: leader-locking-nfs-client-provisioner apiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac.yaml
class.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: nfs-client provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME' parameters: archiveOnDelete: "false"
kubectl apply -f class.yaml
deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nfs-client-provisioner labels: app: nfs-client-provisioner # replace with namespace where provisioner is deployed namespace: default spec: replicas: 1 strategy: type: Recreate selector: matchLabels: app: nfs-client-provisioner template: metadata: labels: app: nfs-client-provisioner spec: serviceAccountName: nfs-client-provisioner containers: - name: nfs-client-provisioner image: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2 volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: k8s-sigs.io/nfs-subdir-external-provisioner - name: NFS_SERVER
# nfs服务端地址 value: 192.168.152.10 - name: NFS_PATH
# nfs目录 value: /data/nfs volumes: - name: nfs-client-root nfs:
# nfs服务端地址 server: 192.168.152.10
# nfs目录 path: /data/nfs
kubectl apply -f deployment.yaml
三、验证
创建一个pvc
nginx-static-pvc.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: nginx-static-pvc annotations: volume.beta.kubernetes.io/storage-provisioner: k8s-sigs.io/nfs-subdir-external-provisioner spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi storageClassName: nfs-client
kubectl apply -f nginx-static-pvc.yaml
创建nginx deployment 使用pvc
nginx.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 1 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80 volumeMounts: - mountPath: /usr/share/nginx/html name: html volumes: - name: html persistentVolumeClaim: claimName: nginx-static-pvc
kubectl apply -f nginx.yaml
至此nginx根目录就挂载到nfs服务器上的/data/nfs/目录下,目录名称:命名空间+pvc名称+pv名称。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)