2

我正在尝试创建两个部署,一个用于 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
4

1 回答 1

5

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

您的方案可能的解决方案是在 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
于 2019-02-26T08:20:29.403 回答