将流量重定向到外部网址

问题描述

基于评论的更新:

让我们说另一个GCP项目中有一个@ hello.company1.com托管的API ...

我希望当some1访问URL abc.company.com时,它们是来自hello.company1.com的服务器流量,类似于API网关...

使用API​​网关可以轻松完成,我只是想弄清楚使用K8S服务和入口是否可行。


我创建了一个Cloud DNS区域,名称为abc.company.com

当有人访问abc.company.com/google时,我希望将请求转发到外部网址,例如google.com

是否可以通过创建外部名称类型的服务以及主机名为abc.company.com的入口来实现

kind: Service
apiVersion: v1
Metadata:
  name: test-srv
spec:
  type: ExternalName
  externalName: google.com

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
Metadata:
  name: test-ingress
spec:
  rules:
  - host: abc.company.com
  - http:
      paths:
      - path: /google
        backend:
          serviceName: test-srv

解决方法

可以实现您想要的,但是您将需要使用Nginx Ingress来实现,因为您将需要使用特定的注释-nginx.ingress.kubernetes.io/upstream-vhost

this Github issue中基于storage.googleapis.com对此进行了很好的描述。

apiVersion: v1
kind: Service
metadata:
  name: google-storage-buckets
spec:
  type: ExternalName
  externalName: storage.googleapis.com
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: proxy-assets-ingress
  annotations:
    kubernetes.io/ingress.class: nginx-ingress
    nginx.ingress.kubernetes.io/rewrite-target: /[BUCKET_NAME]/[BUILD_SHA]
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    nginx.ingress.kubernetes.io/upstream-vhost: "storage.googleapis.com"
spec:
  rules:
  - host: abc.company.com
    http:
      paths:
      - path: /your/path
        backend:
          serviceName: google-storage-buckets
          servicePort: 443

根据您的需要,如果要在非https上使用它,则需要将servicePort更改为80并删除注释nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"

有关其他详细信息,您可以检查其他类似的Stackoverflow question

请记住不要在同一清单的-spec.rules.host中使用spec.rules.http。如果您的配置中没有-,则应仅将httphost一起使用。