You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过本地主机使Kubernetes服务在浏览器中打开Django应用?访问超时问题排查

Troubleshooting Your Kubernetes Web App Access Issue

Hey there, let's figure out why your web app isn't loading via localhost—after looking through your configs, I see a clear port mismatch that's almost definitely causing the timeout. Here's how to fix it and verify everything works:

1. Fix the Critical Port Mismatch

Your Dockerfile shows your Python app is configured to listen on port 8000 (from CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]), but your Kubernetes Deployment and Service are targeting port 5000. Kubernetes is sending traffic to port 5000 on your Pod, but there's no service listening there—hence the timeout.

You have two options to fix this; I recommend updating your Kubernetes configs to match the Dockerfile's port:

Updated Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  type: LoadBalancer
  selector:
    app: mywebsite
  ports:
    - protocol: TCP
      name: http
      port: 8743
      targetPort: 8000  # Changed from 5000 to 8000

Updated Deployment YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebsite
spec:
  selector:
    matchLabels:
      app: mywebsite
  template:
    metadata:
      labels:
        app: mywebsite
    spec:
      containers:
        - name: mywebsite
          image: mywebsite
          imagePullPolicy: Never
          ports:
            - containerPort: 8000  # Changed from 5000 to 8000
          resources:
            requests:
              cpu: 100m
              memory: 250Mi
            limits:
              memory: "2Gi"
              cpu: "500m"

2. Apply the Fix & Test Access

Once you've updated the YAML files, apply the changes to your cluster:

kubectl apply -f your-config-file.yaml

Wait for the Pod to restart (check status with kubectl get pods), then test with one of these methods:

Option 1: Port Forwarding

Run this command to forward local port 8743 to the Service's target port:

kubectl port-forward service/mywebsite 8743:8000

Then open your browser and go to http://localhost:8743—your app should load now.

Minikube doesn't expose LoadBalancer services to localhost by default. Run this command to create a tunnel:

minikube tunnel

Leave this terminal open, then you can access your app directly at http://localhost:8743 (since your Service's port is 8743).

Option 3: NodePort

If you switch your Service type to NodePort, use this command to get the accessible URL:

minikube service mywebsite --url

Open the returned URL in your browser.

3. Extra Checks to Ensure Everything's Working

If you still run into issues after fixing the port mismatch, try these:

  • Check if the app is running inside the Pod: Exec into the Pod and curl the local port to confirm the service is up:
    kubectl exec -it $(kubectl get pods -l app=mywebsite -o jsonpath='{.items[0].metadata.name}') -- curl localhost:8000
    
    You should see your app's response if it's running correctly.
  • Inspect Pod logs: Look for any errors in the Pod's startup logs:
    kubectl logs $(kubectl get pods -l app=mywebsite -o jsonpath='{.items[0].metadata.name}')
    
  • Verify Minikube status: Make sure your Minikube cluster is running properly:
    minikube status
    

内容的提问来源于stack exchange,提问作者Julian Romero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:37:45