kubernetes 为不同后端上的多个服务搜索正确的入口定义

jv2fixgn  于 2022-11-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(92)

我需要帮助为不同后端中的两个服务设置TLS终止(使用ingress-nginx)。

https://some.url.here/serviceOne/ -> http://hostOne/
https://some.url.here/serviceTwo/ -> http://hostTwo/someServiceNameHere/

我试着用rewrite-target: "/"rewrite-target: "/$1"use-regexp: "true"做这件事,但是连接到http://hostTwo/someServiceNameHere/总是失败。
我的入口定义如下:

kind: Ingress
metadata:
  name: rp-some-url-here
  namespace: reverse-proxy
  annotations:
    kubernetes.io/ingress.class: nginx
    # ??? nginx.ingress.kubernetes.io/rewrite-target: "/$1"
    # ??? nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
    nginx.ingress.kubernetes.io/proxy-ssl-verify: "false"
    nginx.ingress.kubernetes.io/upstream-vhost: "some.url.here"
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    # proxy-buffering is now disabled by default,
    # just having it here for reference
    nginx.ingress.kubernetes.io/proxy-buffering: "off"
spec:
  tls:
  - hosts:
    - some.url.here
    secretName: my-tls-secret
  rules:
  - host: some-url-here
    http:
      paths:
      - backend:
          service:
            name: serviceOne
            port:
              number: 80
        # path: /serviceOne # using this with rewrite-target
        path: /serviceOne/.*
        pathType: Prefix
      - backend:
          service:
            name: serviceTwo
            port:
                number: 8180
        # path: /serviceTwo # using this with rewrite-target
        path: /serviceTwo/.*
        pathType: Prefix

服务定义就像

---
apiVersion: v1
kind: Service
metadata:
  name: service-one
  namespace: reverse-proxy
spec:
  type: ExternalName
  externalName: some.hostname.here
---
apiVersion: v1
kind: Service
metadata:
  name: service-two
  namespace: reverse-proxy
spec:
  type: ExternalName
  externalName: another.hostname.here
gopyfrb3

gopyfrb31#

下面是一个快速示例,说明应该如何完成,同时确保您的第二个服务响应您指定的端口(8180)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc1
            port: 
              number: 80
      - path: /something2(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc2
            port: 
              number: 8180

相关问题