How does Ingress proxy an service outside the kubernetes?

9/9/2019

How can I use ingress to proxy a kubernetes external url address? Before I used nginx as a proxy, the configuration is as follows.

       location /index.html {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_pass  http://172.19.2.2:8080/index.html;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_intercept_errors off;
            proxy_connect_timeout 900000;
            proxy_send_timeout 900000;
            proxy_read_timeout 900000;
            add_header Cache-Control 'no-cache';
            add_header Access-Control-Allow-Origin *;
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
       }

http://172.19.2.2:8080/index.html Is a service outside of kubernetes。

How can I achieve the effect of the above nginx configuration proxy in ingress?

kubernetes version info

Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.3", GitCommit:"5e53fd6bc17c0dec8434817e69b04a25d8ae0ff0", GitTreeState:"clean", BuildDate:"2019-06-06T01:44:30Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.3", GitCommit:"5e53fd6bc17c0dec8434817e69b04a25d8ae0ff0", GitTreeState:"clean", BuildDate:"2019-06-06T01:36:19Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

ingress version info

0.20.0

ingress configuration

Configuration without external url

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: dev-yilin-web-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

spec:
  tls:
  - hosts:
    - dev-yilin.example.com
    secretName: example-ingress-secret
  rules:
  - host: dev-yilin.example.com
    http:
      paths:
      - path: /yilin
        backend:
          serviceName: dev-yilin-web
          servicePort: 8080
-- liyao
kubernetes

1 Answer

9/9/2019

Here is a nice article from Google Cloud about how to create services for external endpoints: https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services

Then all you would have to add is a rules entry with the new service and port you configured. As for how kubernetes handles this, it returns a DNS name/IP (depending on which method you configured for your endpoint, and then as far I understand the ingress handles this like any other request.

Hope this helps.

-- cewood
Source: StackOverflow