DEV Community


Kubernetes - Recreate element without error if already exists

Maxime Guilbert
Just a dev who touches a lot of things and wants to share his experiences. (or just want to note somewhere things to don't forget)
・1 min read

One thing I never realized was the power of kubectl apply.

So if you want to quickly create an element in Kubernetes and avoid the error if the element already exists, you can use the following command (and adapt it to your needs)

kubectl create namespace my-namespace --dry-run=client -o yaml | kubectl apply -f -
Enter fullscreen mode Exit fullscreen mode

If you want more complex elements, you can use an existing file as input.

kubectl apply -f myYaml.yml
Enter fullscreen mode Exit fullscreen mode

And if you want more dynamism, you can use Helm or Kustomize!


I hope it will help you! 😀

Discussion (0)