gpt4 book ai didi

docker - Kubernetes 部署填充了错误的持久卷

转载 作者:行者123 更新时间:2023-12-02 11:37:20 25 4
gpt4 key购买 nike

我正在尝试创建两个部署,一个用于 Wordpress,另一个用于 MySQL,它们指的是两个不同的持久卷。

有时,在删除和重新创建卷和部署时,MySQL 部署会填充 Wordpress 卷(最终在 wordpress-volume 目录中生成一个数据库)。

当你执行 kubectl get pv --namespace my-namespace 时会更清楚:

mysql-volume       2Gi   RWO  Retain  Bound  flashart-it/wordpress-volume-claim   manual                   1h
wordpress-volume 2Gi RWO Retain Bound flashart-it/mysql-volume-claim manual

.

我很确定设置没问题。请在下面找到 yaml 文件。

持久卷声明 + 持久卷

kind: PersistentVolume
apiVersion: v1
metadata:
namespace: my-namespace
name: mysql-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /path/to/mount/mysql-volume
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespace: my-namespace
name: mysql-volume-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
---
kind: PersistentVolume
apiVersion: v1
metadata:
namespace: my-namespace
name: wordpress-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /path/to/mount/wordpress-volume
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespace: my-namespace
name: wordpress-volume-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi

部署

kind: Deployment
apiVersion: apps/v1
metadata:
name: wordpress
namespace: my-namespace
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
namespace: my-namespace
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:5.0-php7.1-apache
name: wordpress
env:
# ...
ports:
# ...
volumeMounts:
- name: wordpress-volume
mountPath: /var/www/html
volumes:
- name: wordpress-volume
persistentVolumeClaim:
claimName: wordpress-volume-claim
---
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: my-namespace
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
namespace: my-namespace
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
# ...
ports:
# ...
volumeMounts:
- name: mysql-volume
mountPath: /var/lib/mysql
volumes:
- name: mysql-volume
persistentVolumeClaim:
claimName: mysql-volume-claim

最佳答案

这是 Kubernetes 中的预期行为。 PVC 可以绑定(bind)到任何可用的 PV,前提是存储类匹配,访问模式匹配,并且存储大小足够。名称不用于匹配 PVC 和 PV。

您的情况的可能解决方案是使用 label selector在PVC上过滤合格的PV。

首先给PV添加标签(本例:app=mysql)

kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-volume
labels:
app: mysql

然后,在PVC中添加一个标签选择器来过滤PV。

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespace: my-namespace
name: mysql-volume-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
selector:
matchLabels:
app: mysql

关于docker - Kubernetes 部署填充了错误的持久卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com