Certified Kubernetes Application Developer (CKAD) Tips and Tricks

Outline


  • Kubernetes Resources
  • Vim Cheatsheet
  • Kubectl Imperative Command Complete Guide

Kubernetes Resources


Here are the resources I used for exam preparation:

Vim Cheatsheet


Vim Settings

You would have to write or edit YAML files during the exam. The following settings help in tab/spaces and other related syntax formatting.

vim ~/.vimrc
set nu
set expandtab
set shiftwidth=2
set tabstop=2

Useful Vim Commands

These commands will help you quickly navigate the yaml files, copy, edit, or delete.

Shift + A - go to the end of the current row (insert mode)
Shift + C - delete everything after the cursor (insert mode)
Shift + I - go to the first letter on the current row (insert mode)
Shift + G - go to the last row of data in the file
gg  - go to the first row of data in the file
/Pod - find any instances of Pod in the file
e - jump to the end of the a word
w - jump to the start of the a word
b - jump backwards to the start of the a word
dd - deletes current line
dG - deletes contents from cursor to end of file
ZZ - saves and exit quickly
x - delete character
dd - delete line (cut)
yy - yank line (copy)
ZZ - saves and exit quickly
V - enter visual line
d - delete marked text
y - yank(copy) marked text
> - indent right
> - indent left

Kubectl Imperative Command Complete Guide


In the exam, you only have 2 hours to complete. This exam is primarily about speed. Writing the yaml file from scratch is very time consuming. 

With that in mind, you would want to use imperative commands to create the kubernetes resources. To tackle medium or complex questions, you could generate the yaml files via –dry-run -o yaml flag. Then, you could edit the file via vim, and then create the kubernetes resources.

Quick Primer

Create an alias for kubectl

alias k=kubectl 
alias kdc=“kubectl describe"

Shortcuts for k8s resources.

po for PODs
rs for Replica Sets
deploy for Deployments
svc for Services
ns for Namespaces
netpol for Network Policies
pv for Persistent Volumes
pvc for Persistent Volume Claims
sa for service accounts

To get k8s resources yaml specification

k explain ingress —recursive | less
k explain pod
k explain pod.spec.containers.livenessProbe

To get help on kubectl command

k run --help
k run pod --help
k create deploy --help 

CKAD curriculum includes:

  • Core Concepts – 13%
  • Multi-container Ports – 10%
  • Configuration – 18%
  • Pod Design – 20%
  • Observability – 18%
  • Service & Networking – 13%
  • State Persistence – 8%

We will cover the all the imperative commands for each topic.

Core Concepts

Get namespace

k get all -n mynamespace
k get po --all-namespaces

Generate pod yaml

k run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yaml

Run pod supported commands

k run nginx --image=nginx --restart=Never --port=80 --command 
--serviceaccount=mysa1 --env=HOSTNAME=local 
--labels=bu=finance,env=dev --requests='cpu=100m,memory=256Mi' 
--limits='cpu=200m,memory=512Mi' 
--dry-run -o yaml -- /bin/sh -c 'echo hello world' > nginx.yaml

Create quota

k create quota myrq --hard=cpu=1,memory=1G,pod=2 --dry-run -o yaml

Run temporary pod

k run busybox --image=busybox --rm -it --restart=Never -- wget -O- <pod ip>
k run busybox --image=busybox --rm -it --restart=Never -- /bin/sh -c  ‘wget -O- <pod ip>’

Exec into pod

k exec -it busybox -- /bin/sh
k exec -it busybox -- env
k exec busybox -it -- cp /etc/passwd /etc/foo/passwd

Copy file to local

k cp busybox:/etc/passwd ./passwd

Get resource

k get po -o wide --show-labels 

Multi-container Pods

Connect to the busybox2 container within the multi-container pod

k exec -it busybox -c busybox2 -- /bin/sh

Configuration

Create configmap

k create cm config --from-literal=foo=lala --from-literal=foo2=lolo

Create configmap from file

echo -e "foo3=lili\nfoo4=lele" > config.txt
k create cm configmap2 --from-file=config.txt

Create configmap from env file

echo -e "var1=val1\n# this is a comment\n\nvar2=val2\n#anothercomment" > config.env
kubectl create cm configmap3 --from-env-file=config.env

Requests and Limits

k run nginx --image=nginx --restart=Never --requests='cpu=100m,memory=256Mi' --limits='cpu=200m,memory=512Mi'

Create secret

k create secret generic mysecret --from-literal=password=mypass

Create secret from file

echo -n admin > username
k create secret generic mysecret2 --from-file=username

Get secret value

k get secret mysecret2 -o yaml
echo YWRtaW4K | base64 -d

Show service account from all namespaces

k get sa --all-namespaces

Create service account

k create sa myuser

Pod Design

Set label

k label po nginx1 app=v1

Overwrite label

k label po nginx1 app=v2 --overwrite

Remove label

k label po nginx1 app-

Annotate

k annotate po description='my description'

Create deployment

k create deployment nginx  --image=nginx:1.7.8  --dry-run -o yaml > deploy.yaml
vi deploy.yaml
# change the replicas field from 1 to 2
# add this section to the container spec and save the deploy.yaml file
# ports:
#   - containerPort: 80
k apply -f deploy.yaml

Check roll out status

k rollout status deploy nginx

Update image

k set image deploy nginx nginx=nginx:1.7.9

Check roll out history

k rollout history deploy nginx

Return deployment to previous version

k rollout undo deploy nginx --to-revision=2

Scale deployment replica

k scale deploy nginx --replicas=5

Autoscale deploy

k autoscale deploy nginx --min=5 --max=10 --cpu-percent=80

Create Jobs

k create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'
k create job busybox --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

ActiveDeadline/ Completions / Parallelism

job.spec.activeDeadlineSeconds=30
job.spec.completions=5
job.spec.parallelism=5

Follow the logs

k logs busybox-ptx58 -f

Create Cron Job

k create cronjob busybox --image=busybox --schedule="*/1 * * * *" -- /bin/sh -c 'date; echo Hello from the Kubernetes cluster'

Observability

Logs and grep warning to file

k logs pods -c container | grep WARN > txt

Force delete

k delete po busybox --force --grace-period=0

Get CPU/Memory utilization

kubectl top nodes

Service and Networking

Expose a service

k expose po nginx --name=nginx-service --port=80 --target-port=8000 --type=NodePort

Network policies

Copy and edit from Network Policies

State Persistence

As there is no generator command available, you would need to copy and edit from:

Volume

Configure a Pod to Use a Volume for Storage

Persistent Volume & Persistent Volume Claim

Configure a Pod to Use a PersistentVolume for Storage

Leave A Comment