如何解决Kubernetes Nginx Ingress 使用主机文件返回 502 Bad Gateway
在成功部署应用程序(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"]
可能是什么问题,至少我应该在哪里寻找?
解决方法
我看到两个问题 - 试试这些,如果有帮助,请告诉我:
- 您的 Ingress 清单中缺少
pathType
。必须提供此项,否则 Kubernetes 将无法理解它是精确匹配还是前缀匹配。阅读更多here。 (我看到@Sahadat 正确地指出了这一点,但不知何故删除了他的答案)。 - 您的部署清单中缺少端口号,应该如下所示:
spec:
containers:
- name: posts
image: myuser/posts
ports:
- containerPort: 4000
更新 1:
另一个潜在问题可能是您的主机文件中的此条目 - 127.0.0.1 posts.com
,请删除它并尝试。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。