1

根据这个文档(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

4

1 回答 1

1

TL;博士

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

Annotations只能在整个 kubernetes 资源上设置,因为它们是资源的一部分metadataingress spec不包括较低级别的该功能。

-- Stackoverflow.com:问题:在路径级别应用 nginx-ingress 注释


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

  • 所有必需的域都指向的Service类型: LoadBalancernginx-ingress-controller
    • 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

其他资源:

于 2021-01-04T12:47:33.797 回答