kubernetes ingress server-alias 仅适用于一台入口主机

kubernetes ingress server-alias only applies to one ingress host

根据此文档 (https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#server-alias),我可以向 nginx 配置文件添加额外的 server_name。 但是,它会向我的所有主机添加额外的 server_name,这肯定会导致冲突。 有没有办法只为我的一个主机添加服务器别名?假设我只想将 10.10.0.100 添加到我的 test1 主机。 入口示例:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/server-alias: 10.10.0.100
spec:
  rules:
  - host: test1.com
    http:
      paths:
      - path: /
        backend:
          service:
            name: test1-service
            port:
              number: 8000
        pathType: Prefix
  - host: test2.com
    http:
      paths:
      - path: /
        backend:
          service:
            name: test2-service
            port:
              number: 8000
        pathType: Prefix

TL;DR

您可以将 Ingress 资源拆分到多个对象(它们将协同工作)以仅将 Annotations 添加到特定的 hosts

Annotations can only be set on the whole kubernetes resource, as they are part of the resource metadata. The ingress spec doesn't include that functionality at a lower level.

--


扩展答案以举例说明如何创建此类设置。让我们假设(示例):

  • 指向 nginx-ingress-controllerLoadBalancer 类型的 Service 的所有必需域:
    • hello.kubernetes.docker.internal - 用于 host .spec
    • hello-two.kubernetes.docker.internal - 用于 annotations .metadata
    • --
    • goodbye.kubernetes.docker.internal - 用于 host .spec
    • goodbye-two.kubernetes.docker.internal- 用于annotations .metadata

跳过 DeploymentService 定义,Ingress 资源应如下所示:

hello-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-ingress
  annotations:
    nginx.ingress.kubernetes.io/server-alias: "hello-two.kubernetes.docker.internal"
spec:
  rules:
  - host: hello.kubernetes.docker.internal # <-- IMPORTANT  
    http:
      paths:
      - path: /
        backend:
          service:
            name: hello-service
            port:
              number: 80
        pathType: Prefix

goodbye-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: goodbye-ingress
  annotations:
    nginx.ingress.kubernetes.io/server-alias: "goodbye-two.kubernetes.docker.internal"
spec:
  rules:
  - host: goodbye.kubernetes.docker.internal # <-- IMPORTANT 
    http:
      paths:
      - path: /
        backend:
          service:
            name: goodbye-service
            port:
              number: 80
        pathType: Prefix

以上定义将创建 2 个 Ingress 将被合并的资源:

  • hello-service 将回应:
    • hello.kubernetes.docker.internal
    • hello-two.kubernetes.docker.internal
  • goodbye-service 将回应:
    • goodbye.kubernetes.docker.internal
    • goodbye-two.kubernetes.docker.internal

运行:

  • $ kubectl get ingress:
NAME              CLASS    HOSTS                                ADDRESS     PORTS   AGE
goodbye-ingress   <none>   goodbye.kubernetes.docker.internal   localhost   80      36m
hello-ingress     <none>   hello.kubernetes.docker.internal     localhost   80      36m

其他资源: