CKAD Study Guide
CKAD Study Guide
Certified Kubernetes
Application Developer (CKAD)
Study Guide
Will Boyd
February 2022
Study Guide | Certified Kubernetes Application Developer (CKAD)
Contents
Exploring Volumes 9
Using PersistentVolumes 10
Application Deployment 13
Understanding Deployments 13
Installing Helm 18
Using Helm 18
2
Study Guide | Certified Kubernetes Application Developer (CKAD)
Debugging in Kubernetes 23
Using ServiceAccounts 26
3
Study Guide | Certified Kubernetes Application Developer (CKAD)
Exploring Services 37
4
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
Exam Tips
• Images are files that include all of the software needed to run a
container.
• A Dockerfile defines the contents of an image.
• The docker build command builds an image using a Dockerfile.
FROM nginx:stable
Build a container image from the current directory. The -t flag specifies
the image tag name.
5
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
• Jobs
• CronJob
Exam Tips
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: print
image: busybox:stable
command: ["echo", "This is a test!"]
restartPolicy: Never
backoffLimit: 4
activeDeadlineSeconds: 10
6
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: print
image: busybox:stable
command: ["echo", "This is a test!"]
restartPolicy: Never
backoffLimit: 4
activeDeadlineSeconds: 10
Relevant Documentation
Exam Tips
• A sidecar container performs some task that helps the main container.
• An ambassador container proxies network traffic to and/or from the
main container.
• An adapter container transforms the main container’s output.
In this example, writer is the main container. It writes data to a file. The
sidecar container reads this data from a shared volume and then writes it
to the container log.
7
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: sidecar-test
spec:
containers:
- name: writer
image: busybox:stable
command: ['sh', '-c', 'echo "The writer wrote this!" > /
output/data.txt; while true; do sleep 5; done']
volumeMounts:
- name: shared
mountPath: /output
- name: sidecar
image: busybox:stable
command: ['sh', '-c', 'while true; do cat /input/data.txt;
sleep 5; done']
volumeMounts:
- name: shared
mountPath: /input
volumes:
- name: shared
emptyDir: {}
Relevant Documentation
• Init Containers
Exam Tips
• Init containers run to completion before the main container starts up.
• Add init containers using the initContainers field of the PodSpec.
Init containers execute tasks during the startup phase of a Pod, before the
main container begins to start up.
8
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: init-test
spec:
containers:
- name: nginx
image: nginx:stable
initContainers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'sleep 60']
Exploring Volumes
Relevant Documentation
• Volumes
Exam Tips
• The volumes field in the Pod spec defines details about volumes used
in the Pod.
• The volumeMounts field in the container spec mounts a volume to a
specific container at a specific location.
• hostPath volumes mount data from a specific location on the host
(k8s node).
• hostPath volume types:
• Directory – Mounts an existing directory on the host.
• DirectoryOrCreate – Mounts a directory on the host, and creates
it if it doesn’t exist.
• File – Mounts an existing single file on the host.
• FileOrCreate – Mounts a file on the host, and creates it if it
doesn’t exist.
• emptyDir volumes provide temporary storage that uses the host file
system and are removed if the Pod is deleted.
This example Pod uses a hostPath volume to read data from the host
machine at /etc/hostPath .
9
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: hostpath-volume-test
spec:
restartPolicy: OnFailure
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'cat /data/data.txt']
volumeMounts:
- name: host-data
mountPath: /data
volumes:
- name: host-data
hostPath:
path: /etc/hostPath
type: Directory
Using PersistentVolumes
Relevant Documentation
• Persistent Volumes
Exam Tips
10
Study Guide | Certified Kubernetes Application Developer (CKAD)
A PersistentVolume:
apiVersion: v1
kind: PersistentVolume
metadata:
name: hostpath-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
storageClassName: slow
hostPath:
path: /etc/hostPath
type: Directory
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: hostpath-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 200Mi
storageClassName: slow
11
Study Guide | Certified Kubernetes Application Developer (CKAD)
You can mount a PersistentVolumeClaim within a Pod just like any regular
volume.
apiVersion: v1
kind: Pod
metadata:
name: pv-pod-test
spec:
restartPolicy: OnFailure
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'cat /data/data.txt']
volumeMounts:
- name: pv-host-data
mountPath: /data
volumes:
- name: pv-host-data
persistentVolumeClaim:
claimName: hostpath-pvc
12
Study Guide | Certified Kubernetes Application Developer (CKAD)
Application Deployment
Understanding Deployments
Relevant Documentation
• Deployment
Exam Tips
A Deployment manages a desired state for a set of replica Pods and helps
with the process of rolling out new code.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
13
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
• Updating a Deployment
Exam Tips
You can initiate a rolling update simply by changing the Pod template
configuration (such as the image version). One way to do this is simply by
editing the Deployment, for example, with kubectl edit deployment .
Relevant Documentation
• Deployment
• Service
Exam Tips
14
Study Guide | Certified Kubernetes Application Developer (CKAD)
Deployment strategies like blue/green and canary can help you improve
stability while deploying new code.
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue-deployment
spec:
replicas: 1
selector:
matchLabels:
app: bluegreen-test
color: blue
template:
metadata:
labels:
app: bluegreen-test
color: blue
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:blue
ports:
- containerPort: 80
15
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: apps/v1
kind: Deployment
metadata:
name: green-deployment
spec:
replicas: 1
selector:
matchLabels:
app: bluegreen-test
color: green
template:
metadata:
labels:
app: bluegreen-test
color: green
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:green
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: bluegreen-test-svc
spec:
selector:
app: bluegreen-test
color: blue
ports:
- protocol: TCP
port: 80
targetPort: 80
16
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: apps/v1
kind: Deployment
metadata:
name: main-deployment
spec:
replicas: 3
selector:
matchLabels:
app: canary-test
environment: main
template:
metadata:
labels:
app: canary-test
environment: main
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:1.0.0
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: canary-deployment
spec:
replicas: 1
selector:
matchLabels:
app: canary-test
environment: canary
template:
metadata:
labels:
app: canary-test
environment: canary
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:canary
ports:
- containerPort: 80
17
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Service
metadata:
name: canary-test-svc
spec:
selector:
app: canary-test
ports:
- protocol: TCP
port: 80
targetPort: 80
Installing Helm
Relevant Documentation
• Installing Helm
Exam Tips
Note: Installation of the Helm tool is not part of the CKAD curriculum.
Using Helm
Relevant Documentation
Exam Tips
• Helm Charts are packages that contain all of the resource definitions
needed to get an application up and running in a cluster.
• A Helm Repository is a collection of Charts and a source for browsing
and downloading them.
Update a repository.
18
Study Guide | Certified Kubernetes Application Developer (CKAD)
Install a chart.
19
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
Exam Tips
Relevant Documentation
Exam Tips
20
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: liveness-pod
spec:
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'while true; do sleep 10; done']
livenessProbe:
exec:
command: ['echo', 'health check!']
initialDelaySeconds: 5
periodSeconds: 5
This Pod has both a liveness probe and a readiness probe, both of which
use an http request to check the status of the container.
apiVersion: v1
kind: Pod
metadata:
name: readiness-pod
spec:
containers:
- name: nginx
image: nginx:1.20.1
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 15
periodSeconds: 5
21
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
Exam Tips
When metrics server is installed, you can use kubectl top to view
resource usage data.
Relevant Documentation
• Logging Architecture
Exam Tips
22
Study Guide | Certified Kubernetes Application Developer (CKAD)
Use -c to specify which container to get logs for. This is required if the
Pod has more than one container.
Debugging in Kubernetes
Relevant Documentation
• Troubleshoot Applications
• Application Introspection and Debugging
• Monitoring, Logging, and Debugging
Exam Tips
23
Study Guide | Certified Kubernetes Application Developer (CKAD)
Get Kubernetes API Server logs for kubeadm cluster (must be run on a
control plane node):
24
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
Exam Tips
An example of a CRD:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: beehives.acloud.guru
spec:
group: acloud.guru
names:
plural: beehives
singular: beehive
kind: BeeHive
shortNames:
- hive
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
25
Study Guide | Certified Kubernetes Application Developer (CKAD)
supers:
type: integer
bees:
type: integer
The example CRD would allow you to create objects like this:
apiVersion: acloud.guru/v1
kind: BeeHive
metadata:
name: test-beehive
spec:
supers: 3
bees: 60000
You can interact with custom resources, just like regular Kubernetes
resources, with commands like kubectl get and kubectl describe .
Using ServiceAccounts
Relevant Documentation
Exam Tips
An example ServiceAccount:
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-sa
automountServiceAccountToken: true
26
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: sa-pod
spec:
serviceAccountName: my-sa
containers:
- name: nginx
image: nginx:stable
Relevant Documentation
• Authenticating
• Controlling Access to the Kubernetes API
• Using RBAC Authorization
Exam Tips
27
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: list-pods-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["list"]
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: list-pods-rb
subjects:
- kind: ServiceAccount
name: my-sa
namespace: default
roleRef:
kind: Role
name: list-pods-role
apiGroup: rbac.authorization.k8s.io
Relevant Documentation
Exam Tips
sudo vi /etc/kubernetes/manifests/kube-apiserver.yaml
28
Study Guide | Certified Kubernetes Application Developer (CKAD)
- --enable-admission-
plugins=NodeRestriction,NamespaceAutoProvision
Relevant Documentation
Exam Tips
Resource Limits - Specify an enforced upper limit for resource usage. The
container process will be terminated if it exceeds these limits.
29
Study Guide | Certified Kubernetes Application Developer (CKAD)
A Pod with resource requests and limits for cpu and memory .
apiVersion: v1
kind: Pod
metadata:
name: resources-pod
namespace: resources-test
spec:
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'while true; do echo Running...;
sleep 5; done']
resources:
requests:
memory: 64Mi
cpu: 250m
limits:
memory: 128Mi
cpu: 500m
apiVersion: v1
kind: ResourceQuota
metadata:
name: resources-test-quota
namespace: resources-test
spec:
hard:
requests.memory: 128Mi
requests.cpu: 500m
limits.memory: 256Mi
limits.cpu: "1"
30
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
• ConfigMaps
• Secrets
Exam Tips
An example ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
message: Hello, World!
app.cfg: |
# A configuration file!
key1=value1
key2=value2
31
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Pod
metadata:
name: cm-pod
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'echo $MESSAGE; cat /config/app.cfg']
env:
- name: MESSAGE
valueFrom:
configMapKeyRef:
name: my-configmap
key: message
volumeMounts:
- name: config
mountPath: /config
readOnly: true
volumes:
- name: config
configMap:
name: my-configmap
items:
- key: app.cfg
path: app.cfg
Secrets are similar to ConfigMaps, but are designed to store sensitive data
like passwords or API keys.
When creating a Secret manifest, you must first base64-encode any secret
values.
32
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
sensitive.data: U2VjcmV0IFN0dWZmIQo=
passwords.txt: U2VjcmV0IHN0dWZmIGluIGEgZmlsZSEK
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
restartPolicy: Never
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'echo $SENSITIVE_STUFF; cat /config/
passwords.txt']
env:
- name: SENSITIVE_STUFF
valueFrom:
secretKeyRef:
name: my-secret
key: sensitive.data
volumeMounts:
- name: secret-config
mountPath: /config
readOnly: true
volumes:
- name: secret-config
secret:
secretName: my-secret
items:
- key: passwords.txt
path: passwords.txt
33
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
Exam Tips
apiVersion: v1
kind: Pod
metadata:
name: securitycontext-pod
spec:
containers:
- name: busybox
image: busybox:stable
command: ['sh', '-c', 'while true; do echo Running...;
sleep 5; done']
securityContext:
runAsUser: 3000
runAsGroup: 4000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
34
Study Guide | Certified Kubernetes Application Developer (CKAD)
Relevant Documentation
• NetworkPolicies
• Cluster Networking
Exam Tips
35
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: np-test-a-default-deny-ingress
namespace: np-test-a
spec:
podSelector: {}
policyTypes:
- Ingress
This policy affects only Ingress (incoming) traffic. It allows traffic from
any Pod that meets both of the following criteria:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: np-test-client-allow
namespace: np-test-a
spec:
podSelector:
matchLabels:
app: np-test-server
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
team: bteam
podSelector:
matchLabels:
app: np-test-client
ports:
- protocol: TCP
port: 80
36
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: np-test-client-allow-egress
namespace: np-test-b
spec:
podSelector:
matchLabels:
app: np-test-client
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
team: ateam
ports:
- protocol: TCP
port: 80
Exploring Services
Relevant Documentation
• Service
Exam Tips
37
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: v1
kind: Service
metadata:
name: clusterip-service
spec:
type: ClusterIP
selector:
app: service-server
ports:
- protocol: TCP
port: 8080
targetPort: 80
apiVersion: v1
kind: Service
metadata:
name: nodeport-service
spec:
type: NodePort
selector:
app: service-server
ports:
- protocol: TCP
port: 8080
targetPort: 80
nodePort: 30080
Relevant Documentation
• Ingress
Exam Tips
38
Study Guide | Certified Kubernetes Application Developer (CKAD)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-test-ingress
spec:
ingressClassName: nginx
rules:
- host: ingresstest.acloud.guru
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: ingress-test-service
port:
number: 80
39