如何让 Kubernetes 服务使用本地主机在 Web 浏览器上打开我的 Django 应用程序?

How do I get Kubernetes service to open my django application on a web browser using local host?

我一直在尝试让我的 kubernetes 通过我的本地主机在浏览器上启动我的 Web 应用程序。当我尝试打开本地主机时超时,我尝试使用 minikube 服务 --url 但这也不起作用。我所有的部署和服务 pods 都是 运行。我还尝试了端口转发并将类型更改为 nodeport。我已经提供了我的 yaml, docker, and svc code.

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


---
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: 5000
        resources:
          requests:
            cpu: 100m
            memory: 250Mi
          limits:
            memory: "2Gi"
            cpu: "500m"   
# For more information, please refer to https://aka.ms/vscode-docker—python
FROM python:3.8-slim-buster

EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR lapp
COPY . .

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode—docker-python—configure-containers
RUN adduser -u 5678 --disab1ed-password --gecos "" appuser && chown -R appuser /app
USER appuser

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Name:                     mywebsite
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=mywebsite
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.99.161.241
IPs:                      10.99.161.241
Port:                     http 8743/TCP
TargetPort:               5000/TCP
NodePort:                 http 32697/TCP
Endpoints:                172.17.0.3:5000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

这是由于您的容器 运行ning 在端口 8000

但是您的服务正在将流量转发到 5000

apiVersion: v1
kind: Service
metadata:
  name: mywebsite
spec:
  type: LoadBalancer
  selector:
    app: mywebsite
  ports:
  - protocol: TCP
    name: http
    port: 8743
    targetPort: **8000**

部署应该

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**
        resources:
          requests:
            cpu: 100m
            memory: 250Mi
          limits:
            memory: "2Gi"
            cpu: "500m"

您需要更改 SVC

中的 targetPort

containerPortDeployment

否则

改变

EXPOSE 80005000 并命令 运行 5000

上的应用程序
CMD ["python", "manage.py", "runserver", "0.0.0.0:5000"]

不要忘记在上述更改后docker再构建一次。