GKE实例中无法查看已挂载Persistent Volume的问题求助
Let's break down what's going wrong here and fix it step by step. I see a few key issues in your StatefulSet configuration that are preventing your GCE disk from showing up when you run df -hT.
First: Your StatefulSet is using an emptyDir instead of your persistent volume
Looking at your StatefulSet YAML, you've defined a volumes section with emptyDir: {} for my-volume. This creates a temporary, ephemeral directory that gets deleted when the pod restarts—this has nothing to do with your GCE disk or the PV/PVC you created. Worse, your volumeMounts are pointing to this emptyDir volume, so your persistent disk isn't being mounted at all.
Second: Your manual PV isn't being used by your PVC (or StatefulSet)
When you created your PVC my-claim, it automatically bound to a dynamically provisioned PV (the ones starting with pvc-...) instead of your manual my-volume PV. That's because your manual PV doesn't specify a storageClassName matching the default one your cluster uses (likely standard), so the PVC opted for dynamic provisioning.
Plus, your StatefulSet is using volumeClaimTemplates which creates separate PVCs for each replica (like my-claim-xxx_0), but your volume mounts aren't referencing these correctly.
Fix Option 1: Use your manually created PV/PVC with the StatefulSet
If you want to stick with the GCE disk you created manually, follow these steps:
Update your manual PV to match the default storage class
Edit your PV to add astorageClassNameso it can bind to your PVC:apiVersion: v1 kind: PersistentVolume metadata: name: my-volume spec: capacity: storage: 200Gi accessModes: - ReadWriteOnce storageClassName: standard # Match your cluster's default storage class gcePersistentDisk: pdName: my-disk fsType: ext4Apply the update:
kubectl apply -f your-pv.yamlDelete and re-create your PVC to bind to the manual PV
First delete the existing PVC that's bound to the dynamic PV:kubectl delete pvc my-claimRe-create your PVC, and it should now bind to
my-volumeinstead of creating a new PV.Fix your StatefulSet configuration
Remove theemptyDirvolume andvolumeClaimTemplates, then reference your existing PVC directly:apiVersion: apps/v1beta2 kind: StatefulSet metadata: name: your-statefulset-name # Fill in your actual name spec: serviceName: your-service-name # Required for StatefulSets replicas: 3 # Adjust to your desired number of replicas template: metadata: labels: app: your-app-label spec: containers: - name: your-container-name # Fill in your actual container name image: your-container-image # Fill in your actual image volumeMounts: - name: my-volume mountPath: /mnt/disks volumes: - name: my-volume persistentVolumeClaim: claimName: my-claim # Reference your existing PVCApply the updated StatefulSet:
kubectl apply -f statefulset.yaml
Fix Option 2: Let the StatefulSet manage volumes automatically (Recommended)
StatefulSets are designed to create unique PVCs for each replica automatically via volumeClaimTemplates. If you don't need the specific manual disk you created, this is the simpler approach:
Delete your manual PV and existing PVCs
kubectl delete pv my-volume kubectl delete pvc my-claim my-claim-xxx_0 my-claim-xxx_1 # Delete all related PVCsFix your StatefulSet configuration
Remove theemptyDirvolume, and ensure yourvolumeMountsreference the name involumeClaimTemplates:apiVersion: apps/v1beta2 kind: StatefulSet metadata: name: your-statefulset-name spec: serviceName: your-service-name replicas: 3 template: metadata: labels: app: your-app-label spec: containers: - name: your-container-name image: your-container-image volumeMounts: - name: my-claim # Matches the name in volumeClaimTemplates mountPath: /mnt/disks volumeClaimTemplates: - metadata: name: my-claim spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 200Gi storageClassName: standard # Use your cluster's default storage classApply the updated StatefulSet:
kubectl apply -f statefulset.yaml
Verify the fix
After applying the corrected StatefulSet, wait for the pods to restart. Then SSH into an instance and run df -hT—you should see your /mnt/disks mount listed with the ext4 filesystem type, pointing to your GCE disk.
内容的提问来源于stack exchange,提问作者user3908406

