基于NFS的PV动态供给(StorageClass)
基于NFS的PV动态供给(StorageClass)
静态:pod-->pvc-->pv
动态:pod -->pvc-->storageclass
1. Storageclass解决PV手动创建需求
当每次创建 PVC 声明使用存储时,都需要去手动的创建 PV,来满足 PVC 的使用。
可以用一种机制来根据用户声明的存储使用量(PVC)来动态的创建对应的持久化存储卷(PV)。k8s 用 StorageClass 来实现动态创建 持久化存储。
2 配置nfs服务器
/nfs1 *(rw,no_root_squash,no_all_squash,sync)
/nfs2 *(rw,no_root_squash,no_all_squash,sync)
/nfs3 *(rw,no_root_squash,no_all_squash,sync)
3 去官网下载三个文件
这三个文件去网上下载 https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client/deploy
使用脚本批量下载:
for file in class.yaml deployment.yaml rbac.yaml; do wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/$file ; done
其中deployment.yaml需要修改一下挂载的地址和目录
[root@k8s-master01 pv]# cat 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: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.1.210
- name: NFS_PATH
value: /nfs3
volumes:
- name: nfs-client-root
nfs:
server: 192.168.1.210 #nfs地址
path: /nfs3 #要挂载的目录
然后分别去应用这三个文件
kubectl create -f rbac.yaml
kubectl create -f class.yaml
kubectl create -f deployment.yaml
然后去查看pod看nfs是否创建成功
4 去创建nginx,并且去动态获取PV
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30012
name: web
#clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: wangyanglinux/myapp:v1
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html #pod要挂载的目录
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteMany" ]
storageClassName: "managed-nfs-storage" #要跟下载的class.yaml里面的一致
resources:
requests:
storage: 1Gi
然后去应用一下 kubectl apply -f nginx.yaml
5 查看pv和pvc
查看挂载的宿主机目录/nfs3
当你想去更新nginx的内容时,你进去对应的目录去里面新建一个index.html即可