Kubernetes Ingress路径访问容器失败求助(Minikube环境)
Hey there! Let's work through this Ingress problem you're having. Since your NodePort services work fine but the path-based Ingress isn't connecting, we can narrow down the issues to a few common culprits. Here's what to check and fix:
1. Verify Minikube's Ingress Addon is Enabled
Minikube doesn't turn on the Ingress controller by default—this is probably the first thing to check.
- Run this command to see if the ingress addon is active:
minikube addons list - If you see
ingress: disabled, enable it with:minikube addons enable ingress - Wait a minute or two, then confirm the controller pod is running:
Look for a pod named likekubectl get pods -n kube-systemnginx-ingress-controller-xxxxxxwith aRunningstatus.
2. Fix Path Rewriting for Your Services
Most apps like Portainer and Rancher expect to be served from the root path (/), but your Ingress is routing requests to /portainer and /rancher. Without path rewriting, the service will receive requests at /portainer which it doesn't recognize, leading to 404 errors.
Update your Ingress configuration to add rewrite rules using NGINX annotations. Here's the corrected YAML:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: path-based-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 spec: rules: - http: paths: - path: /portainer(/|$)(.*) backend: serviceName: portainer-service servicePort: 80 - path: /rancher(/|$)(.*) backend: serviceName: rancher-service servicePort: 80
- The annotation
nginx.ingress.kubernetes.io/rewrite-target: /$2tells the Ingress controller to strip off the/portaineror/rancherprefix before sending the request to the service. - The path pattern
/portainer(/|$)(.*)matches both/portainerand/portainer/some/subpath, ensuring all related requests get rewritten correctly.
Apply the updated Ingress with:
kubectl apply -f your-ingress-file.yaml
3. Check Ingress Resource Status
Make sure your Ingress is properly configured and linked to your services:
- Run this command to inspect the Ingress details:
kubectl describe ingress path-based-ingress - Look for:
- No errors in the
Eventssection (like "service not found" or "invalid port"). - The
Addressfield shows your Minikube IP (you can get this withminikube ip).
- No errors in the
4. Test the Ingress Directly
After making these changes, test the access with curl or your browser:
curl http://$(minikube ip)/portainer curl http://$(minikube ip)/rancher
You should now see the Portainer and Rancher interfaces load correctly.
If you still run into issues, let me know what errors you're seeing (like 404, connection refused) and we can dig deeper!
内容的提问来源于stack exchange,提问作者tienyulin

