I have to create deployment spec file that will:
Save a copy of this spec file to deployment_spec.yaml or json. After I am done, I need to clean up(delete) any new Kubernetes object which I produced during this task. Based on this task, I do not know how to solve this task. Should I create the deployment and after that, use command
kubectl get deployment star_deploy -o yaml > deployment_spec.yaml
or during creation, use command
kubectl create deployment star_deploy --image=nginx --dry-run -o yaml > deployment_spec.yaml
What do you think is best way to do it?
I would advocate usage of --dry-run
option to create a base yaml file and edit it to make necessary changes and then use kubectl create -f deployment.yaml
to actaually create the resources.
Once the task is done you can clean up using kubectl delete -f deployment.yaml
This is following the declarative methodology which makes it easier to define deployments (or any resource) and manage their lifecycle through CI/CD or GitOps.
Dry run is always good because it give only necessary output without any garbage value like revision number, creationTimestmp etc. And this will give you clean production grade yaml file for deployment. Later you can use this to create daemonsets etc.
kubectl create deployment star_deploy --image=nginx --dry-run=client -o yaml > deployment_spec.yaml
Note: Always run dry-run as client
Once you generate the yaml then apply this yaml to create deployment by using
kubectl apply -f deployment_spec.yaml
to scale the pods for that deployment you don'even need to edit yaml file.
kubectl scale deployment star_deploy --replicas=3
and to delete the created deployment you can use
kubectl delete deployment star_deploy