How does Kubernetes track which cloud disk is atta

2019-05-18 10:41发布

问题:

I created a StatefulSet on GKE, and it provisioned a bunch of GCE disks that are attached to the pods that belong to that StatefulSet. Suppose I scale the StatefulSet to 0: the constituent pods are destroyed and the disks are released. When I scale back up, the disks are reattached and mounted inside the correct pods.

My questions are:

  • How does Kubernetes keep track of which GCE disk to reconnect to which StatefulSet pod?
  • Suppose I want to restore a StatefulSet Pod's PV from a snapshot. How can I get Kubernetes to use the disk that was created from the snapshot, instead of old disk?

回答1:

When you scale the StatefulSet to 0 replicas, the pods get destroyed but the persistent volumes and persistent volume claims are kept. The association with the GCE disk is written inside the PersistentVolume object. When you scale the RS up again, pods are assigned to the correct PV and thus get the same volume from GCE.

In order to change the persistent volume - GCE disk association after a snapshot restore, you need to edit the PV object.



回答2:

Kubernetes 1.12 start addressing this issue in a more generalized way with Snapshot / restore functionality for Kubernetes and CSI (Container Storage Interface), introduced as an alpha feature.
This provides standardized APIs design (CRDs) and adds PV snapshot/restore support for CSI volume drivers.

This is no longer specific to GKE.

See the feature request "Snapshot / Restore Volume Support for Kubernetes (CRD + External Controller)" and its associated CSI snapshot design.

The statefulSet aspect was not yet fully addressed in this at the beta level, but will be in the future:

The following are non-goals for the current phase, but will be considered at a later phase.

Goal 5: Provide higher-level management, such as backing up and restoring a pod and statefulSet, and creating a consistent group of snapshots.

See PR for the documentation: "Volume Snapshot and Restore Volume from Snapshot Support"