K8s StatefulSet中ConfigMap被挂载为文件夹而非文件问题求助
问题根源
你创建ConfigMap时的默认键名和StatefulSet中配置的subPath不匹配,导致K8s无法找到对应配置文件,自动将目标路径创建为目录:
- 使用
kubectl create configmap --from-file命令时如果不显式指定键名,默认会以源文件的文件名作为ConfigMap的data字段键名 - 你创建
server-propertiesConfigMap时使用的源文件是config/server.main.properties,因此该ConfigMap内的实际键名为server.main.properties,但你在StatefulSet的volumeMounts中配置的subPath为server.properties,K8s匹配不到对应键的内容时,就会将/usr/local/confluent/config/server.properties路径初始化为空目录
解决方案
两种方案二选一即可:
方案1:创建ConfigMap时指定匹配的键名
删除原有错误的ConfigMap后,使用如下命令重新创建:
kubectl delete configmap server-properties kubectl create configmap server-properties --from-file=server.properties=config/server.main.properties
该命令会显式指定ConfigMap的键名为server.properties,和你StatefulSet中配置的subPath完全匹配。
方案2:修改StatefulSet的subPath配置
调整StatefulSet中server-properties对应的volumeMounts配置,将subPath改为ConfigMap实际存在的键名:
volumeMounts: - mountPath: /usr/local/confluent/config/server.properties name: server-properties subPath: server.main.properties # 修改为ConfigMap实际的键名
修改后重新apply StatefulSet即可生效。
验证方法
配置调整完成后可以先执行以下命令确认ConfigMap的键名是否符合预期:
kubectl get configmap server-properties -o jsonpath='{.data}'
确认键名和subPath一致后再重建Pod,挂载就会正常生成文件而非目录。
内容的提问来源于stack exchange,提问作者dejanmarich
相关产品推荐
相关产品推荐

