6

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
niklodeon
  • 1,320
  • 5
  • 20
  • 51
  • DNS Servers do not know anything about URLs. DNS servers only process domain names. If your goal is to rewrite URLs you will need a service running at the receiving endpoint to redirect the client to the new location. This is easily done with most web servers such as Apache or Nginx. Another option is to use a Load Balancer configured with URL maps in front of your domains. – John Hanley Nov 05 '20 at 22:10
  • @JohnHanley may be I should not have used the word redirecting in my question... My aim is that when some1 visits abc.mycompany/test-api then they are actually served the content from test-api.mycompany.com – niklodeon Nov 05 '20 at 22:36
  • Do you ant a proxy or a redirect? – guillaume blaquiere Nov 06 '20 at 08:31
  • @guillaumeblaquiere proxy... – niklodeon Nov 06 '20 at 11:35
  • Could you elaborate your question (also with information from comments)? So you want to redirect trafic to specific service (inside this cluster or other cluster, cloud?) to execute some script? – PjoterS Nov 06 '20 at 11:43

1 Answers1

7

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.

PjoterS
  • 12,841
  • 1
  • 22
  • 54