Commands Flashcards
List all the namespaces in the cluster
kubectl get namespaces
kubectl get ns
List all the pods in all namespaces
kubectl get po –all-namespaces
List all the pods in the particular namespace
kubectl get po -n
List all the services in the particular namespace
kubectl get svc -n
List all the pods showing name and namespace with a json path expression
kubectl get pods -o=jsonpath=”{.items[*][‘metadata.name’, ‘metadata.namespace’]}”
Create an nginx pod in a default namespace and verify the pod running
// creating a pod kubectl run nginx --image=nginx --restart=Never // List the pod kubectl get po
Create the same nginx pod with a yaml file
// get the yaml file with --dry-run flag kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml
// cat nginx-pod.yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx name: nginx spec: containers: - image: nginx name: nginx resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {} // create a pod kubectl create -f nginx-pod.yaml
Output the yaml file of the pod you just created ( nginx)
kubectl get po nginx -o yaml
Output the yaml file of the pod you just created without the cluster-specific information (nginx)
kubectl get po nginx -o yaml –export
Get the complete details of the pod you just created (nginx)
kubectl describe pod nginx
Delete the pod you just created ( nginx, nginx-pod.yaml)
kubectl delete po nginx
kubectl delete -f nginx-pod.yaml
Delete the pod you just created without any delay (force delete) (nginx)
kubectl delete po nginx –grace-period=0 –force
Create the nginx pod with version 1.17.4 and expose it on port 80
kubectl run nginx –image=nginx:1.17.4 –restart=Never –port=80
Change the Image version to 1.15-alpine for the pod you just created and verify the image version is updated
kubectl set image pod/nginx nginx=nginx:1.15-alpine
kubectl describe po nginx
// another way it will open vi editor and change the version
kubeclt edit po nginx
kubectl describe po nginx
Change the Image version back to 1.17.1 for the pod you just updated and observe the changes
kubectl set image pod/nginx nginx=nginx:1.17.1
kubectl describe po nginx
kubectl get po nginx -w # watch it
Check the Image version without the describe command
kubectl get po nginx -o jsonpath=’{.spec.containers[].image}{“\n”}’
Create the nginx pod and execute the simple shell on the pod
// creating a pod kubectl run nginx --image=nginx --restart=Never
// exec into the pod kubectl exec -it nginx /bin/sh
Get the IP Address of the pod you just created
kubectl get po nginx -o wide
Create a busybox pod and run command ls while creating it and check the logs
kubectl run busybox –image=busybox –restart=Never – ls
kubectl logs busybox
If pod crashed check the previous logs of the pod
kubectl logs busybox -p
Create a busybox pod with command sleep 3600
kubectl run busybox –image=busybox –restart=Never – /bin/sh -c “sleep 3600”
Check the connection of the nginx pod from the busybox pod
kubectl get po nginx -o wide
// check the connection kubectl exec -it busybox -- wget -o-
Create a busybox pod and echo message ‘How are you’ and delete it manually
kubectl run busybox –image=nginx –restart=Never -it – echo “How are you”
kubectl delete po busybox
Create a busybox pod and echo message ‘How are you’ and have it deleted immediately
// notice the –rm flag
kubectl run busybox –image=nginx –restart=Never -it –rm – echo “How are you”
Create an nginx pod and list the pod with different levels of verbosity
// create a pod kubectl run nginx --image=nginx --restart=Never --port=80
// List the pod with different verbosity kubectl get po nginx --v=7 kubectl get po nginx --v=8 kubectl get po nginx --v=9
- List the nginx pod with custom columns POD_NAME and POD_STATUS26. List the nginx pod with custom columns POD_NAME and POD_STATUS
kubectl get po -o=custom-columns=”POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state”
- List all the pods sorted by name
kubectl get pods –sort-by=.metadata.name
- List all the pods sorted by created timestamp
kubectl get pods–sort-by=.metadata.creationTimestamp
- Create a Pod with three busy box containers with commands “ls; sleep 3600;”, “echo Hello World; sleep 3600;” and “echo this is the third container; sleep 3600” respectively and check the status
// first create single container pod with dry run flag kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml
// edit the pod to following yaml and create it
kubectl create -f multi-container.yaml
kubectl get po busybox
Via API
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: busybox name: busybox spec: containers: - args: - bin/sh - -c - ls; sleep 3600 image: busybox name: busybox1 resources: {} - args: - bin/sh - -c - echo Hello world; sleep 3600 image: busybox name: busybox2 resources: {} - args: - bin/sh - -c - echo this is third container; sleep 3600 image: busybox name: busybox3 resources: {} dnsPolicy: ClusterFirst restartPolicy
- Check the logs of each container that you just created in question 29…below
// first create single container pod with dry run flag kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml
// edit the pod to following yaml and create it
kubectl create -f multi-container.yaml
kubectl get po busybox
kubectl logs busybox -c busybox1
kubectl logs busybox -c busybox2
kubectl logs busybox -c busybox3
- Check the previous logs of the second container busybox2 if any
kubectl logs busybox -c busybox2 –previous
- Run command ls in the third container busybox3 of the above pod
kubectl exec busybox -c busybox3 – ls
- Show metrics of the above pod containers and puts them into the file.log and verify
// first create single container pod with dry run flag kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml
// edit the pod to following yaml and create it
kubectl create -f multi-container.yaml
kubectl get po busybox
kubectl top pod busybox –containers
// putting them into file
kubectl top pod busybox –containers > file.log
cat file.log
- Create a Pod with main container busybox and which executes this “while true; do echo ‘Hi I am from Main container’»_space; /var/log/index.html; sleep 5; done” and with sidecar container with nginx image which exposes on port 80. Use emptyDir Volume and mount this volume on path /var/log for busybox and on path /usr/share/nginx/html for nginx container. Verify both containers are running.
// create an initial yaml file with this kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml // edit the yml as below and create it kubectl create -f multi-container.yaml kubectl get po multi-cont-pod
API Version:
apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: multi-cont-pod name: multi-cont-pod spec: volumes: - name: var-logs emptyDir: {} containers: - image: busybox command: ["/bin/sh"] args: ["-c", "while true; do echo 'Hi I am from Main container' >> /var/log/index.html; sleep 5;done"] name: main-container resources: {} volumeMounts: - name: var-logs mountPath: /var/log - image: nginx name: sidecar-container resources: {} ports: - containerPort: 80 volumeMounts: - name: var-logs mountPath: /usr/share/nginx/html dnsPolicy: ClusterFirst restartPolicy: Never status: {}
- Exec into both containers and verify that main.txt exist and query the main.txt from sidecar container with curl localhost
// exec into main container
kubectl exec -it multi-cont-pod -c main-container – sh
cat /var/log/main.txt
// exec into sidecar container
kubectl exec -it multi-cont-pod -c sidecar-container – sh
cat /usr/share/nginx/html/index.html
// install curl and get default page
kubectl exec -it multi-cont-pod -c sidecar-container – sh
# apt-get update && apt-get install -y curl
# curl localhost
- Get the pods with label information
kubectl get pods –show-labels
- Create 5 nginx pods in which two of them is labeled env=prod and three of them is labeled env=dev
kubectl run nginx-dev1 –image=nginx –restart=Never –labels=env=dev
kubectl run nginx-dev2 –image=nginx –restart=Never –labels=env=dev
kubectl run nginx-dev3 –image=nginx –restart=Never –labels=env=dev
kubectl run nginx-prod1 –image=nginx –restart=Never –labels=env=prod
kubectl run nginx-prod2 –image=nginx –restart=Never –labels=env=prod
- Verify all the pods are created with correct labels
kubeclt get pods –show-labels
- Get the pods with label env=dev
kubectl get pods -l env=dev
- Get the pods with label env=dev and also output the labels
kubectl get pods -l env=dev –show-labels
- Get the pods with label env=prod
kubectl get pods -l env=prod
- Get the pods with label env=prod and also output the labels
kubectl get pods -l env=prod –show-labels
- Get the pods with label env
kubectl get pods -L env
- Get the pods with labels env=dev and env=prod
kubectl get pods -l ‘env in (dev,prod)’
- Get the pods with labels env=dev and env=prod and output the labels as well
kubectl get pods -l ‘env in (dev,prod)’ –show-labels
- Change the label for one of the pod to env=uat and list all the pods to verify
kubectl label pod/nginx-dev3 env=uat –overwrite
kubectl get pods –show-labels
- Remove the labels for the pods that we created now and verify all the labels are removed
kubectl label pod nginx-dev{1..3} env-
kubectl label pod nginx-prod{1..2} env-
kubectl get po –show-labels
- Let’s add the label app=nginx for all the pods and verify
kubectl label pod nginx-dev{1..3} app=nginx
kubectl label pod nginx-prod{1..2} app=nginx
kubectl get po –show-labels
- Get all the nodes with labels (if using minikube you would get only master node)
kubectl get nodes –show-labels
- Label the node (minikube if you are using) nodeName=nginxnode
kubectl label node minikube nodeName=nginxnode
- Create a Pod that will be deployed on this node with the label nodeName=nginxnode
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml // add the nodeSelector like below and create the pod kubectl create -f pod.yaml
API Version: apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: nginx name: nginx spec: nodeSelector: nodeName: nginxnode containers: - image: nginx name: nginx resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {}
- Verify the pod that it is scheduled with the node selector
kubectl describe po nginx | grep Node-Selectors
- Verify the pod nginx that we just created has this label
kubectl describe po nginx | grep Labels
- Annotate the pods with name=webapp
kubectl annotate pod nginx-dev{1..3} name=webapp
kubectl annotate pod nginx-prod{1..2} name=webapp
- Verify the pods that have been annotated correctly
kubectl annotate pod nginx-dev{1..3} name=webapp
kubectl annotate pod nginx-prod{1..2} name=webapp
kubectl describe po nginx-dev{1..3} | grep -i annotations
kubectl describe po nginx-prod{1..2} | grep -i annotations
- Remove the annotations on the pods and verify
kubectl annotate pod nginx-dev{1..3} name-
kubectl annotate pod nginx-prod{1..2} name-
kubectl describe po nginx-dev{1..3} | grep -i annotations
kubectl describe po nginx-prod{1..2} | grep -i annotations
- Remove all the pods that we created so far
kubectl delete po –all
- Create a deployment called webapp with image nginx with 5 replicas
kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml // change the replicas to 5 in the yaml and create it kubectl create -f webapp.yaml
API Version:
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: webapp name: webapp spec: replicas: 5 selector: matchLabels: app: webapp strategy: {} template: metadata: creationTimestamp: null labels: app: webapp spec: containers: - image: nginx name: nginx resources: {} status: {}
- Get the deployment you just created with labels
kubectl get deploy webapp –show-labels
- Output the yaml file of the deployment you just created
kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml // change the replicas to 5 in the yaml and create it kubectl create -f webapp.yaml
kubectl get deploy webapp -o yaml
- Get the pods of this deployment
kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml // change the replicas to 5 in the yaml and create it kubectl create -f webapp.yaml
// get the label of the deployment kubectl get deploy --show-labels // get the pods with that label kubectl get pods -l app=webapp
- Scale the deployment from 5 replicas to 20 replicas and verify
kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml // change the replicas to 5 in the yaml and create it kubectl create -f webapp.yaml
kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp
- Get the deployment rollout status
kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp
kubectl rollout status deploy webapp
- Get the replicaset that created with this deployment
kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp
kubectl get rs -l app=webapp
- Get the yaml of the replicaset and pods of this deployment
kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp
kubectl get rs -l app=webapp -o yaml
kubectl get po -l app=webapp -o yaml