持久卷声明和持久卷 yaml 文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/datatypo"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-claim
spec:
storageClassName: manual
volumeName: my-volume
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
部署 yaml 文件
apiVersion: v1
kind: Service
metadata:
name: typo3
labels:
app: typo3
spec:
type: NodePort
ports:
- nodePort: 31021
port: 80
targetPort: 80
selector:
app: typo3
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: typo3
spec:
selector:
matchLabels:
app: typo3
replicas: 1
template:
metadata:
labels:
app: typo3
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app
operator: In
values:
- typo3
containers:
- image: image:typo3
name: typo3
imagePullPolicy: Never
ports:
- containerPort: 80
volumeMounts:
- name: my-volume
mountPath: /var/www/html/
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-claim
注意:如果未添加持久卷,则内容显示在 pod 内(在 中var/www/html
)。但是在添加持久卷之后,它不会显示同一文件夹和外部挂载路径中的任何内容/mnt/datatypo
。
这是预期的行为:安装持久卷时,它会覆盖
mountPath
.因此,您有两种选择:
hostPath
到容器中的另一个目录,然后将内容复制到最终目标文件夹。(可以通过容器中的命令来实现)您也可以挂载单个文件,有不同的选项
hostPath types
。请熟悉hostPath 类型。笔记!使用
hostPath
mount 只能用于在本地测试某些功能,在生产系统中这是非常不安全的方法:卷 - hostPath。