带有Kubernetes入口的502错误网关 [英] 502 Bad Gateway with Kubernetes Ingress

查看:73
本文介绍了带有Kubernetes入口的502错误网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个kubernetes设置,配置如下:

I have a kubernetes setup with the configuration like below:

#---
kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  selector:
    app: my-service
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80



---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-service
    spec:
      containers:
        - name: my-service
          image: my-custom-docker-regisry/my-service:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
      imagePullSecrets:
      - name: regcred

和我的入口:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /myservice
        backend:
          serviceName: myservice
          servicePort: 80

我试图做的是从docker注册表中提取图像并在kubernetes中运行它.我在这里配置了一个部署和一项服务,并通过入口将服务公开给外部.

What I tried to do is pulling the image from my docker registry and run it in the kubernetes. I have configured here one deployment and one service and expose the service to the outside with the ingress.

我的minikube在IP 192.168.99.100下运行,当我尝试访问地址为curl 192.168.99.100:80/myservice的应用程序时,我得到了502 Bad Gateway.

My minikube is running under ip 192.168.99.100 and when I tried to access my application with address: curl 192.168.99.100:80/myservice, I got 502 Bad Gateway.

有人知道它为什么会发生吗,还是我在配置上做错了什么?谢谢你!

Does anyone have an idea why it happended or did I do something wrong with the configuration? Thank you in advanced!

推荐答案

您的入口针对此服务:

     serviceName: myservice
     servicePort: 80

,但是名为myservice的服务公开了端口8080而不是80:

but the service named myservice exposes port 8080 rather than 80:

  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80

您的入口应指向该服务公开的端口之一.

Your ingress should point to one of the ports exposed by the service.

此外,服务本身以端口80为目标,但是部署中的Pod似乎公开了端口8080,而不是80:

Also, the service itself targets port 80, but the pods in your deployment seem to expose port 8080, rather than 80:

  containers:
    - name: my-service
      image: my-custom-docker-regisry/my-service:latest
      imagePullPolicy: Always
      ports:
        - containerPort: 8080

长话短说,看来您可以在service中将porttargetPort交换,以便:

So long story short, looks like you could swap port with targetPort in your service so that:

  • pod暴露端口8080
  • 该服务在服务名称myservice端口80下公开所有Pod的端口8080
  • 入口将nginx配置为将您的流量代理到服务myservice端口80.
  • the pods expose port 8080
  • the service exposes port 8080 of all the pods under service name myservice port 80,
  • the ingress configures nginx to proxy your traffic to service myservice port 80.

这篇关于带有Kubernetes入口的502错误网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆