k8s入口重定向到集群外部的端点

时间:2019-10-21 15:24:36

标签: nginx kubernetes google-cloud-platform kubernetes-ingress

我正在使用Google Clould,GKE。

我有这个示例ingress.yaml

  1 apiVersion: extensions/v1beta1
  2 kind: Ingress
  3 metadata:
  4   name: kuard
  5   namespace: sample
  6   annotations:
  7     kubernetes.io/ingress.class: "nginx"
  8     cert-manager.io/issuer: "letsencrypt-prod"
  9     nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com
 10 spec:
 11   tls:
 12   - hosts:
 13     - example.gordion.io
 14     secretName: quickstart-example-tls
 15   rules:
 16   - host: example.gordion.io
 17     http:
 18       paths:
 19       - path: /
 20         backend:
 21           serviceName: kuard
 22           servicePort: 80

我需要在用户请求特定主机(例如example-2.gordion.io)时使用Nginx重定向到群集之外的其他站点(实际上是在其他Google群集上)。

目前,我仅了解似乎是全局的特定注释nginx.ingress.kubernetes.io/permanent-redirect。如何根据该入口文件中特定的请求主机进行重定向?

1 个答案:

答案 0 :(得分:2)

您将externalName服务与另一个入口文件结合在一起: 在以下yaml文件中,我们定义了一个名为example-2.gordion.io-service的{​​{3}}服务,该服务将导致另一个集群中的实际站点服务:

kind: Service
apiVersion: v1
metadata:
  name: example-2.gordion.io-service
spec:
  type: ExternalName
  externalName: internal-name-of-example-2.gordion.io

还有一个将example-2.gordion.io定向到example-2.gordion.io-service的入口文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: example-2.gordion.io
    http:
      paths:
      - path: /
        backend:
          serviceName: example-2.gordion.io-service
          servicePort: 80