基于Kind在Kubernetes中搭建本地开发版Kafka并实现持久化卷映射的问题排查
Let’s break down why your ZooKeeper volume isn’t retaining data while Kafka’s works, and walk through fixes step by step.
Most Likely Culprit: Permission Mismatches
Confluent’s cp-zookeeper image runs as a non-root user (usually UID/GID 1000 by default), but the host directory you’re mounting (./tmp/zookeeper-data) probably has root-only permissions. When the ZooKeeper container tries to write to /var/lib/zookeeper/data or /var/lib/zookeeper/log, it gets denied—so the folders are created but stay empty.
Fix Step 1: Adjust Host Directory Permissions
On your local machine, set the correct ownership for the ZooKeeper data directory to match the container’s user:
# Create required subdirectories if they don’t exist mkdir -p ./tmp/zookeeper-data/data ./tmp/zookeeper-data/log # Match ownership to Confluent’s container user (UID/GID 1000) chown -R 1000:1000 ./tmp/zookeeper-data
Fix Step 2: Restart the ZooKeeper Deployment
Force a restart to apply the permission changes:
kubectl rollout restart deployment zookeeper
Additional Troubleshooting Steps
If permissions aren’t the issue, check these:
1. Verify PV/PVC Binding
Make sure your Persistent Volume and Claim are properly bound:
kubectl get pv zookeeper-pv kubectl get pvc zookeeper-pvc
You should see STATUS: Bound for both. If not, double-check that your PV’s storageClassName exactly matches the PVC’s (yours uses zookeeper-local-storage, which is correct for local hostPath volumes).
2. Check ZooKeeper Pod Logs for Errors
Look for permission denied or directory access issues in the logs:
kubectl logs <your-zookeeper-pod-name>
Search for lines like Permission denied or Failed to write to data directory—these will confirm if the container can’t access the volume.
3. Validate Kind ExtraMounts
Ensure the Kind cluster correctly maps your host directory to the worker node:
# Get your Kind worker node name (usually kind-worker) docker ps | grep kind-worker # Exec into the node to inspect the mounted directory docker exec -it <kind-worker-node-name> ls -la /var/lib/zookeeper
You should see the data and log folders with the correct UID/GID (1000:1000) and write permissions.
4. Confirm ZooKeeper Environment Variables
Double-check that the ZOOKEEPER_DATA_DIR and ZOOKEEPER_LOG_DIR env vars point to subdirectories of your mounted volume. Your config looks correct (/var/lib/zookeeper/data and /var/lib/zookeeper/log), but verify the container uses these paths:
kubectl exec -it <your-zookeeper-pod-name> -- echo $ZOOKEEPER_DATA_DIR
Why Kafka Works but ZooKeeper Doesn’t?
Chances are your Kafka host directory (./tmp/kafka-data) already had open permissions, or Kafka’s container uses a user that matches your host’s permissions. The core difference here is aligning the container user with the host directory’s ownership—something Kafka happened to get right automatically, but ZooKeeper didn’t.
内容的提问来源于stack exchange,提问作者groo

