如何让Kubernetes PetSet使用现有的Persistent Volumes?

时间:2016-10-28 06:59:31

标签: kubernetes

作为PetSet定义的一部分,为Kubernetes定义了volumeClainTemplates,以动态生成持久卷声明。例如:

  volumeClaimTemplates:
  - metadata:
      name: datadir
      annotations:
        volume.alpha.kubernetes.io/storage-class: anything
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 24Gi

但是,我已经定义了一些持久卷:

#kubectl get pv    
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                 REASON    AGE 
pv-1-rw   24Gi       RWO           Retain          Bound     rnd/pvc-1-rw             1h
pv-2-rw   24Gi       RWO           Retain          Bound     rnd/pvc-2-rw             6d
pv-3-rw   24Gi       RWO           Retain          Bound     rnd/pvc-3-rw             6d
...

我希望Kubernetes从现有的卷中选择持久卷而不是动态创建新卷。

我正在使用Kubernetes 1.4.3。有谁知道怎么做?

3 个答案:

答案 0 :(得分:7)

volumeClaimTemplatesPersistentVolumeClaim的数组。您可以尝试使用selector定义它们,并以某种方式标记现有卷,即:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0001
  labels:
    foo: foo
    bar: bar
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data/pv0001/
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0002
  labels:
    foo: foo
    bar: bar
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data/pv0002/
---
kind: Service
apiVersion: v1
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
    - port: 80
  selector:
    app: nginx
---
kind: PetSet
apiVersion: apps/v1alpha1
metadata:
  name: nginx
spec:
  serviceName: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: gcr.io/google_containers/nginx-slim:0.8
          ports:
            - containerPort: 80
          volumeMounts:
            - name: html
              mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
    - metadata:
        name: html
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 5Gi
        selector:
          matchLabels:
            foo: foo
            bar: bar

当然,卷必须可用于边界。

$ kubectl get pvc html-nginx-0
NAME           STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
html-nginx-0   Bound     pv0002    5Gi        RWO           1m
$ kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM                  REASON    AGE
pv0001    5Gi        RWO           Retain          Available                                    2m
pv0002    5Gi        RWO           Retain          Bound       default/html-nginx-0             2m

答案 1 :(得分:0)

以下是如何使用按标签选择卷

volumeClaimTemplates:
- metadata:
    name: data
spec:
    accessModes: [ "ReadWriteOnce" ]
    selector: 
    matchLabels:
        data-label: database-1

答案 2 :(得分:0)

一个简单的解决方案是手动创建PVC和PV。如果您的PVC被命名为var data = [{ id: 1, name: "Carl", city: "New York" }, { id: 2, name: "Anna", city: "New York" }, { id: 3, name: "Carl", city: "Sydney" } ] var filterValue = ["Carl"]; console.log(data.filter(function(e) { return filterValue.indexOf(e.name) != -1 }))html-nginx-0,则批量声明模板将使用它们而不是创建新的PVC,一切都会好起来的。

您可以使用种类https://kind.sigs.k8s.io/,其中启用了动态卷供应,以获取PV和PVC的Yaml示例。并使PV适应您的存储。