kubernetes ingress服务器别名仅适用于一个ingress主机



根据此文档(https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#server-别名(,我可以在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只能在整个kubernetes资源上设置,因为它们是资源metadata的一部分。ingress spec在较低级别上不包含该功能。

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


扩展答案,给出如何创建此类设置的示例。让我们假设(示例(:

  • 指向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

其他资源:

  • Kubernetes.io:文档:概念:服务网络:入口
  • Kubernets.github.io:Ingress NGINX:Annotations:服务器别名

相关内容

  • 没有找到相关文章

最新更新