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
Q

Create an nginx pod and list the pod with different levels of verbosity

A
// 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q
  1. List the nginx pod with custom columns POD_NAME and POD_STATUS26. List the nginx pod with custom columns POD_NAME and POD_STATUS
A

kubectl get po -o=custom-columns=”POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q
  1. List all the pods sorted by name
A

kubectl get pods –sort-by=.metadata.name

28
Q
  1. List all the pods sorted by created timestamp
A

kubectl get pods–sort-by=.metadata.creationTimestamp

29
Q
  1. 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
A
// 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
Q
  1. 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

A

kubectl logs busybox -c busybox1
kubectl logs busybox -c busybox2
kubectl logs busybox -c busybox3

31
Q
  1. Check the previous logs of the second container busybox2 if any
A

kubectl logs busybox -c busybox2 –previous

32
Q
  1. Run command ls in the third container busybox3 of the above pod
A

kubectl exec busybox -c busybox3 – ls

33
Q
  1. 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

A

kubectl top pod busybox –containers
// putting them into file
kubectl top pod busybox –containers > file.log
cat file.log

34
Q
  1. Create a Pod with main container busybox and which executes this “while true; do echo ‘Hi I am from Main container’&raquo_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.
A
// 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
Q
  1. Exec into both containers and verify that main.txt exist and query the main.txt from sidecar container with curl localhost
A

// 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
Q
  1. Get the pods with label information
A

kubectl get pods –show-labels

37
Q
  1. Create 5 nginx pods in which two of them is labeled env=prod and three of them is labeled env=dev
A

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
Q
  1. Verify all the pods are created with correct labels
A

kubeclt get pods –show-labels

39
Q
  1. Get the pods with label env=dev
A

kubectl get pods -l env=dev

40
Q
  1. Get the pods with label env=dev and also output the labels
A

kubectl get pods -l env=dev –show-labels

41
Q
  1. Get the pods with label env=prod
A

kubectl get pods -l env=prod

42
Q
  1. Get the pods with label env=prod and also output the labels
A

kubectl get pods -l env=prod –show-labels

43
Q
  1. Get the pods with label env
A

kubectl get pods -L env

44
Q
  1. Get the pods with labels env=dev and env=prod
A

kubectl get pods -l ‘env in (dev,prod)’

45
Q
  1. Get the pods with labels env=dev and env=prod and output the labels as well
A

kubectl get pods -l ‘env in (dev,prod)’ –show-labels

46
Q
  1. Change the label for one of the pod to env=uat and list all the pods to verify
A

kubectl label pod/nginx-dev3 env=uat –overwrite

kubectl get pods –show-labels

47
Q
  1. Remove the labels for the pods that we created now and verify all the labels are removed
A

kubectl label pod nginx-dev{1..3} env-
kubectl label pod nginx-prod{1..2} env-
kubectl get po –show-labels

48
Q
  1. Let’s add the label app=nginx for all the pods and verify
A

kubectl label pod nginx-dev{1..3} app=nginx
kubectl label pod nginx-prod{1..2} app=nginx
kubectl get po –show-labels

49
Q
  1. Get all the nodes with labels (if using minikube you would get only master node)
A

kubectl get nodes –show-labels

50
Q
  1. Label the node (minikube if you are using) nodeName=nginxnode
A

kubectl label node minikube nodeName=nginxnode

51
Q
  1. Create a Pod that will be deployed on this node with the label nodeName=nginxnode
A
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
Q
  1. Verify the pod that it is scheduled with the node selector
A

kubectl describe po nginx | grep Node-Selectors

53
Q
  1. Verify the pod nginx that we just created has this label
A

kubectl describe po nginx | grep Labels

54
Q
  1. Annotate the pods with name=webapp
A

kubectl annotate pod nginx-dev{1..3} name=webapp

kubectl annotate pod nginx-prod{1..2} name=webapp

55
Q
  1. 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

A

kubectl describe po nginx-dev{1..3} | grep -i annotations

kubectl describe po nginx-prod{1..2} | grep -i annotations

56
Q
  1. Remove the annotations on the pods and verify
A

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
Q
  1. Remove all the pods that we created so far
A

kubectl delete po –all

58
Q
  1. Create a deployment called webapp with image nginx with 5 replicas
A
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
Q
  1. Get the deployment you just created with labels
A

kubectl get deploy webapp –show-labels

60
Q
  1. 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
A

kubectl get deploy webapp -o yaml

61
Q
  1. 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
A
// get the label of the deployment
kubectl get deploy --show-labels
// get the pods with that label
kubectl get pods -l app=webapp
62
Q
  1. 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
A

kubectl scale deploy webapp –replicas=20

kubectl get po -l app=webapp

63
Q
  1. Get the deployment rollout status

kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp

A

kubectl rollout status deploy webapp

64
Q
  1. Get the replicaset that created with this deployment

kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp

A

kubectl get rs -l app=webapp

65
Q
  1. Get the yaml of the replicaset and pods of this deployment

kubectl scale deploy webapp –replicas=20
kubectl get po -l app=webapp

A

kubectl get rs -l app=webapp -o yaml

kubectl get po -l app=webapp -o yaml