Kubernetes 1.20.5实验记录–数据卷和数据持久化(NFS PV&PVC)
1.1 NFS PV&PVC
1.1.1 静态方式
1、配置NFS服务器:
略
2、Worker节点安装nfs-utils:
yum -y install nfs-utils
3、创建PV:
文件nfs-static-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-static-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
kubectl apply -f nfs-static-pv.yaml
4、查看PV状态:
kubectl get pv
5、创建PVC:
文件nfs-static-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-static-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
kubectl apply -f nfs-static-pvc.yaml
6、查看PVC状态:
kubectl get pvc
7、查看PV状态:
kubectl get pv
8、创建Pod:
文件nfs-static-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nfs-static-pod
spec:
containers:
- name: nfs-static-pod
image: busybox
args:
- /bin/sh
- -c
- sleep 30000
volumeMounts:
- mountPath: /data
name: data
volumes:
- name: data
persistentVolumeClaim:
claimName: nfs-static-pvc
kubectl apply -f nfs-static-pod.yaml
9、查看Pod状态:
kubectl get pod -o wide
10、查看Pod内挂载情况:
kubectl exec -it nfs-static-pod -- sh
11、删除Pod:
kubectl delete -f nfs-static-pod.yaml
12、删除PVC:
kubectl delete -f nfs-static-pvc.yaml
或
kubectl delete pvc nfs-static-pvc
13、删除PV:
kubectl delete -f nfs-static-pv.yaml
或
kubectl delete pvc nfs-static-pv
1.1.1 动态方式
1、配置NFS服务器:
略
2、禁用selfLink(1.20.x版本需要配置)
修改/etc/kubernetes/manifests/kube-apiserver.yaml
添加- --feature-gates=RemoveSelfLink=false
2、Worker节点安装nfs-utils:
yum -y install nfs-utils
3、创建nfs provisioner和其他额外配置:
文件夹RBAC文件:
serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: nfs-provisioner
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
roleRef:
kind: Role
name: nfs-provisioner
apiGroup: rbac.authorization.k8s.io
clusterrole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: nfs-provisioner
rules:
- 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"]
clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-provisioner
apiGroup: rbac.authorization.k8s.io
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-provisioner
spec:
replicas: 1
selector:
matchLabels:
app: nfs-provisioner
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-provisioner
template:
metadata:
labels:
app: nfs-provisioner
spec:
serviceAccountName: nfs-provisioner
containers:
- name: nfs-provisioner
image: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfsstor
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: nfs.com/fs
- name: NFS_SERVER
value: 192.168.0.100
- name: NFS_PATH
value: /nfsstor
volumes:
- name: nfsstor
nfs:
server: 192.168.0.100
path: /nfsstor
kubectl apply -f RBAC/
4、查看Pod状态:
kubectl get pod -o wide
5、创建StorageClass:
文件nfs-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storageclass
provisioner: nfs.com/fs
parameters:
archiveOnDelete: "false"
kubectl apply -f nfs-storageclass.yaml
6、查看StorageClass:
kubectl get storageclass
7、创建PVC:
文件nfs-dynamic-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-dynamic-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: nfs-storageclass
resources:
requests:
storage: 1Gi
kubectl apply -f nfs-dynamic-pvc.yaml
8、查看PVC状态:
kubectl get pvc
9、查看PV状态:
kubectl get pv
10、创建Pod:
文件nfs-dynamic-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nfs-dynamic-pod
spec:
containers:
- name: nfs-dynamic-pod
image: busybox
args:
- /bin/sh
- -c
- sleep 3000
volumeMounts:
- name: data
mountPath: /data
readOnly: false
volumes:
- name: data
persistentVolumeClaim:
claimName: nfs-dynamic-pvc
kubectl apply -f nfs-dynamic-pod.yaml
11、查看Pod状态:
kubectl get pod -o wide
12、查看Pod内挂载情况:
kubectl exec -it nfs-dynamic-pod -- sh
13、删除Pod:
kubectl delete -f nfs-dynamic-pod.yaml
14、删除PVC:
kubectl delete -f nfs-dynamic-pvc.yaml
或
kubectl delete pvc nfs-dynamic-pvc
15、删除StorageClass:
kubectl delete -f nfs-storageclass.yaml
16、删除nfs provisioner和其他额外配置:
kubectl delete -f RBAC/