Dynamically adding/removing named hosts from k8s ingress

前端 未结 2 984
春和景丽
春和景丽 2021-01-21 01:07

I\'m setting up a k8s cluster on GKE. A wildcard DNS *.server.com will point to a Ingress controller. Internally to the cluster, there will be webserver pods, eac

相关标签:
2条回答
  • 2021-01-21 01:46

    It appears like you're planning to host multiple domain names on a single Load Balancer (==single Ingress resource). If not, this answer doesn't apply.

    You can do this by configuring Ingress with a long list of domain names like:

    spec:
      rules:
      - host: cats.server.com
        http:
          paths:
          - path: /*
            backend:
              serviceName: cats
              servicePort: 8080
      - host: dogs.server.com
        http:
          paths:
          - path: /*
            backend:
              serviceName: dogs
              servicePort: 8080
      - [...]
    

    If that's your intention, there's no way of doing this without editing this whole list and applying it to the cluster every time.

    You can build a tool to construct this manifest file, then apply the changes. The Ingress controller is smart enough that existing domains will not see a downtime if they're still on the list.

    However the domains you removed from the list will also be removed from the URL Map of the load balancer and hence stop accepting the traffic.

    0 讨论(0)
  • 2021-01-21 01:49

    I found a solution to add a rule to an ingress by executing the following patch:

    [
      {
        "op": "add",
        "path": "/spec/rules/-",
        "value": {
          "host": "<HOST>",
          "http": {
            "paths": [
              {
                "path": "/*",
                "backend": {
                  "serviceName": "<SERVICE_NAME>",
                  "servicePort": <PORT>
                }
              }
            ]
          }
        }
      }
    ]
    
    kubectl patch ingress ${INGRESS_NAME} --type json -p "$(cat patch.json)"
    

    But I cant find the solution to remove it. What I tried is the follwoing patch;

    [
      {
        "op": "remove",
        "path": '{.spec.rules[?(@.host=="<HOST>")]}'
      }
    ]
    
    

    But I just get the error 'The "" is invalid' back from kubectl

    Whats wrong with it? I followed the jsonPath syntax from https://kubernetes.io/docs/reference/kubectl/jsonpath/

    0 讨论(0)
提交回复
热议问题