Kubernetes – CKAD Tips

Certified Kubernetes Application Developer (CKAD)

CKAD is an online tests with problems to be solved by a command line tool. Here are the tips of the test.

Links


[1] Clusters and Nodes

  • There are several cluster environment for the exam.

You need to be in a correct cluster. Set the configuration context

kubectl config use-context k8s

You can access each node via ssh

ssh k8s-node-0
sudo -i # root

[2] Create or Apply

# create
kubectl run <pod-name> --image=nginx --restart=Never
kubectl get pods

# apply = create and update
kubectl run <pod-name> --image=nginx --restart=Never \
 --dry-run=client -o yaml > pod.yaml
kubectl apply -f pod.yaml

# use --save-config 
# when you want to use 'kubectl apply' in the future
kubectl create -f pod.yaml --save-config

Here are different ways to create a pod quickly.

# with namespace
kubectl create ns myns
kubectl run busybox --image=busybox --restart=Never -n myns

# run command
kubectl run busybox --image=busybox --restart=Never --command -it -- env
kubectl run busybox --image=busybox --restart=Never --command -it -- echo 'Hello World'

# expose traffic on port
kubectl run nginx --image=nginx --restart=Never --port=80
kubectl get pod nginx -o yaml | grep -i 'containerport'

# env values
kubectl run nginx --image=nginx --restart=Never --env=key1=val1 --env=key2=val2
kubectl exec nginx -it -- env

# labels
kubectl run nginx --image=nginx --restart=Never --labels="app=web,env=prod"
kubectl get pods --show-labels

[3] Create the definition file via –dry-run flag

kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml > mypod.yaml
vim mypod.yaml
kubectl apply -f mypod.yaml
kubectl get pods
kubectl create service nodeport nginx-service --tcp=80:80 --node-port=30080 --dry-run=client -o yaml > my-service.yaml
vim my-service.yaml
kubectl apply -f my-service.yaml
kubectl get services

[4] Getting the definition file from the existing pod

kubectl get pod nginx -o yaml > pod.yaml

[5] Updating Deployments

  • Rolling Update and Rollback
kubectl create deployment nginx-deployment \
  --image=nginx:1.16.1 --dry-run=client \
  -o yaml > my-deployment.yaml

vim my-deployment.yaml # set replicas=2
kubectl apply -f my-deployment.yaml

kubectl get deployments
kubectl get pods

# updating the image
kubectl set image deploy/nginx-deployment nginx=nginx:1.9.1

# check the image is updated
kubectl describe deployment nginx-deployment
kubectl get deployment nginx-deployment -o yaml

kubectl get pods
kubectl describe pod <pod-name> | grep 'Image'

# Checking the rollout history
kubectl rollout status deploy/nginx-deployment
kubectl rollout history deploy/nginx-deployment

# Rollback
kubectl rollout undo deploy/nginx-deployment

kubectl rollout status deploy/nginx-deployment
kubectl rollout history deploy/nginx-deployment

kubectl get pods
kubectl describe pod <pod-name> | grep 'Image'

[6] Executing Commands in a Pod

# Execute the command in a pod
kubectl exec -it <pod-name> -- <command>

# Find out who runs the pod
kubectl exec <pod-name> -- whoami

[7] Change the pod image

kubectl get pods

# get the container name
kubectl describe pod <pod-name>

# change the container image
kubectl set image pod/<pod-name> <container-name>=<image-name>

kubectl describe pod <pod-name>

[8] Working with Labels

# add
kubectl label pods --all env=prod 
kubectl label pod <pod-name> mode=v1

# update
kubectl label pod <pod-name> --overwrite app=v2

# remove
kubectl label pod <pod-name> <key>-
kubectl label pod mypod mode-
# display resources withs all labels
kubectl get pods --show-labels
kubectl get nodes --show-labels

# display pods with the label
# -L, --label-columns=[k1,k2,...]
kubectl get pods -L <label-key>,<label-key>
kubectl get pods -L mode

# label selector
# -l, --selector=[k1=v1,k2=v2,...]
kubectl get pods -l <key>=<value>
kubectl get pods -l app=v2

[9] Working with Annotations

# add
kubectl annotate pods --all license=free 
kubectl annotate pod <pod-name> <key>=<value>
kubectl annotate pod mypod user=john

# update
kubectl annotate pod <pod-name> --overwrite <key>=<value>
kubectl annotate pod mypod --overwrite license=paid

# remove
kubectl annotate pod <pod-name> <key>-
kubectl annotate pod mypod license-

[10] Jobs and CronJobs

# creating 
kubectl create job <name> --image=<name> -- <command>
kubectl create job pi --image=perl \ 
 -- perl -Mbignum=bpi -wle 'print bpi(2000)'

# wait until it is done
kubectl get jobs
kubectl describe job <job-name>

# get the pod-name 
kubeclt get pods

# get the output
kubectl logs <pod-name>

[11] ConfigMaps

# create from literal
kubectl create cm config1 --from-literal=k1=v1

# create from a file
echo -e "k1=v1\nk2=v2" > config.txt
cat config.txt
kubectl create cm config2 --from-file=config.txt
kubectl create cm config3 --from-file=newkey=config.txt

# create from an env file
echo -e "k1=v1\nk2=v2" > config.env
cat config.env
kubectl create cm config4 --from-env-file=config.env
kubectl get cm
kubectl describe cm <configmap-name>
kubectl get cm <configmap-name> -o yaml

[12] Secrets

# create from literal
create secret generic secret1 --from-literal=key=value

kubectl get secret
kubectl describe secret <secret-name>

# get the encoded value
kubectl get secret <secret-name> -o yaml
# decode it
echo <encoded-value> | base64 -d


[13] Service Accounts

kubectl create sa <sa-name>

kubectl get sa -A
kubectl get sa <sa-name> -o yaml
kubectl describe sa <sa-name>

kubectl run nginx --image=nginx --restart=Never \
 --serviceaccount=<sa-name>  

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s