Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding/removing named hosts from k8s ingress

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, each exposing a unique service. The Ingress controller will use the server name to route to the various services.

Servers will be created and destroyed on a nearly daily basis. I'd like to know if there's a way to add and remove a named server from the ingress controller without editing the whole list of named servers.

like image 270
Laizer Avatar asked Oct 11 '25 18:10

Laizer


1 Answers

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/


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!