Kubernetes rolling deployment using the yaml file - kubernetes

I have deployed an application into Kubernetes using the following command.
kubectl apply -f deployment.yaml -n <NAMESPACE>
I have my deployment content in the deployment yaml file.
This is working fine. Now, I have updated few things in the deployment.yaml file and hence would like to update the deployment.
Option 1:- Delete and deploy again
kubectl delete -f deployment.yaml -n <NAMESPACE>
kubectl apply -f deployment.yaml -n <NAMESPACE>
Option 2:- Use set to update changes
kubectl set image deployment/nginx-deployment nginx=nginx:1.91
I don't want to use this approach as I am keeping my deployment.yaml file in GitHUB.
Option 3:- Using edit command
kubectl edit deployment/nginx-deployment
I don't want to use the above 3 options.
Is there any way to update the deployment using the file itself.
Like,
kubectl update deployment.yaml -n NAMESPACE
This way, I will make sure that I will always have the latest deployment file in my GitHub repo.

As #Daisy Shipton has said, what you want to do could be simplified with a simple command: kubectl apply -f deployment.yaml.
I will also add that I don't think it's correct to utilize the Option 2 to update the image utilized by the Pod with an imperative command! If the source of truth is the Deployment file present on your GitHub, you should simply update that file, by modifying the image that is used by your Pod's container there!
Next time you desire to update your Deployment object, unless you don't forget to modify the .yaml file, you will be setting the Pods to use the previous Nginx's image.
So there certainly should exist some restriction in using imperative commands to update the specification of any Kubernetes's object!

Related

Kubectl update imagePullPolicy

How do we update the imagePullPolicy alone for certain deployments using kubectl? The image tag has changed, however we don't require a restart. Need to update existing deployments with --image-pull-policy as IfNotPresent
Note: Don't have the complete YAML or JSON for the deployments, hence need to do it via kubectl
use
kubectl edit deployment <deployment_name> -n namespace
Then you will be able to edit imagePullPolicy

Kubernetes Simple Declarative Management

Is there a way for more simple Declarative Management than writing template files myself?
E.g. create the template files with kubectl command and then use kubectl apply on those templates?
kubectl create deployment my-app --image=nginx:latest --replicas=3 --port=8080 --dry-run=client --output=yaml > my-deployment.yaml
kubectl create service loadbalancer my-app --tcp=80:8080 --dry-run=client -o=yaml > my-service.yaml
And after this apply the generated template files:
kubectl apply -f .
Is it OK to use such approach for production?
Or it is considered not a good practice?
While it's quite common to write the individual YAML manifests
yourself, it's less common to apply them individually as you've shown
in your question. Where I work, we use Kustomize to manage our
manifests; this is a tool that assembles a collection of manifests
into a configuration which you then apply all at once using kubectl apply (other folks use Helm, but I don't have any experience with that tool).
There are lots of examples in the documentation, but for your example, you might do something like this:
Put your deployment manifest in my-deployment.yaml
Put your service manifest in my-service.yaml
Create a file kustomization.yaml with the following content:
resources:
- my-deployment.yaml
- my-service.yaml
To create or update your resources in the cluster, you run:
kustomize build | kubectl apply -f-
Kustomize has a number of options for generating things like
ConfigMaps and Secrets from files, for creating a hierarchical
configuration in which you can override portions of your manifests
with modified content, etc.
Note that a version of Kustomize is actually build into the kubectl
command; for the above example, you could have simply run:
kubectl apply -k .
The version of Kustomize built into kubectl is a little older than the standalone version, but for simple configurations it works just fine.

Kubernetes - update existing configmap from file

I created a simple text file on my local machine
I created a configmap out of that test file:
kubectl create configmap test-configm --from-file=test-file.txt
I added the volumemounts and volume to my deployment and verified the file is in my pods.
Now I want to modify the test-file.txt on my local machine and then update the configmap I created in step 2 so that all my pods can get the new version of that file, how can I accomplish this?
Thanks!
Per https://kubernetes.io/docs/concepts/configuration/configmap/ mounted configMaps are updated automatically. You would simply have to update the configMap using a dry-run followed by imperative command like this.
kubectl create configmap test-configm --from-file=test-file.txt --dry-run -o yaml | kubectl apply -f -

kubernetes -I changed the deployment name ,then redeploying to the environment,how to cleanup the old deployment and pods with old name?

Had requirement to change the pod or deployment name.now when we deploy,we have 2 deployments and 3 pods each with old and new name
So far i was deleting the old deployments manually.
do i need to manually delete the old deployment and pods or is there a better method?
To delete deployment use
$ kubectl delete deploy/old_deployment_name
This will delete deployment, including pods and rs, if you had them.
And dont do this mistake the second time :) #Kamol is right - the best way of managing resources is change config file(e.g your deployment) and re-apply with
kubectl apply -f deployment.yaml
I think we can also remove everything if its installed with the apply command
kubectl apply -f deployment.yaml
you can delete
kubectl delete -f deployment.yaml

find which yaml file was used for the any kubernetes resource?

How to find, that which yaml file was used for the deployment of any kubernetes resource.
I checked "kubectl describe", it doesn't list the same, is there anyway to know.
use case:
I want to update the yaml and redeploy, one option, I guess is to generate the yaml from running resource, update and redeploy.
any suggestions ?
To get yaml for your k8s application deployment.
Use this
kubectl get deploy my-deployment -o yaml --export
OR
kubectl get pod my-pod -o yaml --export
OR
kubectl get svc my-svc -o yaml --export
Editing is also simple.
kubectl get deploy my-deployment -o yaml --export > my-deployment.yml
<Edit the my-deployment.yml file and kubectl apply -f my-deployment.yml>
OR
kubectl edit deployment my-deployment
Hope this helps.
You can use the following command to get the content of an yaml file, that was used to create a deployment:
kubectl apply view-last-applied <resource_type> <resource_name>
In your case it will be similar to:
kubectl apply view-last-applied deployment <deployment_name>
I think you can choose here from two options.
Option 1:
You can grep all YAMLs looking for specific annotations or labels.
$ grep "app: nginx-test" *.yaml
or
$ grep -e "prometheus.io/scheme: http" *.yaml
When you find proper file you can edit it (vi, nano, etc) and apply.
$ kubectl apply -f [yaml-name]
Option 2:
When you know name of your deployment you can edit it.
$ kubectl edit deployment [deployment-name]
You will see current deployment YAML with a status: section which describing current status of deployment. If you don't like vi, you can use nano instead
$ KUBE_EDITOR="nano" kubectl edit [deployment-name]
If you want to create YAML from your current deployment I would advise you to use kubectl edit with --export flag. It will removes unneeded information (like status: from previous comment).
$ kubectl get deploy [your-deployment] -oyaml --export >> newDeployment.yaml
Hope it will help.