Ingress如何在kubernetes之外代理服务?

时间:2019-09-09 03:08:59

标签: kubernetes

如何使用Ingress代理kubernetes外部URL地址? 在使用nginx作为代理之前,配置如下。

       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是kubernetes之外的服务。

如何在入口中实现上述nginx配置代理的效果?

kubernetes版本信息

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"}

入口版本信息

0.20.0

入口配置

没有外部网址的配置

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

1 个答案:

答案 0 :(得分:1)

这是Google Cloud上的一篇不错的文章,内容涉及如何为外部端点创建服务:https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services

然后,您需要添加的就是带有您配置的新服务和端口的规则条目。至于kubernetes的处理方式,它将返回一个DNS名称/ IP(取决于您为端点配置的方法,然后据我所知,入口将像处理其他请求一样处理该问题。

希望这会有所帮助。