Kubernetes Init容器在ConfigMap挂载前执行脚本问题求助
Hey there, let's break down your problem step by step. First, looking at your pod event logs, the immediate blocking error has nothing to do with ConfigMap mounting—it's a formatting issue with your container's startup command. Let's fix that first, then tackle the ConfigMap concern.
First off, here's the critical error to address:
错误:启动容器"init-myservice"失败:来自守护进程的错误响应:OCI运行时创建失败:container_linux.go:344:启动容器进程导致"exec: "sh -c 'sleep 60; /app/setup.sh'": stat sh -c 'sleep 60; /app/setup.sh': 没有那个文件或目录":未知
1. Fix the Init Container Command Format
In Kubernetes, the command field expects an array structure where the first element is the executable, followed by its arguments. Right now you're passing the entire command as a single string, so Kubernetes is trying to find an executable file named sh -c 'sleep 60; /app/setup.sh' (which obviously doesn't exist).
Update the command section in your Deployment to use the correct array format:
command: - sh - -c - "sleep 60; /app/setup.sh"
This tells Kubernetes to run the sh executable, with -c as the first argument, followed by your full script command string.
2. Validate ConfigMap & PodPreset Mounting
Once the command issue is fixed, let's ensure your ConfigMap is properly attached to the init container:
Check PodPreset Availability & Configuration
PodPreset isn't enabled by default in all Kubernetes clusters (for example, minikube requires explicit enablement with minikube start --extra-config=apiserver.enable-admission-plugins=PodPreset). Double-check:
- The
selector.matchLabelsin your PodPreset (app: stockai) exactly matches the labels on your pod template (this looks correct in your config) - The
defaultMode: 0777on your ConfigMap ensuressetup.shhas executable permissions (this is set properly)
Test Direct Mounting (Bypass PodPreset)
If you're unsure if PodPreset is working, temporarily add the ConfigMap mount directly to your Deployment's init container to rule out PodPreset-related issues:
initContainers: - name: init-myservice image: alpine command: - sh - -c - "sleep 60; /app/setup.sh" volumeMounts: - name: shared-data mountPath: /pod-data - name: setup mountPath: "/app/setup.sh" subPath: "setup.sh" volumes: - name: shared-data emptyDir: {} - name: setup configMap: name: stock-ai-init-config defaultMode: 0777
This lets you verify if the ConfigMap mounts correctly and the script runs as expected without relying on PodPreset.
3. Extra Notes for Your setup.sh Script
Your script uses git clone git@github.com:...—if this is a private repository, you'll need to mount an SSH key into the container's /root/.ssh directory via a Kubernetes Secret. For public repos, just ensure the container has network access to GitHub.
Step-by-Step Checklist
- Fix the init container's command array format to resolve the "file not found" startup error
- Confirm PodPreset is enabled in your cluster, or use direct ConfigMap mounting in the Deployment
- Verify
setup.shhas executable permissions and can access the target Git repository
内容的提问来源于stack exchange,提问作者user3625941

