ChaseMoskal
ChaseMoskal

Reputation: 7681

Deleting kubernetes yaml: how to prevent old objects from floating around?

i'm working on a continuous deployment routine for a kubernetes application: everytime i push a git tag, a github action is activated which calls kubectl apply -f kubernetes to apply a bunch of yaml kubernetes definitions

let's say i add yaml for a new service, and deploy it -- kubectl will add it

but then later on, i simply delete the yaml for that service, and redeploy -- kubectl will NOT delete it

is there any way that kubectl can recognize that the service yaml is missing, and respond by deleting the service automatically during continuous deployment? in my local test, the service remains floating around

does the developer have to know to connect kubectl to the production cluster and delete the service manually, in addition to deleting the yaml definition?

is there a mechanism for kubernetes to "know what's missing"?

Upvotes: 2

Views: 3927

Answers (4)

Akira Yamamoto
Akira Yamamoto

Reputation: 4955

You can use the --prune flag to identify and delete objects that have been removed from the local filesystem. Examples:

KUBECTL_APPLYSET=true kubectl apply -f <directory> --prune --applyset=<name>

kubectl apply -f <directory> --prune -l <labels> --prune-allowlist=<gvk-list>

See the Kubernetes documentation.

Note: this feature is on alpha.

Upvotes: 0

Mostafa Wael
Mostafa Wael

Reputation: 3846

Before deleting the yaml file, you can run kubectl delete -f file.yaml, this way all the resources created by this file will be deleted.


However, what you are looking for, is achieving the desired state using k8s. You can do this by using tools like Helmfile.

Helmfile, allow you to specify the resources you want to have all in one file, and it will achieve the desired state every time you run helmfile apply

Upvotes: 3

Mark Watney
Mark Watney

Reputation: 5980

You need to use a CI/CD tool for Kubernetes to achieve what you need. As mentioned by Sithroo Helm is a very good option.

Helm lets you fetch, deploy and manage the lifecycle of applications, both 3rd party products and your own.

No more maintaining random groups of YAML files (or very long ones) describing pods, replica sets, services, RBAC settings, etc. With helm, there is a structure and a convention for a software package that defines a layer of YAML templates and another layer that changes the templates called values. Values are injected into templates, thus allowing a separation of configuration, and defines where changes are allowed. This whole package is called a Helm Chart.

Essentially you create structured application packages that contain everything they need to run on a Kubernetes cluster; including dependencies the application requires. Source

Before you start, I recommend you these articles explaining it's quirks and features.

The missing CI/CD Kubernetes component: Helm package manager

Continuous Integration & Delivery (CI/CD) for Kubernetes Using CircleCI & Helm

Upvotes: 1

Anmol Agrawal
Anmol Agrawal

Reputation: 914

There's no such way. You can deploy resources from yaml file from anywhere if you can reach the node and configure kube config. So kubernetes will not know how to respond on a file deletion. If you still want to do this, you can write a program (a go code) which checks the availability of files in one place and deletes the corresponding resource whenever the file gets deleted.

There's one way via kubernetes is by using kubernetes operator, and whenever there is any change in your files you can update the crd used to deploy resources via operator.

Upvotes: 1

Related Questions