Kubernetes more ...



  • Create an NGINX Pod

kubectl run nginx --image=nginx

  • Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)

kubectl run nginx --image=nginx --dry-run=client -o yaml

  • Create a deployment

kubectl create deployment --image=nginx nginx

  • Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

  • Generate Deployment YAML file (-o yaml). Don't create it(--dry-run) with 4 Replicas (--replicas=4)

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

  • Save it to a file, make necessary changes to the file (for example, adding more replicas) and then create the deployment.

kubectl create -f nginx-deployment.yaml

OR

  • In k8s version 1.19+, we can specify the --replicas option to create a deployment with 4 replicas.

kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml


Note: if we don't want to create deployment object and we just wanted to create a yaml definition file for deployment then use below cmd
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

dry run ensures deployment oject is not created in cluster  and the output is redirected to yaml file.

If we already have a deployment object and we want to create yaml definition file from the existing deployment then use below command:

kubectl get deployment <existing deployment name> -o yaml > file_name.yaml

Namespaces


Delete all pods in a name space: 
kubectl delete --all pods --namespace=foo

Similarly delete all deployments in a namespace
kubectl delete --all deployments--namespace=foo

Switch to other namespaces:
kubectl config set-context $(kubectl config current-config) --namespace="name of ns you want to switch"

--------------------------
If we want to edit pod,then we cannod to so normally, create a yaml definition file from alraedy existing pos as showen below and replace forefully
step1:create a yam file from existing pod
kubectl get pod <pod name> -o yaml> anyfilename.yaml
step2: replace alreay running pod forefully
kubectl replace -f abovecreated tamlfile.yaml --force

---------------------------------------------------------------------------------------
A quick note on editing PODs and Deployments

Edit a POD

Remember, you CANNOT edit specifications of an existing POD other than the below.

  • spec.containers[*].image

  • spec.initContainers[*].image

  • spec.activeDeadlineSeconds

  • spec.tolerations

For example you cannot edit the environment variables, service accounts, resource limits (all of which we will discuss later) of a running pod. But if you really want to, you have 2 options:

1. Run the kubectl edit pod <pod name> command.  This will open the pod specification in an editor (vi editor). Then edit the required properties. When you try to save it, you will be denied. This is because you are attempting to edit a field on the pod that is not editable.

A copy of the file with your changes is saved in a temporary location as shown above.

You can then delete the existing pod by running the command:

kubectl delete pod webapp


Then create a new pod with your changes using the temporary file

kubectl create -f /tmp/kubectl-edit-ccvrq.yaml


2. The second option is to extract the pod definition in YAML format to a file using the command

kubectl get pod webapp -o yaml > my-new-pod.yaml

Then make the changes to the exported file using an editor (vi editor). Save the changes

vi my-new-pod.yaml

Then delete the existing pod

kubectl delete pod webapp

Then create a new pod with the edited file

kubectl create -f my-new-pod.yaml


Edit Deployments

With Deployments you can easily edit any field/property of the POD template. Since the pod template is a child of the deployment specification,  with every change the deployment will automatically delete and create a new pod with the new changes. So if you are asked to edit a property of a POD part of a deployment you may do that simply by running the command

kubectl edit deployment my-deployment




Comments

Popular posts from this blog

Docker Imp points

Docker volumes backup and restore & Docker networks