1 安装NFS
NFS(Network File System,网络文件系统)是一个共享文件系统。
# 参见以下博客
https://blog.csdn.net/make_progress/article/details/124295978
在master节点上创建目录
# 创建目录
sudo mkdir -p /data/nfsdata
# 添加权限
shdo chmod 777 /data/nfsdata
编辑" /etc/exports"文件
# 编辑文件
sudo vim /etc/exports
# 添加以下内容
/data/nfsdata 192.168.108.*(rw,sync,no_subtree_check)
# 重启服务
sudo service nfs-kernel-server restart
# 查看共享目录
sudo showmount -e 192.168.108.100
# 返回值如下,表示创建成功
Export list for 192.168.108.100:
/data/nfsdata 192.168.108.*
2 使用NFS实现持久化
(1)创建"python-deployment-for-nfs.yaml"文件
# python-deployment-for-nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-deployment-for-nfs
spec:
replicas: 3
selector:
matchLabels:
app: python-for-nfs-pod
template:
metadata:
labels:
app: python-for-nfs-pod
spec:
containers:
- name: python-nfs
image: python:3.8.2
imagePullPolicy: IfNotPresent
command: ['/bin/bash', '-c', '--']
# 启动"python -m http.server 80"服务,“>>”表示向文件中追加数据
args: ['echo "<p> The host is $(hostname) </p>" >> /containerdata/podinfor; python -m http.server 80']
# 设置80端口
ports:
- name: http
containerPort: 80
# 设置挂载点
volumeMounts:
# 此处的名称与nfs有对应关系
- name: python-nfs-data
mountPath: /containerdata
# 配置nfs存储卷
volumes:
# 此处的名称需与spec.containers.volumeMounts.name相同
- name: python-nfs-data
nfs:
path: /data/nfsdata
# NFS服务器节点
server: 192.168.108.100
(2)执行文件
kubectl apply -f python-deployment-for-nfs.yaml
(3)查看结果
# 查看192.168.108.100节点文件
more /data/nfsdata/podinfor
# 返回值
<p> The host is python-deployment-for-nfs-67c4c4f855-n9jlz </p>
<p> The host is python-deployment-for-nfs-67c4c4f855-pr8jr </p>
<p> The host is python-deployment-for-nfs-67c4c4f855-vspm8 </p>
3 使用PV和PVC实现持久化
PV(PersistentVolume)表示持久存储卷;PVC(PersistentVolumeClaim)表示持久化存储卷申请。
3.1 创建PV
(1)创建python-pv.yaml文件
# python-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: python-pv
spec:
# 设置容量
capacity:
storage: 1Gi
# 设置资源的访问策略,访问策略有ReadOnlyMany,ReadWriteOnce,ReadWriteMany
# ReadOnlyMany表示该卷可以被多个节点以只读模式挂载;
# ReadWriteOnce表示该卷可以被单个节点以读写模式挂载;
# ReadWriteMany表示该卷可以被多个节点以读写模式挂载;
accessModes:
- ReadWriteMany
# PV资源的回收策略,回收策略有Retain(保留)、Recycle(自动回收)、Delete(自动删除)
persistentVolumeReclaimPolicy: Recycle
# 设置资源的class名称
# 注意,后面PVC申请资源需要用到此处的名称
storageClassName: pythonpv
# 设置NFS对应的挂载点
nfs:
path: /data/nfsdata
# NFS服务器节点
server: 192.168.108.100
(2)执行python-pv.yaml文件
kubectl apply -f python-pv.yaml
(3)查看PV
# 查看pv
kubectl get pv
# 返回值
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
python-pv 1Gi RWX Recycle Available pythonpv 59s
3.2 创建PVC
(1)创建python-pvc.yaml文件
# python-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
# 注意,后面Deployment申请资源需要用到此处的名称
name: python-pvc
spec:
# 设置资源的访问策略,访问策略有ReadOnlyMany,ReadWriteOnce,ReadWriteMany
# ReadOnlyMany表示该卷可以被多个节点以只读模式挂载;
# ReadWriteOnce表示该卷可以被单个节点以读写模式挂载;
# ReadWriteMany表示该卷可以被多个节点以读写模式挂载;
accessModes:
- ReadWriteMany
# 设置资源的class名称
# 注意,此处的名称必须与PV中的storageClassName相同
storageClassName: pythonpv
# 设置申请的资源大小
resources:
requests:
storage: 100Mi
(2)执行文件
kubectl apply -f python-pvc.yaml
(3)查看PVC
# 查看PVC
kubectl get pvc
# 返回值
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
python-pvc Bound python-pv 1Gi RWX pythonpv 20s
3.3 创建Deployment
(1)创建“python-deployment.yaml”文件
# python-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-deployment
spec:
replicas: 3
selector:
matchLabels:
app: python-pv
template:
metadata:
labels:
app: python-pv
spec:
containers:
- name: python-pv
image: python:3.8.2
imagePullPolicy: IfNotPresent
command: ['/bin/bash', '-c', '--']
# 启动"python -m http.server 80"服务,“>>”表示向文件中追加数据
args: ['echo "<p> The host is $(hostname) </p>" >> /containerdata/podinfor; python -m http.server 80']
# 设置80端口
ports:
- name: http
containerPort: 80
# 设置挂载点
volumeMounts:
# 此处的名称与nfs有对应关系
- name: python-nfs-data
mountPath: /containerdata
# 配置nfs存储卷
volumes:
# 此处的名称需与spec.containers.volumeMounts.name相同
- name: python-nfs-data
# 向PVC申请资源,此处的名称对应python-pvc.yaml文件中的metadata.name
persistentVolumeClaim:
claimName: python-pvc
(2)执行文件
kubectl apply -f python-deployment.yaml
(3)查看结果
# 查看podinfo
more /data/nfsdata/podinfor
# 返回结果如下
<p> The host is python-deployment-for-nfs-67c4c4f855-n9jlz </p>
<p> The host is python-deployment-for-nfs-67c4c4f855-pr8jr </p>
<p> The host is python-deployment-for-nfs-67c4c4f855-vspm8 </p>
# 表明追加成功
<p> The host is python-deployment-64fb987f94-dsdnk </p>
<p> The host is python-deployment-64fb987f94-fmb99 </p>
<p> The host is python-deployment-64fb987f94-84bvs </p>
|