Redirecting traffic to external url

Viewed 6502

Updates based on comments:

Lets say there's an API hosted @ hello.company1.com in another GCP Project...

I would like to have a possibility that when some1 visits a url abc.company.com they are serverd traffic from hello.company1.com something similar to an API gateway...

It could be easily done with an API gateway, I am just trying to figure out if its possible to with K8S service & ingress.


I have created a Cloud DNS zone as abc.company.com

When someone would visit abc.company.com/google I would like the request to be forwarded to an external url let's say google.com

Could this be achieved by creating a service of type external name and an ingress with host name 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
1 Answers

It's possible to achieve what you want, however you will need to use Nginx Ingress to do that, as you will need to use specific annotation - nginx.ingress.kubernetes.io/upstream-vhost.

It was well described in this Github issue based on 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

Depends on your needs, if you would use it on non https you would need to change servicePort to 80 and remove annotation nginx.ingress.kubernetes.io/backend-protocol: "HTTPS".

For additional details, you can check other similar Stackoverflow question.

Please remember to not use - in spec.rules.host and spec.rules.http in the same manifest. You should use - only with http, if you don't have host in your configuration.

Related