可以在kubernetes的不同名称空间中使用相同的主机名吗?

时间:2020-09-22 15:21:50

标签: kubernetes kubernetes-ingress nginx-ingress

我想在第一个名称空间中将我的主机名用作test.com,在另一个名称空间中将我的主机名用作example.com/demo。但是我无法做到这一点,因为(我认为)nginx-ingress控制器始终指向第一个网站(example.com)。我的nginx-ingress控制器正在默认名称空间中运行

#namespace prod
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: prod-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153


#namespace dev
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: dev-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /demo
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153
 
 

1 个答案:

答案 0 :(得分:2)

您可以使用相同的主机名/域,但是要访问不同的后端服务,必须定义不同的路径,在这种情况下,您可以执行以下操作:

#namespace prod
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: prod-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /prod/static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /prod/
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153


#namespace dev
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: test-ingress
  namespace: dev-env
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /dev/static
            backend:
              serviceName: app-svc
              servicePort: 80
          - path: /demo
            backend:
              serviceName: app-svc
              servicePort: 8000
  tls:
  - hosts:
    - example.com
    secretName: selfsigned-cert
status:
  loadBalancer:
    ingress:
      - ip: 80.180.31.153

像这样的你有

example.com/prod/static -> points to app-svc:80 on prod-env namespace  
example.com/prod/ -> points to app-svc:8000 on prod-env namespace  

example.com/dev/static -> points to app-svc:8000 on dev-env namespace  
example.com/demo -> points to app-svc:8000 on dev-env namespace 

如果需要在主机后保留url,则应为每个命名空间添加一个子域:

dev.example.com/static -> points to app-svc:80 on prod-env namespace  
prod.example.com/static -> points to app-svc:8000 on dev-env namespace  

要使此功能正常运行,您必须在域DNS管理中将其定义为A记录,并指向与loadBalancer相同的IP(在本例中为80.180.31.153):

example.com A -> 80.180.31.153 (optional)   
dev.exmaple.com A -> 80.180.31.153  
prod.example.com A -> 80.180.31.153  

或最简单的一个:

*.example.com A -> 80.180.31.153