为什么 Kubernetes Ingress 返回 502 Bad Gateway 错误

问题描述 投票:0回答:1

我是 Kubernetes 新手,想要为我的集群设置 Ingress,但收到 502 Bad Gateway 错误。 Pod、服务和入口已成功创建,并且没有显示任何错误。当我为不同的项目配置不同的入口服务时,它工作得很好。这是我在 nginx 控制器中遇到的错误:

2023-09-11 19:09:56 2023/09/11 17:09:56 [错误] 2385#2385:*218434 连接到上游时失败(111:连接被拒绝),客户端:192.168.65.4 ,服务器:_,请求:“GET /ticket HTTP/1.1”,上游:“http://10.1.0.252:8000/”,主机:“localhost”

2023-09-11 19:09:56 192.168.65.4 - - [2023年9月11日:17:09:56 +0000] “GET /ticket HTTP/1.1” 502 552 “-” “Mozilla/5.0 (Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML,如 Gecko) Chrome/116.0.0.0 Safari/537.36" 2601 0.000 [default-ticket-application-service-8000] [] 10.1.0.253:8000, 10.1.0.254: 8000, 10.1.0.252:8000 0, 0, 0 0.000, 0.001, 0.000 502, 502, 502 8cc88b8a483dd76b820b7da78260b6fd

入口服务.yml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service-project
  annotations:
    kubernetes.io/ingress.class: 'nginx'
    nginx.ingress.kubernetes.io/use-regex: 'true'
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /ticket
            pathType: Prefix
            backend:
              service:
                name: ticket-application-service
                port:
                  number: 8000

ticket-application-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: ticket-application-service
spec:
  type: ClusterIP
  selector:
    component: ticketApp
  ports:
    - port: 8000
      targetPort: 8000

ticket-application-deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ticket-application-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      component: ticketApp
  template:
    metadata:
      labels:
        component: ticketApp
    spec:
      containers:
        - name: ticket-app
          image: borjanob/ticket_app:55da0f121ba6432da3c5afdf48950a00bc431e29
          ports: 
            - containerPort: 8000

用于在部署中构建映像的 Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY TicketApplication/TicketApplication.csproj .
RUN dotnet restore "./TicketApplication.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "TicketApplication/TicketApplication.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TicketApplication/TicketApplication.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TicketApplication.dll"]

我尝试将端口号更改为 80,但出现相同的错误。

kubernetes nginx kubernetes-ingress
1个回答
0
投票

当我为不同的项目配置不同的入口服务时,它工作得很好。

在我看来,该错误可能是您正在构建的项目中的一些问题。您是否尝试过运行图像(不使用 kubernetes),而只是 docker run image:tag 并尝试对 docker 容器执行简单的卷曲? (例如卷曲本地主机:80)

如果失败,错误可能与您正在构建的镜像有关。

为了进一步证明这一点,您可以运行一个简单的 pod、service 和 ingress(例如 nginx),看看它是否返回相同的 502 错误。

© www.soinside.com 2019 - 2024. All rights reserved.