2016-10-28 57 views
2

作爲PetSet定義的一部分,volumeClainTemplates是爲Kubernetes定義的,以動態生成持久性體積聲明。例如:如何讓Kubernetes PetSet使用現有的持久卷?

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

volumeClaimTemplates是一個數組PersistentVolumeClaim。您可以嘗試使用他們和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 
+0

我試過這個,它爲我工作k8s v1.4.4。我必須創建一個帶有標籤的PV,以便稍後用於petset中的選擇器。 PVC由petset自動創建。 –

相關問題