Commands Flashcards

1
Q

List all the namespaces in the cluster

A

kubectl get namespaces

kubectl get ns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List all the pods in all namespaces

A

kubectl get po –all-namespaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List all the pods in the particular namespace

A

kubectl get po -n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List all the services in the particular namespace

A

kubectl get svc -n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List all the pods showing name and namespace with a json path expression

A

kubectl get pods -o=jsonpath=”{.items[*][‘metadata.name’, ‘metadata.namespace’]}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create an nginx pod in a default namespace and verify the pod running

A
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// List the pod
kubectl get po
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create the same nginx pod with a yaml file

A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Output the yaml file of the pod you just created ( nginx)

A

kubectl get po nginx -o yaml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Output the yaml file of the pod you just created without the cluster-specific information (nginx)

A

kubectl get po nginx -o yaml –export

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Get the complete details of the pod you just created (nginx)

A

kubectl describe pod nginx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Delete the pod you just created ( nginx, nginx-pod.yaml)

A

kubectl delete po nginx

kubectl delete -f nginx-pod.yaml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Delete the pod you just created without any delay (force delete) (nginx)

A

kubectl delete po nginx –grace-period=0 –force

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create the nginx pod with version 1.17.4 and expose it on port 80

A

kubectl run nginx –image=nginx:1.17.4 –restart=Never –port=80

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Change the Image version to 1.15-alpine for the pod you just created and verify the image version is updated

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Change the Image version back to 1.17.1 for the pod you just updated and observe the changes

A

kubectl set image pod/nginx nginx=nginx:1.17.1

kubectl describe po nginx

kubectl get po nginx -w # watch it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Check the Image version without the describe command

A

kubectl get po nginx -o jsonpath=’{.spec.containers[].image}{“\n”}’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Create the nginx pod and execute the simple shell on the pod

A
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// exec into the pod
kubectl exec -it nginx /bin/sh
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Get the IP Address of the pod you just created

A

kubectl get po nginx -o wide

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Create a busybox pod and run command ls while creating it and check the logs

A

kubectl run busybox –image=busybox –restart=Never – ls

kubectl logs busybox

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

If pod crashed check the previous logs of the pod

A

kubectl logs busybox -p

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Create a busybox pod with command sleep 3600

A

kubectl run busybox –image=busybox –restart=Never – /bin/sh -c “sleep 3600”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Check the connection of the nginx pod from the busybox pod

A

kubectl get po nginx -o wide

// check the connection
kubectl exec -it busybox -- wget -o-
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Create a busybox pod and echo message ‘How are you’ and delete it manually

A

kubectl run busybox –image=nginx –restart=Never -it – echo “How are you”

kubectl delete po busybox

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Create a busybox pod and echo message ‘How are you’ and have it deleted immediately

A

// notice the –rm flag

kubectl run busybox –image=nginx –restart=Never -it –rm – echo “How are you”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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 ```
26
26. 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"
27
27. List all the pods sorted by name
kubectl get pods --sort-by=.metadata.name
28
28. List all the pods sorted by created timestamp
kubectl get pods--sort-by=.metadata.creationTimestamp
29
29. 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 ```
30
30. 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
31
31. Check the previous logs of the second container busybox2 if any
kubectl logs busybox -c busybox2 --previous
32
32. Run command ls in the third container busybox3 of the above pod
kubectl exec busybox -c busybox3 -- ls
33
33. 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
34
34. Create a Pod with main container busybox and which executes this “while true; do echo ‘Hi I am from Main container’ >> /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: {} ```
35
35. 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
36
36. Get the pods with label information
kubectl get pods --show-labels
37
37. 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
38
38. Verify all the pods are created with correct labels
kubeclt get pods --show-labels
39
39. Get the pods with label env=dev
kubectl get pods -l env=dev
40
40. Get the pods with label env=dev and also output the labels
kubectl get pods -l env=dev --show-labels
41
41. Get the pods with label env=prod
kubectl get pods -l env=prod
42
42. Get the pods with label env=prod and also output the labels
kubectl get pods -l env=prod --show-labels
43
43. Get the pods with label env
kubectl get pods -L env
44
44. Get the pods with labels env=dev and env=prod
kubectl get pods -l 'env in (dev,prod)'
45
45. 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
46
46. 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
47
47. 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
48
48. 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
49
49. Get all the nodes with labels (if using minikube you would get only master node)
kubectl get nodes --show-labels
50
50. Label the node (minikube if you are using) nodeName=nginxnode
kubectl label node minikube nodeName=nginxnode
51
51. 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: {} ```
52
52. Verify the pod that it is scheduled with the node selector
kubectl describe po nginx | grep Node-Selectors
53
53. Verify the pod nginx that we just created has this label
kubectl describe po nginx | grep Labels
54
54. Annotate the pods with name=webapp
kubectl annotate pod nginx-dev{1..3} name=webapp | kubectl annotate pod nginx-prod{1..2} name=webapp
55
55. 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
56
56. 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
57
57. Remove all the pods that we created so far
kubectl delete po --all
58
58. 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: {} ```
59
59. Get the deployment you just created with labels
kubectl get deploy webapp --show-labels
60
60. 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
61
61. 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 ```
62
62. 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
63
63. Get the deployment rollout status kubectl scale deploy webapp --replicas=20 kubectl get po -l app=webapp
kubectl rollout status deploy webapp
64
64. 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
65
65. 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