Kubernetes Garbage Collection

This means that one can decide the parent and child relationship for a pair or more number of objects.For an example, let’s say that we have a Custom Resource Definition in Kubernetes called Database and every object of type Database that we create requires us to have its own Namespace..Kubernetes provides a metadata field called ownerReference that can be used to define a relationship between the Object and Namespace..In literal terms we can define something like this:I have a Database object called Postgres which is the owner of its corresponding Namespace called Postgres-Namespace.Let’s try to understand this much more clearly through an example..You can walk along and try out the example that follows.The prerequisites for this would be to have the understanding at least the terms CRDs and Namespaces and also to have a Kuberenetes cluster to work with..You can use a minikube VM.Create the CRD and Namespace for us to play withThe manifest shared below defines a CRD called Database:Let’s use this to define a database object called postgres:Let’s also define a namespace called postgres-namespace:Now with those, we have a database object called postgres and also a namespace that should be related to the object..Let’s create them using the commands below:kubectl create -f database-crd.yamlkubectl create -f postgres-crd.yamlkubectl apply -f postgres-ns.yamlSet ownerReference & create a Parent-Child relationNow, there is a unique metadata field called ownerReference that Kubernetes provides, with which we can map a resource to its parent..This also means that after such a mapping is done, when the parent is out of scope the resource that points to the parent will also go out of scope..To imagine this interms of the example that we’re looking at, when we delete the database called postgres, this should result in the automatic garbage collection of the namespace “postgres-namespace”.Now we’re going to link the postgres object and the namespace that we had just created in the previous example..We should make sure that the namespace should be garbage collected after the database object is deleted.So let us go ahead and these fields added as metadata to the manifest that we created for namespace.UID, UID UID!Now you might notice the uid field set under the ownerReferences..UID is the identifier that is going to be unique across all the types of resources that you create..Kubernetes leverages this to actually create the Parent and Child relationship between various resources..To get this UID, you can use this command:kubectl get database postgres -oyaml | grep uidUsing this UID, we can easily relate the namespace to the database postgres object.. More details

Leave a Reply