Search code examples
google-cloud-platformterraformgoogle-kubernetes-engineload-balancing

How to make network endpoint groups disappear automatically


In GCP, I have a GKE with a workload configured. My service definition has the following annotation which automatically creates the network endpoint groups for me:

cloud.google.com/neg: '{"exposed_ports": {"80":{"name": "app2-service-80-neg"}}}'

I can then attach this to a backend load balancer service and all works well. However, shouldn't these network endpoint groups disappear if I delete the underlying service/deployment/pods? They seem to stick around after I delete everything at the kubernetes level which causes issues for my terraform because I run a terraform destroy regularly and it can't delete everything since these objects are still kicking around preventing me from deleting my VPC.


Solution

  • As @Vishal Bulbule mentioned NEG has to be deleted separately.
    As per this official doc

    Note that the NEG cannot be deleted if there are backend services referencing it.

    So,

    • When a GKE service is deleted, the associated NEG will not be
      garbage collected if the NEG is still referenced by a backend service.
    • Dereference the NEG from the backend service to allow NEG deletion.
    • When a cluster is deleted, standalone NEGs are not deleted automatically and need to be deleted manually. Use this Http request to delete the NEG or else refer to this doc to help in deleting it manually.

    You can also use below to delete a network endpoint group named my-neg:

    gcloud compute network-endpoint-groups delete my-neg --zone=us central1-a