如何在Kubernetes部署中使用卷?

时间:2016-07-28 09:39:48

标签: kubernetes

我想将卷用于具有多个副本的部署。如何定义PersistentVolumeClaim以便为每个副本生成它?目前(见下面的例子)我能够生成一个卷并将其分配给pod。问题是,只生成了一个卷,导致出现此错误消息:

  38m   1m  18  {kubelet worker-1.loc}      Warning FailedMount Unable to mount volumes for pod "solr-1254544937-zblou_default(610b157c-549e-11e6-a624-0238b97cfe8f)": timeout expired waiting for volumes to attach/mount for pod "solr-1254544937-zblou"/"default". list of unattached/unmounted volumes=[datadir]
  38m   1m  18  {kubelet worker-1.loc}      Warning FailedSync  Error syncing pod, skipping: timeout expired waiting for volumes to attach/mount for pod "solr-1254544937-zblou"/"default". list of unattached/unmounted volumes=[datadir]

如何告诉Kubernetes为每个副本生成一个卷?

我正在使用Kubernetes 1.3。

示例:

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: solr-datadir
  annotations:
    volume.alpha.kubernetes.io/storage-class: anything
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
---
apiVersion: extensions/v1beta1
kind: Deployment

metadata:
  name: solr
  labels:
    team: platform
    tier: search
    app: solr

spec:
  revisionHistoryLimit: 3
  replicas: 3

  template:
    metadata:
      name: solr
      labels:
        team: platform
        tier: search
        app: solr

    spec:
      containers:
      - name: solr
        image: solr:6-alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
        resources:
          requests:
            cpu: 512m
            memory: 512Mi
        command: 
        - /bin/bash
        args:
        - -c
        - /opt/solr/bin/solr start -f -z zookeeper:2181
        volumeMounts:
        - mountPath: "/opt/solr/server/solr/mycores"
          name: datadir
      volumes:
      - name: datadir
        persistentVolumeClaim:
          claimName: solr-datadir

生成的播客:

$ kubectl get pods -lapp=solr 
NAME                    READY     STATUS              RESTARTS   AGE
solr-1254544937-chenr   1/1       Running             0          55m
solr-1254544937-gjud0   0/1       ContainerCreating   0          55m
solr-1254544937-zblou   0/1       ContainerCreating   0          55m

生成的卷:

$ kubectl get pv
NAME                                       CAPACITY   ACCESSMODES   STATUS    CLAIM                         REASON    AGE
pvc-3955e8f1-549e-11e6-94be-060ea3314be5   50Gi       RWO           Bound     default/solr-datadir                    57m

生成的声明:

$ kubectl get pvc
NAME                  STATUS    VOLUME                                     CAPACITY   ACCESSMODES   AGE
solr-datadir          Bound     pvc-3955e8f1-549e-11e6-94be-060ea3314be5   0                        57m

1 个答案:

答案 0 :(得分:10)

ReplicaSet将卷视为无状态。如果replicaset pod模板指定的卷只能以读写方式附加一次,则该复制集中的所有pod将使用相同的卷。如果该卷只能一次以读写方式附加到一个节点(如GCE PD),则在第一个pod成功调度并启动后,如果将其调度到另一个节点,则后续的pod实例将无法启动,因为卷无法连接到第二个节点。

您正在寻找的是宠物套装,它可以让您为每个副本生成一个卷。请参阅http://kubernetes.io/docs/user-guide/petset/该功能目前处于Alpha状态,但应解决您的用例问题。

更新:在Kubernetes中,1.5 + PetSet被重命名为StatefulSets。请参阅文档here