minikube 的 Kubernetes 入门教程--(四)
有了NFS 持久卷,不必担心 pod 会被调度到哪个节点上,因为 NFS 卷是在所有节点上共享的,无论 pod 被调度到哪个节点,应用程序都能访问其所有数据。并将 pod 的 “当前时间 ”和 “主机名 ”写入 /my-test/shared.txt 文件。本文探讨在 Host 上创建并导出共享NFS挂载,并将它(NFS)作为K8s集群中的持久卷存储。1.创建NFS server并提供共享存储,在
本文探讨在 Host 上创建并导出共享NFS挂载,并将它(NFS)作为K8s集群中的持久卷存储。
在深入这些步骤之前,先了解一下共享NFS。
1.创建NFS server并提供共享存储,在网络上其他主机可以使用分享的存储。
[root@nfs ~]# exportfs -v
/data <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
2.K8s集群中节点(Ubuntu操作系统)上安装nfs-common
sudo apt install nfs-common
3.使用host主机的NFS共享创建 PersistentVolume和PersistentVolumeClaim
PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-volume
spec:
capacity:
storage: 256Mi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
mountOptions:
- hard
- nfsvers=4.1
nfs:
path: /data
server: 192.168.2.27
PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-claim
spec:
resources:
requests:
storage: 128Mi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
我们可以使用 kubernetes get pv,pvc 命令查看已创建的 PersistentVolume 和 PersistentVolumeClaim
4.创建一个Deployment
yaml 文件创建部署信息如下:
调度 2 个 busybox pod
卷挂载到 /my-test
并将 pod 的 “当前时间 ”和 “主机名 ”写入 /my-test/shared.txt 文件。
apiVersion: apps/v1
kind: Deployment
metadata:
name: standard-host-path-nfs-pvc-deployment
namespace: default
labels:
app: standard-host-path-nfs-pvc-deployment
spec:
selector:
matchLabels:
app: mypod
replicas: 2
template:
metadata:
labels:
app: mypod
spec:
containers:
- name: pause
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- 'sleep 60; for i in $(seq 1 5); do host=$(hostname); now=$(date +"%T"); echo "adding time: $now, from $host" >> /my-test/shared.txt; sleep 10; done; sleep infinity'
ports:
- containerPort: 80
volumeMounts:
- name: vola
mountPath: /my-test
volumes:
- name: vola
persistentVolumeClaim:
claimName: nfs-claim
执行以下命令创建部署
andy@hostease:~/vsproject/nfs$ kubectl apply -f standard-host-path-nfs-pvc-deployment.yaml
deployment.apps/standard-host-path-nfs-pvc-deployment created
获取Pod
andy@hostease:~/vsproject/nfs$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9 1/1 Running 0 5m11s 10.244.0.17 minikube <none> <none>
standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh 1/1 Running 0 5m11s 10.244.0.18 minikube <none> <none>
查看/my-test/shared.txt 文件内容
andy@hostease:~/vsproject/nfs$ kubectl exec -it standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9 -- sh
/ # ls my-test/
general.test shared.txt test
/ # cat my-test/shared.txt
adding time: 06:30:18, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:21, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:28, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:31, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:38, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:41, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:48, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:51, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:58, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:31:01, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
回到NFS存储提供Host,查看
[root@nfs /]# cd data/
[root@nfs data]# ls -lh
total 4.0K
-rw-r--r--. 1 root root 0 Jul 24 15:17 general.test
-rw-r--r--. 1 root root 830 Jul 31 14:31 shared.txt
-rw-r--r--. 1 root root 0 Jul 24 16:21 test
[root@nfs data]# cat shared.txt
adding time: 06:30:18, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:21, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:28, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:31, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:38, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:41, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:48, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:30:51, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
adding time: 06:30:58, from standard-host-path-nfs-pvc-deployment-696c77df74-tfjvh
adding time: 06:31:01, from standard-host-path-nfs-pvc-deployment-696c77df74-6hvz9
有了NFS 持久卷,不必担心 pod 会被调度到哪个节点上,因为 NFS 卷是在所有节点上共享的,无论 pod 被调度到哪个节点,应用程序都能访问其所有数据。
如果你觉得有用,别忘了给我一个赞或分享我的文章!
更多推荐
所有评论(0)