部署 NFS 服务器
此处忽略, 参考上文, 建议直接使用云服务器提供商提供的文件存储即可
部署完成后的NFS服务器信息如下:
NFS服务器地址: 10.0.2.222
NFS服务器数据存储目录: /k8s
1 创建 ServiceAccount
现在的 Kubernetes 集群大部分是基于 RBAC 的权限控制,所以创建一个一定权限的 ServiceAccount 与后面要创建的 “NFS Provisioner” 绑定,赋予一定的权限。
/etc/kubernetes/conf/nfs/nfs-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
namespace: default # 替换成你要部署的 Namespace
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
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"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
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
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
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
kubectl apply -f nfs-rbac.yaml
2. 部署 NFS-Subdir-External-Provisioner
创建 NFS Provisioner 部署文件,这里将其部署到 “default” Namespace 中。
/etc/kubernetes/conf/nfs/nfs-provisioner-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
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: gcr.io/k8s-staging-sig-storage/nfs-subdir-external-provisioner:v4.0.0
image: registry.cn-beijing.aliyuncs.com/xngczl/nfs-subdir-external-provisione:v4.0.0
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME ## Provisioner的名称,以后设置的storageclass要和这个保持一致
value: nfs-client
- name: NFS_SERVER ## NFS服务器地址,需和valumes参数中配置的保持一致
value: 10.0.2.222
- name: NFS_PATH ## NFS服务器数据存储目录,需和valumes参数中配置的保持一致
value: /k8s
volumes:
- name: nfs-client-root
nfs:
server: 10.0.2.222 ## NFS服务器地址
path: /k8s ## NFS服务器数据存储目录
创建 NFS Provisioner:
kubectl apply -f nfs-provisioner-deploy.yaml
3、创建 NFS SotageClass
创建一个 StoageClass,声明 NFS 动态卷提供者名称为 “nfs-storage”。
/etc/kubernetes/conf/nfs/nfs-provisioner-deploy.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "false" ## 是否设置为默认的storageclass
provisioner: nfs-client ## 动态卷分配者名称,必须和上面创建的"provisioner"变量中设置的Name一致
parameters:
archiveOnDelete: "true" ## 设置为"false"时删除PVC不会保留数据,"true"则保留数据
mountOptions:
- hard ## 指定为硬挂载方式
- nfsvers=4 ## 指定NFS版本,这个需要根据NFS Server版本号设置
创建 StorageClass:
kubectl apply -f nfs-storage.yaml
4.创建 PVC 和 Pod 进行测试
创建一个测试用的 PVC 并观察是否自动创建是 PV 与其绑定。
/etc/kubernetes/conf/nfs/test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc
spec:
storageClassName: nfs-storage #---需要与上面创建的storageclass的名称一致
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Mi
创建 PVC:
kubectl apply -f test-pvc.yaml
查看 PVC 状态是否与 PV 绑定
利用 Kubectl 命令获取 pvc 资源,查看 STATUS 状态是否为 “Bound”。
root@l2:~# kubectl get pvc test-pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-pvc Bound pvc-edbbeb16-69b7-464c-895e-524a0dabe045 1Mi RWO nfs-storage 50m
5. 创建测试 Pod 并绑定 PVC
创建一个测试用的 Pod,指定存储为上面创建的 PVC,然后创建一个文件在挂载的 PVC 目录中,然后进入 NFS 服务器下查看该文件是否存入其中。
/etc/kubernetes/conf/nfs/test-pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: busybox:latest
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1" #创建一个名称为"SUCCESS"的文件
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-pvc
创建 Pod:
kubectl apply -f test-pod.yaml
5. 进入 NFS 服务器验证是否创建对应文件
root@l2:~# cd /mnt/nfs/k8s/default-test-pvc-pvc-edbbeb16-69b7-464c-895e-524a0dabe045
root@l2:/mnt/nfs/k8s/default-test-pvc-pvc-edbbeb16-69b7-464c-895e-524a0dabe045# ls -l
total 0
-rw-r--r-- 1 root root 0 Oct 27 11:15 SUCCESS