繁体   English   中英

Kubernetes Nginx 入口返回 502 Bad Gateway 使用主机文件

[英]Kubernetes Nginx Ingress returning 502 Bad Gateway using hosts file

成功部署应用程序后(pod 看起来没问题,日志没问题),我创建了 ClusterIP 服务,然后创建了 NGINX Ingress 并将主机添加到主机文件。 当我尝试访问posts.com/posts时,出现 502 bad gateway 错误。

ingress-srv.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: posts.com
    http:
      paths:
      - path: /posts
        pathType: Prefix
        backend:
          service:
            name: posts-clusterip-srv
            port:
              number: 4000

posts-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: posts-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: posts
  template:
    metadata:
      labels:
        app: posts
    spec:
      containers:
        - name: posts
          image: myUser/posts
          resources:
            limits:
              memory: 512Mi
              cpu: "1"
            requests:
              memory: 256Mi
              cpu: "0.2"
          ports:
            - containerPort: 4000
---
apiVersion: v1
kind: Service
metadata:
  name: posts-clusterip-srv
spec:
  selector:
    app: posts
  ports:
    - name: posts
      protocol: TCP
      port: 4000
      targetPort: 4000
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1   posts.com
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

Dockerfile

FROM node:alpine

WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./

CMD ["npm", "start"]

可能是什么问题,至少我应该在哪里寻找?

我看到两个问题 - 试试这些,让我知道它是否有帮助:

  1. 您的 Ingress 清单中缺少pathType 必须提供此信息,否则 Kubernetes 将无法理解它是完全匹配还是前缀匹配。 这里阅读更多。 (我看到@Sahadat 正确地指出了这一点,但不知何故删除了他的答案)。
  2. 您的部署清单中缺少端口号,应该如下所示:
spec:
  containers:
    - name: posts
      image: myuser/posts
      ports:
      - containerPort: 4000

更新 1:

另一个潜在问题可能是您的主机文件中的此条目 - 127.0.0.1 posts.com ,删除它并尝试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM