简体   繁体   English

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

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

After successfully deployed the application (pod looks OK, logs are ok), I created ClusterIP service, then created NGINX Ingress and added host to hosts file.成功部署应用程序后(pod 看起来没问题,日志没问题),我创建了 ClusterIP 服务,然后创建了 NGINX Ingress 并将主机添加到主机文件。 When I try to access posts.com/posts , I get 502 bad gateway error.当我尝试访问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"]

What can be the problem and where at least I should look for?可能是什么问题,至少我应该在哪里寻找?

I see two problems - try these and let me know if it helps or not:我看到两个问题 - 试试这些,让我知道它是否有帮助:

  1. You are missing pathType in your Ingress manifest.您的 Ingress 清单中缺少pathType It is mandatory to provide this otherwise Kubernetes won't understand whether it is a exact match or a prefix match.必须提供此信息,否则 Kubernetes 将无法理解它是完全匹配还是前缀匹配。 Read more over here .这里阅读更多。 (I see that @Sahadat pointed it out rightly but somehow deleted his answer). (我看到@Sahadat 正确地指出了这一点,但不知何故删除了他的答案)。
  2. You are missing port number in your Deployment manifest, it should be like below:您的部署清单中缺少端口号,应该如下所示:
spec:
  containers:
    - name: posts
      image: myuser/posts
      ports:
      - containerPort: 4000

UPDATE 1:更新 1:

One more potential problem could be this entry in your host file - 127.0.0.1 posts.com , delete it and try.另一个潜在问题可能是您的主机文件中的此条目 - 127.0.0.1 posts.com ,删除它并尝试。

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

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