---
# System prepended metadata

title: Core concepts Tips & tricks

---

# Core concepts Tips & tricks 
- Generating pod yaml defs
    - `kubectl run --generator=run-pod/v1 <pod-name> --image=<image-name> --dry-run -o yaml > pod.yaml`
- Set image using imperative command
    - `kubectl set image pod <pod-name> <container-name>:<image-name>`
    - `kubectl set image deployment <deployment-name> <container-name>:<image-name>`

- Amrit's bash history:
    ```
       15  kubectl describe pod $(kubectl get pods --no-headers | awk '{print $1}') | grep -i node:
       16  kubectl describe pod webapp
       17  clear
       18  kubectl get pod webapp
       19  kubectl  describe pod webapp
       20  kubectl get pods
       21  kubectl delete pod webapp --force --grace-period=0
       22  kubectl get pods
       23  clear
       24  kubectl run --generator=run-pod/v1 redis --image=redis123
       25  kubectl get pods
       26  kubectl  delete pod redis --force --grace-period=0
       27  kubectl run --generator=run-pod/v1 redis --image=redis123 --dry-run -o yaml > redis.yaml
       28  kubectl  apply -f redis.yaml
       29  kubectl edit pod redis
       30  kubectl get pods -w
       
       -- Deployment bash history --
       
        3  kubectl get pods
        4  kubectl get rs
        5  kubectl get deployments
        6  clear
        7  kubectl get deployments
        8  kubectl get rs
        9  kubectl get pods
       10  kubectl  describe deployments frontend-deployment
       11  kubectl describe pod frontend-deployment-b9b7c6cb6-jz588
       12  vim /root/deployment-definition-1.yaml
       13  kubectl  apply -f /root/deployment-definition-1.yaml --dry-run
       14  kubectl  apply -f /root/deployment-definition-1.yaml
       15  vim /root/deployment-definition-1.yaml
       16  kubectl  apply -f /root/deployment-definition-1.yaml
       17  kubectl get deployments
       18  clear
       19  kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --replicas=3
       20  kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --dry-run -o yaml
       21  kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --dry-run -o yaml > deployment.yaml
       
      -- Namespaces --
       3  kubectl get ns
       4  kubectl get ns --no-headers | wc -l
       5  kubectl get pods -n research
       6  kubectl run --generator=run-pod/v1 redis --image=redis -n finance
       7  kubectl get pods -n finance
       10  kubectl get pods --all-namespaces | grep -i blue
       11  history
      
      -- Services --
        6  kubectl get svc
        7  kubectl describe svc kubernetes
        8  kubectl get deployments.
        9  kubectl get deployments simple-webapp-deployment -o yaml | grep image
       10  kubectl expose deployment simple-webapp-deployment --type=NodePort --help
       11  kubectl expose deployment simple-webapp-deployment --type=NodePort --target-port=8080 --port=8080 --node-port=30080 --dry-run
       12  kubectl expose deployment simple-webapp-deployment --type=NodePort --target-port=8080 --port=8080  --dry-run
       13  kubectl expose deployment simple-webapp-deployment --type=NodePort --target-port=8080 --port=8080  --dry-run -o yaml > service-definition-1.yaml
       14  vim service-definition-1.yaml
       15  kubectl apply -f service-definition-1.yaml --dry-run
       16  kubectl apply -f service-definition-1.yaml
       
     -- Imperative commands --
       3 kubectl  run --generator=run-pod/v1 nginx-pod --image=nginx:alpine
       4  kubectl  run --generator=run-pod/v1 redis --image=redis:alpine --labels="tier=db" --dry-run
       5  kubectl  run --generator=run-pod/v1 redis --image=redis:alpine --labels="tier=db"
       6  kubectl describe pod redis
       7  kubectl expose pod redis --name=redis-service --port=6379
       8  kubectl describe svc redis-service
       9  kubectl create deployment webapp --image=kodekloud/webapp-color
       10  kubectl scale deployment webapp --replicas=3
       11  kubectl get deployments.
       12  kubectl expose deployment webapp --name=webapp-service --type=NodePort --port=8080 --targetPort=8080 --help | grep -i node
       13  kubectl expose deployment webapp --name=webapp-service --type=NodePort --port=8080 --target-port=8080 --help | grep -i node
       14  kubectl expose deployment webapp --name=webapp-service --type=NodePort --port=8080 --target-port=8080 --node-port=30082
       15  kubectl expose deployment webapp --name=webapp-service --type=NodePort --port=8080 --target-port=8080 --dry-run -o yaml > svc.yaml
       16  vim svc.yaml
       17  kubectl apply -f svc.yaml
       18  kubectl  describe svc webapp-service
         
    ```
 - Buvanesh's bash history
    ```    
    3  kubectl get pod
    4  kubectl run --generator=run-pod/v1 nginx --image=nginx
    5  kubectl get pod
    6  kubectl get pod -oyaml |grep -i image
    7  kubectl get pods -owide
    8  kubectl logs webapp
    9  kubectl describe pod agentx
   10  kubectl describe pod webapp
   11  kubectl get pod
   12  kubectl delete pod webapp
   13  kubectl run --generator=run-pod/v1 redis --image=redis123
   14  kubectl get pod
   15  kubectl set iamge pod redis=redis
   16  kubectl set image pod redis=redis
   17  kubectl set image pod redis redis=redis
   18  kubectl get pod
   19  history
   ```
```-- ReplicaSet --
   3  kubectl get pod
    4  kubectl get rs
    5  kubectl get pod
    6  kubectl get pod new-replica-set-2j4bz -oyaml |grep -i imag
    7  kubectl delete pod new-replica-set-sgsnk
    8  kubectl get pod
    9  cat replicaset-definition-1
   10  cat replicaset-definition-1.yaml
   11  ls
   12  vi replicaset-definition-1
   13  vim replicaset-definition-1.yaml
   14  kubectl create -f replicaset-definition-1.yaml
   15  vim replicaset-definition-1.yaml
   16  kubectl create -f replicaset-definition-
   17  kubectl create -f replicaset-definition-1.yaml
   18  kubectl get pod
   19  vim replicaset-definition-2.yaml
   20  kubectl create -f replicaset-definition-2.yaml
   21  kubectl delete rs replicaset-1
   22  kubectl delete rs replicaset-2
   23  kubectl set image s
   24  kubectl get pod
   25  kubectl describe rs new-replica-set -oyaml
   26  kubectl describe rs new-replica-set -
   27  kubectl set image rs new-replica-set busybox-container=busybox
   28  kubectl get pod
   29  kubectl edit rs new-replica-set
   30  kubectl get pod
   31  kubectl delete pod --all
   32  kubectl get pod
   33  kubectl scale rs --replicas=5 new-replica-set
   34  kubectl scale rs --replicas=2 new-replica-set
   35  history
   
   -- Deployment --
    3  kubectl get pods
    4  kubectl get rs
    5  kubectl get deploy
    6  kubectl get drs
    7  kubectl get rs
    8  kubectl get pods
    9  kubectl get deployment frontend-deployment -oyaml |grep -i image
   10  cat deployment-definition-1.yaml
   11  kubectl run --generator=deployment/v1beta1 deployment-1 --
   12  cat deployment-definition-1.yaml
   13  kubectl run --generator=deployment/v1beta1 deployment-1 --image=busybox888 --command sh "-c" echo Hello
Kubernetes!
   14  kubectl run --generator=deployment/v1beta1 deployment-1 --image=busybox888 --command sh "\-c" echo Hello Kubernetes!
   15  kubectl get pod
   16  kubectl run --generator=deployment/v1beta1 --image=httpd:2.4-alpine --replicas=3
   17  kubectl run --generator=deployment/v1beta1 httpd-frontend --image=httpd:2.4-alpine --replicas=3
   
   -- Namespace --
       5  kubectl get ns | wc -l
    6  kubectl get pods -n research |wc -l
    7  kubectl create pod redis --image=redis
    8  kubectl run --generator=run-pod/v1 redis --image=redis -n finanace
    9  kubectl run --generator=run-pod/v1 redis --image=redis -n finance
   10  kubectl get pods --all-namespaces
   -- Services--
    6  kubectl get svc --no-headers |wc -l
    7  kubcetl get svc
    8  kubectl get svc
    9  kubectl get svc -oyaml   10  kubectl get svc --show-labels
   11  kubectl get ep --no-headers   12  alias k=kubectl
   13  complete -F __start_kubectl k   14  k get pod
   15  k get deployments --no-headers |wc -   16  k get deployments --no-headers |wc -l
   17  k get deployments -o yaml |grep -i image   18  vim service-definition-1.yaml
   19  kubectl create -f service-definition-1.yaml
   
   -- Imperative --
    5  k run --generator=run-pod/v1 nginx-pod --image=nginx:alpine
    6  k run --generator=run-pod/v1 redis --image=redis:alpine --labels=tier=db
    7  kubectl get pods --show-labels    8  kubectl expose pod redis --name=redis-service --port=6379
    9  kubectl run --generator=run-pod/v1 webapp --image=kodekloud/webapp-color --replicas=3
   10  kubectl delete pod webapp
   11  kubectl run --generator=deployment/v1beta1 webapp --image=kodekloud/webapp-color --replicas=3
   12  kubectl expose deployment webapp --name=webapp-service --port=8080 --nodeport=30082
   13  kubectl expose deployment webapp --name=webapp-service --port=8080 --type=NodePort
   14  kubectl edit svc webapp-service
   15  kubectl get pod

   
``` - Jay's bash history ```

  
``
   3  alias k=kubectl
    4  complete -F __start_kubectl k
    5  k get pods
    6  k get all
    7  k get ns
    8  k get pods -n=research
    9  k run  redis --image=redis -n finance
   10  k get pods -n finance
   11  k delete pod redis -n finance
   12  k delete pod redis-bb7894d65-c9fgr -n finance
   13  k run  redis --image=redis -n finance  --restart=Never
   14  k get pods -n finance
   15  k get ns
   16  for x in ` k get ns`;do echo $x; k get pods -n $x  | grep blue ; done
   17  k exec -it pod blue -n marketing
   18  k exec -it  blue -n marketing -- sh
   ```
   
   
   Notes:
   1. Delete all of the pods under a replicaset
      `$ kubectl scale rs rs-name --replicas=0`
   2. Delete pods quickly
      `$ kubectl delete pod webapp --force --grace-period=0`
   3. Get shortcut for alias 
      `$ kubectl create <resource-name> --help |grep --Aliases`
   4. Get all api-resources in a cluster with kind and their respective apiGroups
       `$ kubectl api-resources` 
         OR
       `$ kubectl create <resource> <resource-name> -oyaml --dry-run`
   5. Use wc -l while getting info about no. of pods/deployments/etc
      `$ kubectl get pods |wc -l` # -1 
       OR use --no-headers to get rid of the first line
      `$ kubectl get pod --no-headers`
   7. Shorten form setup for kubectl
      `alias k=kubectl`
      `complete -F __start_kubectl k`
      Ref: https://kubernetes.io/docs/reference/kubectl/cheatsheet/ 