Minikube命令行部署服务正常,YAML部署无法访问求助
Let’s break down why your YAML-deployed service isn’t accessible even though the command-line setup works perfectly—there’s a key difference between the two setups that’s likely causing the problem.
1. First, Check the Critical Environment Variable Difference
Your command-line deployment doesn’t set the REACT_APP_API_ROOT environment variable, but your YAML adds it with the value http://localhost:12000. Here’s why that’s a problem:
- Inside a Kubernetes Pod,
localhostrefers to the Pod itself, not your local machine where minikube is running. If your backend API is on your host machine or another Pod, this value will make the frontend app fail to connect to the API, leading to a blank/unreachable page (even though the Pod and Service are running).
2. Verify Pod Health and Logs
Before digging deeper, confirm your Pod is actually running and check its logs for clues:
- Run
kubectl get podsto ensure thetodo-frontPod has aRunningstatus. - Fetch the logs with
kubectl logs <todo-front-pod-name>(replace with your actual Pod name). Look for errors like "failed to connect to API" or similar—this will confirm if the environment variable is breaking the app.
3. Test Direct Pod Access
To rule out Service/minikube issues, forward the Pod port directly to your local machine:
kubectl port-forward <todo-front-pod-name> 3000:3000
Then visit http://localhost:3000 in your browser. If this works but minikube service doesn’t, the problem is with the Service or minikube’s port forwarding. If it still fails, the issue is definitely inside the Pod (almost certainly the REACT_APP_API_ROOT value).
4. Fix the Environment Variable
Adjust the REACT_APP_API_ROOT value based on where your backend API is hosted:
- If your backend is in the same Kubernetes cluster: Use the backend Service’s DNS name (Kubernetes automatically resolves Service names to their ClusterIP). For example, if your backend Service is named
todo-backendand exposes port 12000:env: - name: REACT_APP_API_ROOT value: "http://todo-backend:12000" - If your backend is on your local machine (host): Minikube provides a special hostname
host.minikube.internal(available in minikube 1.10+) that resolves to your host machine’s IP. Use this:env: - name: REACT_APP_API_ROOT value: "http://host.minikube.internal:12000" - If you want to match the command-line behavior: Simply remove the entire
envblock from your YAML to replicate the working command-line deployment.
5. Re-Deploy and Verify
After updating the YAML, re-deploy with:
kubectl apply -f deployment/todo-front.yaml
Wait for the Pod to restart, then check logs again to ensure no API connection errors. Finally, run minikube service todo-front—it should now load correctly.
内容的提问来源于stack exchange,提问作者Riku

