kubectl - Core Concepts Flashcards
List all namespaces
kubectl get ns
List all pods in all
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
- kubectl run nginx –image=nginx –restart=Never
2. kubectl get po
Create the same nginx pod with a yaml file
- kubectl run nginx –image=nginx –restart=Never –dry-run -o yaml > nginx-pod.yaml
- kubectl create -f nginx-pod.yaml
Output the yaml file of nginx pod
kubectl get po nginx -o yaml
Output yaml of nginx pod without cluster-specific information
kubectl get po nginx -o yaml –export
Get the complete details of the nginx pod
kubectl describe pod nginx
Delete the nginx pod
kubectl delete po nginx
Delete the pod created with example-pod.yaml
kubectl delete -f example-pod.yaml
Force delete nginx pod
kubectl delete po nginx –grace-period=0 –force
Create nginx pod version 1.17.4 and expose port 80
kubectl run nginx –image=nginx:1.17.4 –restart=Never –port=80
Change nginx image to 1.15-alpine and verify
- kubectl set image pod/nginx nginx=nginx:1.15-alpine
2. kubectl describe po nginx
Change nginx pod image to 1.15-alpine but you can’t remember the image name
kubectl edit po nginx
output just the image version of the nginx pod
kubectl get po nginx -o jsonpath=’{.spec.containers[].image}{“\n”}’
Create nginx pod and get to the shell
- kubectl run nginx –image=nginx –restart=Never
2. kubectl exec -it nginx /bin/sh
Get the IP address of the nginx pod
kubectl get po nginx -o wide
Create a busybox pod and run command ls while creating it then check the logs
- kubectl run busybox –image=busybox –restart=Never – ls
2. kubectl logs busybox
The busybox pod crashed, check previous logs
kubectl logs busybox -p
Create busybox pod with command sleep 3600
kubectl run busybox –image=busybox –restart=Never – /bin/sh -c “sleep 3600”
Use the busybox pod to check the connection of the busybox pod
IP=$(kubectl get po nginx -o=jsonpath=’{.status.podIPs[].ip}’); kubectl exec -it busybox – wget -o- $IP
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
kubectl run busybox –image=nginx –restart=Never -it –rm – echo “How are you”
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”
- kubectl run busybox –image=busybox –restart=Never —dry-run=client -o yaml – bin/sh -c “sleep 3600; ls” > multi-container.yaml
- Edit yaml to add the containers
- kubectl create -f multi-container.yaml
Check the logs of each container on busybox pod
kubectl logs busybox -c busybox1
kubectl logs busybox -c busybox2
kubectl logs busybox -c busybox3
Check the previous logs of the second container busybox2
kubectl logs busybox -c busybox2 –previous
Run command ls in the third container busybox3 of the above pod
kubectl exec busybox -c busybox3 – ls
Add pod container metrics to file file.log and verify
- kubectl top pod busybox –container > file.log
2. cat file.log
Ensure /var/logs/main.txt exists in container main-container in pod multi-pod
- kubectl exec -it multi-pod -c main-container – sh
2. cat /var/log/main.txt
Run curl localhost from nginx sidecar container
- kubectl exec -it mult-pod -c sidecar-container – sh
- apt-get udpate && apt-get install -y curl
- curl localhost
Edit label app on nginx1 pod to be v2
kubectl label po nginx1 app=v2 –overwrite
Delete nginx1, nginx2, and nginx3 pods with one command
kubectl delete po nginx{1,2,3}
Create an nginx pod with env variable var1=val1
kubectl run nginx –image=nginx –restart=Never –env=var1=val1