k8s使用nfs做provisioner

nfs-client-provisioner 是一个Kubernetes的简易NFS的外部provisioner,本身不提供NFS,需要现有的NFS服务器提供存储
这里的 k8s 版本为 v1.19.4

准备k8s集群

1
2
3
4
5
6
7
8
[[email protected] nfs-client]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 60d v1.19.4
k8s-node01 Ready <none> 60d v1.19.4
k8s-node02 Ready <none> 60d v1.19.4
k8s-node03 Ready <none> 60d v1.19.4
k8s-node04 Ready <none> 60d v1.19.4
# yum install -y nfs-utils #所有k8s节点需要装此包

搭建NFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[[email protected] ~]# mkdir /data/nfs -p
[[email protected] ~]# yum install -y nfs-utils
[[email protected] ~]# vim /etc/exports
/data/nfs 10.62.169.0/24(rw,sync,fsid=0)
[[email protected] ~]# systemctl enable rpcbind.service
[[email protected] ~]# systemctl enable nfs-server.service
[[email protected] ~]# systemctl start rpcbind.service
[[email protected] ~]# systemctl start nfs-server.service
[[email protected] ~]# systemctl status rpcbind.service
[[email protected] ~]# systemctl status nfs-server.service


[[email protected] ~]# showmount -e 10.62.169.145
Export list for 10.62.169.145:
/data/nfs 10.62.169.0/24

创建nfs-provisioner的namespaces

创建用于部署nfs-client-provisioner端的namespaces

1
[[email protected] nfs-client]# kubectl create ns nfs

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[[email protected]]# git clone https://github.com/lijiawang/DevOps.git
[[email protected] ~]# cd DevOps/nfs-client/
[[email protected] nfs-client]# kubectl apply -f rbac.yaml
serviceaccount/nfs-client-provisioner created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
[[email protected] nfs-client]# cat deployment.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
namespace: nfs
spec:
replicas: 1
selector:
matchLabels:
app: nfs-client-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: lijiawang/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 10.62.169.145 # 这里是NFS 服务器的地址
- name: NFS_PATH
value: /data/nfs # NFS 路径
volumes:
- name: nfs-client-root
nfs:
server: 10.62.169.145
path: /data/nfs
[[email protected] nfs-client]# kubectl apply -f deployment.yaml
deployment.apps/nfs-client-provisioner created
[[email protected] nfs-client]# kubectl apply -f class.yaml
storageclass.storage.k8s.io/managed-nfs-storage created

验证nfs-client-provisioner是否安装完成

1
2
3
4
5
6
[[email protected] nfs-client]# kubectl get pod -n nfs
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-64b94d544d-9fsnf 1/1 Running 0 100s
[[email protected] nfs-client]# kubectl get storageclasses
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
managed-nfs-storage fuseim.pri/ifs Delete Immediate false 5h36m

使用storageclasses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[[email protected] nfs-client]# cat test-pod.yml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi
---
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: lijiawang/busybox:1.24
command:
- "/bin/sh"
args:
- "-c"
- "touch /mnt/SUCCESS && exit 0 || exit 1 && sleep 100"
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim
[[email protected] nfs-client]# kubectl apply -f test-pod.yml
persistentvolumeclaim/test-claim created
pod/test-pod created

[[email protected] nfs-client]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
test-claim Bound pvc-73ea81a1-b34d-4b0c-a374-69ca9078d378 1Mi RWX managed-nfs-storage 17s

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!