Working With Kubernetes Deployments
When I first started working with Kubernetes Deployments, the defacto way to create
a deployment was to write up some configuration into a YAML file and run kubectl --namespace=<somespace> apply -f <file>
.
This made it easy to make changes to the deployment later. Simply, make the required changes in the file and run
kubectl --namespace=<somespace> apply -f <filename>
.
However, recently after following some Google Kubernetes Engine tutorials, I’ve gotten into a habit of creating
deployments using the kubectl --namespace=<somespace> run
command. I then realised that I needed to make
some changes to the deployment and tried to export the configuration as YAML, make the change and apply it, but
this resulted in a warning:
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
Since then, I’ve tried to find a way of using both methods interchangeably. For example, I’d like to first create
a deployment with kubectl run
and then later switch to updating it by modifying the YAML file and running
kubectl --namespace=<somespace> apply
. Recently, while reading the Kubernetes: Up and Running book by Joe Beda,
Brendan Burns and Kelsey Hightower, I stumbled upon a way of converting a deployment created with kubectl run
to a deployment that you can manipulate with a YAML config file and kubectl apply
.
kubectl get deployments <deploymentname> --export -o yaml > <yaml filename>
kubectl replace -f <filename> --save-config
You can then make changes to the config stored in kubectl apply
.
I hope this is useful to anyone else who wants to switch between using these two methods to manage your
Kubernetes deployments!