Kubernetes部署会填充错误的持久卷

时间:2019-02-26 07:56:02

标签: docker kubernetes persistent-storage persistent-volume-claims

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

1 个答案:

答案 0 :(得分:3)

这是Kubernetes中的预期行为。只要存储类别已匹配,访问模式已匹配且存储大小足够,PVC即可绑定到任何可用的PV。名称不能用于匹配PVC和PV。

针对您的方案的可能解决方案是在PVC上使用label selector来过滤合格的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