502 Kubernetes Ingress 网关错误

2024-04-17

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

#---
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 中运行它。我在这里配置了一项部署和一项服务,并将服务通过入口暴露给外部。

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

有谁知道为什么会发生或者我的配置做错了什么?提前谢谢您!


您的入口针对此服务:

     serviceName: myservice
     servicePort: 80

但名为的服务myservice暴露端口8080而不是80:

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

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

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

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

长话短说,看来你可以交换port with targetPort在你的service以便:

  • Pod 暴露端口8080
  • 该服务公开端口8080服务名称下的所有 Podmyservice port 80,
  • 入口配置 nginx 将您的流量代理到服务myservice port 80.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

502 Kubernetes Ingress 网关错误 的相关文章

随机推荐