八、Kubernetes学习指南:保姆级实操手册08——持久化存储部署(StorageClass)
一、NFS for kubernetes特性介绍
NFS(Network File System)卷在Kubernetes中的使用提供了一种将远程文件系统挂载到Pods中的方法。
优化建议:
- 在创建NFS卷时,明确规划数据的组织结构和访问模式,确保Pods能够高效地访问所需的数据。
- 监控NFS服务器的性能和健康状况,确保它能够满足Pods的需求。
- 定期备份NFS卷中的数据,以防数据丢失或损坏。
- 考虑使用Kubernetes的存储类(StorageClass)自动化NFS卷的配置和管理。
- 在多租户环境中,使用命名空间(Namespaces)和访问控制策略来隔离和保护NFS卷。
1、NFS安装
本文档使用k8s-master03节点做为NFS的服务器端
# 安装 nfs
yum install nfs-utils -y
# 启动 nfs
systemctl start nfs-server
# 查看 nfs 版本
cat /proc/fs/nfsd/versions
2、创建,设置共享目录
# 创建共享目录
mkdir -p /data/nfs
cd /data/nfs
mkdir rw
mkdir ro
# 设置共享目录 export
vim /etc/exports
/data/nfs/rw 10.255.210.0/24(rw,sync,no_subtree_check,no_root_squash)
/data/nfs/ro 10.255.210.0/24(ro,sync,no_subtree_check,no_root_squash)
3、重新加载,查看显示可挂载路径
# 重新加载
exportfs -f
systemctl reload nfs-server
#查看显示可挂载路径
showmount -e
[root@k8s-master03 ~]# showmount -e 10.255.210.3
Export list for 10.255.210.3:
/data/nfs/ro 10.255.210.0/24
/data/nfs/rw 10.255.210.0/24
4、其他节点安装nfs-utils并挂载测试
yum install nfs-utils.x86_64 -y
mkdir -p /mnt/nfs/rw
mount -t nfs 10.255.210.3:/data/nfs/rw /mnt/nfs/rw
df -TH
5、POD挂载测试
新建一个POD的yaml文件,测试挂载
新建nfs-test-pod.yaml的文件
[root@k8s-master01 ~]# cat nfs-test-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nfs-test-pod
spec:
containers:
- image: nginx:1.7.9
name: test-container
volumeMounts:
- mountPath: /my-nfs-data
name: nfs-test-volume
volumes:
- name: nfs-test-volume
nfs:
server: 10.255.210.3 # 网络存储服务地址
path: /data/nfs/rw # 网络存储路径
readOnly: true # 是否只读
# 部署pod文件
kubectl apply -f nfs-test-pod.yaml
#检查挂载:
查看POD挂载
kubectl exec -it nfs-test-pod /bin/sh df -h
二、StorageClass安装
k8s 中提供了一套自动创建 PV 的机制,就是基于 StorageClass 进行的,通过 StorageClass 可以实现仅仅配置 PVC,然后交由 StorageClass 根据 PVC 的需求动态创建 PV。
1、NFS-Previsioner制备器安装
每个 StorageClass 都有一个制备器(Provisioner),用来决定使用哪个卷插件制备 PV。
- 安装文档 :
GitHub - kubernetes-sigs/nfs-subdir-external-provisioner: Dynamic sub-dir volume provisioner on a remote NFS server.
1.1、安装helm
本文中选择通过helm的方式安装nfs-subdir-external-provisioner
,这种方式相对简单。
# 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
Downloading https://get.helm.sh/helm-v3.14.4-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
1.2、安装nsf-subdir-external-provisioner
方法一:在线安装
$ helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
$ helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner -n kube-system \
--set image.repository=dyrnq/nfs-subdir-external-provisioner \
--set nfs.server=10.255.210.3 \
--set nfs.path=/data/nfs/rw
方法二、离线安装
自行下载nfs-subdir-external-provisioner的安装包
wget "https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/releases/download/nfs-subdir-external-provisioner-4.0.18/nfs-subdir-external-provisioner-4.0.18.tgz"
解压目录
tar -xzf nfs-subdir-external-provisioner-4.0.18.tgz
修改values.yaml文件
vim values.yaml
[root@k8s-master01 nfs-subdir-external-provisioner]# cat values.yaml
replicaCount: 1
strategyType: Recreate
image:
repository: harbor.fq.com/public/nfs-provisioner/nfs-subdir-external-provisioner@sha256:f741e403b3ca161e784163de3ebde9190905fdbf7dfaa463620ab8f16c0f6423
#repository: registry.k8s.io/sig-storage/nfs-subdir-external-provisioner
tag: v4.0.2
pullPolicy: IfNotPresent
---添加NFS配置---
nfs:
server: 10.255.210.3
path: /data/nfs/rw
image:
repository: dyrnq/nfs-subdir-external-provisioner
Helmp安装指定包,和指定文件
helm install nfs-provisioner /root/nfs-subdir-external-provisioner-4.0.18.tgz -n kube-system -f /root/nfs-subdir-external-provisioner/values.yaml --set image.repository=dyrnq/nfs-subdir-external-provisioner --set nfs.server=10.255.210.3 --set nfs.path=/data/nfs/rw
1.3、查看安装的pod
kubectl get pod -n kube-system |grep provisioner
nfs-subdir-external-provisioner-75bfd99f76-6skc8 1/1 Running 0 75m
1.4、测试示例
cat >storageclass-test.yaml <<EOF
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage-1
provisioner: cluster.local/nfs-provisioner-nfs-subdir-external-provisioner
parameters:
# 设置为"false"时删除PVC不会保留数据,"true"则保留数据
archiveOnDelete: "false"
mountOptions:
# 指定NFS版本,这个需要根据NFS Server版本号设置
- nfsvers=4
---
# 创建PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-storage-pvc-1
namespace: default
spec:
storageClassName: nfs-storage-1 #需要与上面创建的storageclass的名称一致
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-sc
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nfs-storage-pod-1
image: nginx:1.7.9
ports:
- containerPort: 80
name: web
volumeMounts:
- mountPath: /tmp/pvc
name: nfs-pvc
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: nfs-storage-pvc-1
EOF
1.5、部署,检查测试示例
部署测试示例
kubectl apply -f storageclass-test.yaml
查看storageclass,pvc,pv
[root@k8s-master01 ~]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client cluster.local/nfs-provisioner-nfs-subdir-external-provisioner Delete Immediate true 25h
nfs-storage-1 cluster.local/nfs-provisioner-nfs-subdir-external-provisioner Delete Immediate false 43m
[root@k8s-master01 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-storage-pvc-1 Bound pvc-f6571669-265c-460b-8cc2-3da1c0ab789c 10Mi RWO nfs-storage-1 44m
[root@k8s-master01 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-f6571669-265c-460b-8cc2-3da1c0ab789c 10Mi RWO Delete Bound default/nfs-storage-pvc-1 nfs-storage-1 44m
查看生成的pod
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-test-pod 1/1 Running 0 4d23h
web-sc-7949d6d5c9-fz8nb 1/1 Running 0 44m
web-sc-7949d6d5c9-m46n7 1/1 Running 0 44m
登进pod并查看挂载路径
[root@k8s-master01 ~]# kubectl exec -it web-sc-7949d6d5c9-fz8nb /bin/sh -- df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg01-lv_root ext4 38G 8.3G 28G 24% /dev/termination-log
10.255.210.3:/data/nfs/rw/default-nfs-storage-pvc-1-pvc-f6571669-265c-460b-8cc2-3da1c0ab789c nfs4 38G 9.2G 27G 26% /tmp/pvc
/dev/mapper/vg01-lv_root ext4 38G 8.3G 28G 24% /etc/resolv.conf