- Creating a resource
- Namespace
- Pods
- Deployment
- Service
- Nodes
- Monitoring
- Rollout and Versioning
- Jobs and CronJobs
- ConfigMap
- Service Account
- Network Policy
- Storage
- Volumes
- Resource Quota
- Temporary containers
I'd be assuming my favourite alias is already set
alias k=kubectl
In some cases you would need to update the yaml manifest definition further so for some example below you will see the
--dry-run=client -o yaml
added. It's not entirely required, some tasks you can run immediately without needing a yaml file to update. Read the question carefully and decide what's the quickest best route.
-
k apply -f manifest-file.yaml
Creates the resource defined in the manifest file -
k apply -n <name-space> -f ingress.yaml
Creates the resource defined in the manifest file but in thename-space
namespace.
-
k get ns --no-headers | wc -l
If you ever need to know how many namespaces there currently are -
k create ns dev
Quickly create a namespace calleddev
. -
k config set-context $(k config current-context) --namespace=dev
Quickly switch todev
namespace. Actually didn't have to do this during my exam but had it committed to memory just incase. I always used the-n
flag where namespace was involved.
-
k get po --no-headers | wc -l
If you need to know the total number of pods. You can add the-n
flag if looking at a particular namespace -
k get po -A
Gets all Pods in all namespaces. Very handy if you need to find a faulty podk get po -n <name-space>
Gets all Pods in a particular namespace. By looking at theREADY
column orSTATUS
you can tell if something is not right.k get po -o wide
As above but with more details about the pods e.g. it'sIP
,NODE
also stats likeSTATUS
andREADY
info can tell you what node the pod resides, its ip etc. -
k run mypod --image=nginx --namespace=dev --dry-run=client -o yaml
or
k run mypod --image=nginx -n=dev --dry-run=client -o yaml
Create a namespace specific manifest file for a pod -
k explain pod --recursive | grep envFrom -A3
Quickly get manifest information about theenvFrom
property for a Pod spec.Note: you can use this for any other K8s object. e.g
k explain cronjob --recursive | grep spec -A5
.
To view more lines add a higher number to the-A
flag -
k -n <ns> exec -it <pod-name> -- cat /log/app.log
View the contents of the file/log/app.log
of the podpod-name
in thens
namespace. -
k -n <ns> logs <pod-name>
View the logs ofpod-name
-
k -n <ns> logs <pod-name> -c nginx-container
View the logs ofpod-name
for the container callednginx-container
. Handy when you have a multi-container pod.If you have a multi-container pod and can't remember the exact container name just run the first variation. K8s would complain and list the names of the containers that exists in the pod and ask you to pick one.
I've found this a faster way than looking at the manifest file or running the
describe
command though there are other quick ways as well such as usingdescribe
in combination withgrep
. This was good enough for me. -
k get po -l env=prod,tier=frontend
A pod selector based on multiple labels -
k get po -l 'team in (dev, prod)' --show-label
Similar to above, but in this case, it lists pods that have a label calledteam
withdev
andprod
as values and shows the labels as well in the output -
k get po --show-labels
Lists pods with their labels. Can be used with the-A
combination when doing a larger search and or in combination with the namespace option-n <ns>
to limit results. -
k label po <pod-name> env-
Removes a label calledenv
on the pod calledpod-name
-
k get all -n <name-space> -l key=value
Get all resources in a particular namespace limited by the labelk get all -n <name-space>
Dumps all resource detail to your terminal.
You can add the--no-header
flag if you want to do some counting by piping togrep
. -
k run backend --image=nginx --serviceaccount=<sa-name>
When you need to create a pod with a giving service account name. You can combine this with the--dry-run=client -o yaml
to generate a manifest file to further update if required. You at are sure this bit is covered. -
k run app --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'while true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskusage.log; sleep 5; done;'
Super quick command to create a manifest file set with default command to run. Depending on the requirement you may not even need the--dry-run=client -o yaml
and can create to container straight away, understanding the expectation helps.Also if you need to pass this definition to a file for further updating which might likely be the case then the
> manifest-file.yaml
comes at the end.
e.g.
k run app --image=busybox --dry-run=client -o yaml -- /bin/sh -c 'while true; do echo "$(date) | $(du -sh ~)" >> /var/logs/somefile.txt; sleep 5; done;' > manifest-file.yaml
Don't add it after the
--dry-run=client -o yaml
as your fingers (brain) may be used to already. -
k exec mypod -c <pod-container> -it -- /bin/sh
Log into a particular container calledpod-container
on themypod
pod -
k exec <pod-name> -it -- env
Quickly list the pods environment variables. Fast way to check the existence of a value or verify that your config mapping or env variables have been set correctly. -
k run mypod --image=ngnix --labels=env=prod,team=dev --dry-run=client -o yaml
Creates a pod manifest file with two labelsenv
andteam
set. -
k set image po/mypod pod-container=nginx:latest
Quickly update a pods image. -
k annotate po.mypod contact='Felix',commit='34df3245' --overwrite=true
Annotates a pod calledmypod
with the provided values and overwrite if already exists
Notice how you can use thepo/mypod
,po.mypod
orpo mypod
variation from the examples above -
k run nginx --image=nginx --restart=Never --env=var1=val1
Creates a pod with environment variable set. Saves you having to edit a yaml file or wad through documentation. Depending on the task this may event be sufficient. -
k run nginx arg1 arg2 --image=nginx --restart=Never --labels=app=v1 --dry-run=client -o yaml
Creates a pod with two arguments and labels. Timesaver and you can output to a definition file and edit further. -
k run nginx --image=nginx --restart=Never --requests='cpu=100m,memory=256Mi' --limits='cpu=200m,memory=512Mi'
Creates a pod with resource requirement already set. Let wading around through documentation. You dan output using--dry-run=client -o yaml
to edit further if required. -
k delete po/<my-pod> --grace-period=0 --force
If you need to delete a pod always add the--grace-period=0
flag else you'd have to wait a few precious seconds for the default wait time. There are a few aliases to this out there i had seen but i was comfortable typing this all out.
This actually came in handy for me during the exam as i had created a pod in the wrong namespace, when i realised i quickly removed it with this command and recreated the pod in the expected namespace.
-
k run myapp --image=nginx --port=80 --expose
Creates a service and a pod calledmyapp
on port 80 -
k create deploy <deployment-name> --image=nginx --replicas=3 --dry-run=client -o yaml
Quickly create a deployment calledmydeploy
with a pod running thenginx
image and 3 replicas. You may need to update the manifest file further or this could suffice depending on the task at hand. -
k get deploy <deployment-name> -o yaml
Gets the deployment details in yaml format. You can redirect to a file if need be if you want to edit the deployment. Note Getting K8s object details is now really verbose due to additions of amanagedFields
property, output would not be as neat as you may see in some tutorials. -
k edit deploy/<deployment-name>
Quick way to edit an existing deployment straight from the terminal. Once you save and exit the editor in the terminal K8s would apply your changes. -
k describe deploy <deployment-name> | grep -i containers -A2
A quick way to list out a deployments containers. The-i
flag for thegrep
command is for case insensitivity and-A2
says give me just two lines, which should include the container, it's name and it's image. -
k set image deploy/<deployment-name> nginx-container=nginx:latest
Quickly update a deployments image on the fly for the container callednginx-container
to the latestnginx
image. -
k expose deploy mydeploy --name=<service-name> --target-port=8080 --type=NodePort --port=8080 --dry-run=client -o yaml
Exposes a deployment calledmydeploy
by creating a service calledservice-name
of typeNodePort
. Can be outputted to a file or run straight in the terminal depending on the task at hand. Omit the--dry-run=client -o yaml
section if you want to run immediately. -
k expose deploy -n <name-space> mydeploy --type=NodePort --port=80 --name=service-name --dry-run -o yaml > manifest-file.yaml
Same as above but namespace specific. Note the--dry-run -o yaml > manifest-file.yaml
is optional depending on the task at hand. -
k scale deploy/<mydeploy> --replicas=5
Quick command to add more replicas for a deployment calledmydeploy
. No need to update the deployment manifest file then apply it if you are faced with this sort of task. -
k autoscale deployment foo --min=2 --max=10
Quickly create an autoscale for a deployment.
Have a look at this command for optionsk autoscale -h
. It might just save you some time.
-
k create svc nodeport my-service --tcp=8080:80 --dry-run=client -o yaml
Creates a service of type NodePort withport
set to8080
andtargetPort
set to80
.
Tip: Runk create svc -h
for some help if unsure -
k create svc clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
Same as above but for a ClusterIP type service. When omitted this is the default service type. -
k expose po <pod-name> --port=6379 --name <service-name> --dry-run=client -o yaml
Creates a service calledservice-name
and exposes the pod calledpod-name
on the specified port. This particular command would set both the serviceport
andtargetPort
to6379
. Might be what the task requires, if not you can quickly run this commandk expose -h
for a plethora of examples to pick from. -
k expose po <pod-name> --port=6379 --target-port=6379 --name=service-name
Same as above but this time we want to specify a port and a targetPort which is the port the pods are listening on. -
k run mypod --image=nginx --port=80 --expose --dry-run=client -o yaml
Quickly create a service and pod exposed by the service. Nice if the pod does not already exist and you are asked to create a pod and then create a service to expose the pod. This one-liner would be massive time saver in such a situation. -
k expose deploy mydeploy --port=80 --target-port=8000 --dry-run=client -o yaml
Quick command which creates a service for themydeploy
deployment
-
k taint nodes <node-name> key=value:NoSchedule
Quickly taint a node with theNoSchedule
option. No need to wad through documentation to figure this out. -
k describe node <node-name> | grep -i taint
Quickly find the taints that exists on a node callednode-name
. -
k taint node <node-name> <taint-name>-
Quickly removes a taint calledtaine-name
on the node callednode-name
. -
k label nodes <node-name> <label-key>=<label-value>
Quickly add a label to a node callednode-name
. -
k get nodes <node-name> --show-labels
Lists the node callednode-name
with its labels. Omitting the node name would list all nodes labels in the current namespace.
-
k top node
If you need to findout resource consumption for all nodes. e.g. to determine from the list of node which one is using memory or cpu, the colums section from this output contains the info you would need. -
k top node <node-name>
Same as above but specific to the specified node. -
k top pod
Similar to above but for examining pod resource usage. This is useful if you have a list of running pod and want to see which is using the most resource. It can be combined with the-n
option to be more namespace specific. -
k top pod <pod-name>
Same as above but specific to the specified pod.
-
k rollout undo deploy <deployment-name>
Quickly undo the most recent change to the deployment calleddeployment-name
. -
k rollout history deploy <deploy-name>
List current deployment revisions for thedeploy-name
deployment. -
k rollout undo deploy/<deployment-name> --to-revision=<revision-number>
Quickly undo a recent deployment change to a specific previous deployment revision -
k rollout status deploy <deploy-name>
Gets you the current rollout status info for the deployment calleddeploy-name
. -
k rollout history deploy <deploy-name>
Gets you the rollout history for the deployment.
-
k create cronjob my-cron-job --schedule="*/1 * * * *" --image=busybox --dry-run=client -o yaml
Quickly create a cronjob that is defined to run every minute using the busybox image` -
k create cronjob my-cron-job --image=busybox --schedule="*/1 * * * *" --dry-run=client -o yaml -- /bin/sh -c 'echo "Current date: $(date)"'
Same as above but in this example, we specify the command for the container as part of options in the terminal. If the required command based on the exam question is simple enough you can execute this straight away without the--dry-run=client -o yaml
option as in this example. -
k create job my-job --image=busybox --dry-run=client -o yaml
Quickly create a job definition using the busybox image
Note Have a play with these commands for some insightful info
create cronjob -h
andcreate job -h
-
k create cm <cm-name> --from-literal=key=value
Quickly creates a configmap calledcm-name
with valueskey=value
No need to waste time looking through the documentation, this is your go-to and would save you time 🤓.Note: You can add many
--from-literal=key=value
just make sure there's a space between them -
k create cm <cm-name> -n <name-space>
Same as above but in the namespace specified -
k create configmap db-config --from-env-file=config.txt
If creating from a file.
k create sa <sa-name> -n <name-space>
Quickly creates a service account calledsa-name
in the namespace calledname-space
.
k describe netpol <policy-name>
Get quick info about the networkpolicy. Comes in handy when troubleshooting connectivity issues with pods.
k get sc
Display available storage information
-
k get pv
Displays available persistentvolumes -
k get pvc
Displays available persistentvolume claims available.
Note: ps
's and pvc
's don't have imperative commands that can be used to quickly create them at this time. This is one of those places where you need to be armed with your K8s reference documentation and make sure you have relevant sections bookmarked for quick access. Another area without direct imperative options at the moment is around ingress and egress, you'd have to use the documentation.
🧨 Make sure when using the documentation examples your storageClassName
names match. If asked to specify a particular storage class name in your pv
manifest file then make sure that matches the definition in the pvc
also. This fact can get lost or omitted while copy-pasting from one place to another.
k create quota myrq --hard=cpu=1,memory=1G,pods=2 --dry-run=client -o yaml
Quickly creates a ResourceQuota calledmyrq
with the provided resource requirements. You can runk create quota -h
for more options when faced with this kind of question or if documentation does not cut it for you.
k run tmp --image=busybox --restart=Never -it --rm -- wget -O- 10.109.67.191:80
You can quickly create temporary containers as in this example command that creates a pod using the busybox image thenwget
data from another pod in the cluster. The-it
gives you a shell the--rm
removes the container once you exit the shell. This is usually useful when you want to get data from pods that are part of a ClusterIP service or test if pods are running.
Keep this knowledge in your tool set, you may need it.
Play with these commands over and over again and use them in your practice tests when the need presents itself, they would become second nature very quickly. I found that once you've covered the K8s CKAD exam Domains & Competencies scope, there would be no strange surprises in the questions, you'd just need to learn to move fast, manage your time and quickly get the info you need for the task at hand.
I hope these come in handy and most importantly come back to memory if/when you need them during your exam.
You can continue reading up on some CKAD exam preparation tips and resources that i found helpful here.
Wishing you success in your exams 🙏🏽