K8S后端存储:NFS
大家都知道,NFS是一种基于网络的文件系统协议,允许在不同的机器之间共享文件系统资源。在K8S中,可以使用NFS作为后端存储,以提供持久化存储和共享存储卷。但是否适合在生产环境使用NFS作为后端存储,这取决于具体的应用程序和使用场景。如果应用程序对性能和可靠性要求比较高,可能需要选择其他更适合的存储方案,比如ceph。如果只是在测试或者开发环境中,我觉得使用NFS可以更方便地实现共享存储卷,提高测试或者开发的效率。
搭建NFS
代码语言:txt复制# step1 安装yum install nfs-utils -y
yum install nfs-utils -y
# step2 创建NFS共享目录
mkdir /data/nfs_k8s_storage_share
# step3 配置NFS共享,编辑/etc/exports文件,将要共享的目录添加到文件中
/data/nfs_k8s_storage_share *(rw,sync,no_root_squash) # 这将允许任何客户端以读写模式访问共享目录。
# step4 重新加载NFS配置
exportfs -r
# step5 启动NFS服务
systemctl start nfs-server
systemctl enable nfs-server
# step6 确认NFS服务正在运行
systemctl status nfs-server
# step7 注意防火墙,可以关闭或添加允许策略
firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --reload
# step8 验证是否共享成功
showmount -e 192.168.11.254
k8s工作节点安装nfs-utils
安装完即可,无需做任何配置
代码语言:txt复制yum install nfs-utils -y
“
具体在哪个工作节点安装得看在下面的deployment.yaml中拉起的NFS客户端配置程序pod跑在哪个节点上,可以让该pod调度到指定的节点,那么该节点就要安装好nfs-utils。如果不干涉调度行为,或者考虑到后续要跑多个副本就可以将全部工作节点都安装好nfs-utils。
”
在k8s master节点上安装nfs动态供给插件
仓库: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner
相关yaml下载链接: https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/rbac.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/deployment.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/class.yaml
- rbac.yaml:授权访问ApiServer
- deployment.yaml:部署插件,部署之前要修改里面的指向的nfs服务器地址和共享目录
- class.yaml:创建存储类
开始部署插件
- 部署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
代码语言:txt复制kubectl create -f rbac.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: tantianran/nfs-subdir-external-provisioner:v4.0.1 # 使用到镜像已经转存到我的仓库
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.11.254 #此处修改成nfs服务器的ip地址
- name: NFS_PATH
value: /data/nfs_k8s_storage_share # 此处修改成nfs的共享目录
volumes:
- name: nfs-client-root
nfs:
server: 192.168.11.254 #此处修改成nfs服务器的ip地址
path: /data/nfs_k8s_storage_share # 此处修改成nfs的共享目录
代码语言:txt复制kubectl create -f deployment.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"
代码语言:txt复制kubectl create -f class.yaml
- 查看
# 查看授权
kubectl get sa
# 查看存储类
kubectl get sc
使用
- 创建一个PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: nfs-client
在这里,PVC名称为my-pvc,请求5GB的存储空间,并将存储类设置为nfs-client
- 查看pvc
# pvc已创建
[root@k8s-a-master nfs-storage-yaml]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
my-pvc Bound pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 5Gi RWO nfs-client 2m6s
# 对应的pv已经自动创建
[root@k8s-a-master nfs-storage-yaml]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 5Gi RWO Delete Bound default/my-pvc nfs-client 2m7s
[root@k8s-a-master nfs-storage-yaml]#
- 创建一个应用pod
apiVersion: v1
kind: Pod
metadata:
name: test-nginx-pod
spec:
containers:
- name: test-nginx-container
image: nginx
volumeMounts:
- name: storage
mountPath: /data
volumes:
- name: storage
persistentVolumeClaim:
claimName: my-pvc
- 查看pod是否挂载成功
[root@k8s-a-master nfs-storage-yaml]# kubectl exec -it test-nginx-pod -- df -h
... 3.9G 0 3.9G 0% /sys/fs/cgroup
192.168.11.254:/data/nfs_k8s_storage_share/default-my-pvc-pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61 500G 1.8G 499G 1% /data
..