Free CKAD Practice Questions

export dr="--dry-run=client"
export ns=default
alias k="kubectl"
alias ka="k apply -f"
alias kc="k create"
alias kr="k run"
alias kg="k get"
alias kdd="k describe"
alias kdx="k delete --force --grace-period=0"
kc ns myns
export ns=myns
kr -n $ns nginx1 --image=nginx:alpine
kg po -n $ns
kdd -n $ns pod nginx1
kr nginx2 --image=nginx --port=8080
kr nginx3 --image=nginx $dr -oyaml > pod3.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx3
name: nginx3
spec:
containers:
- image: nginx
name: ngcontainer
restartPolicy: Never
ka pod3.yaml
kr nginx --image=nginx $dr -oyaml --command -- date > pod4.yaml 
kr nginx --image=nginx $dr -ojson --env=app=webapp > pod5.json
kr nginx6 --image=nginx678
kg po -A
kr bbox7 --image=busybox --restart=Never --rm -it -- sh -c env
k run nginx8 --image=nginx -l version=v1
k label pod nginx8 app=mywebapp
k label pod nginx8 version=v2 --overwrite
kr nginx10 --image=nginx -l=app=webapp --port=8080 --restart=OnFailure --limits='cpu=100m,memory=1024Mi' -- sh -c "echo HelloWorld"
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx11
name: nginx11
spec:
containers:
- image: nginx
name: nginx11
nodeName: minikube
k label node minikube nodename=mynode
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx12
name: nginx12
spec:
containers:
- image: nginx
name: nginx12
nodeSelector:
nodename: mynode
kg pods -o wide
kg nodes -o wide
kr ngtemp1 --image=nginx -l=app=v1
kr ngtemp2 --image=nginx -l=app=v1
kr ngtemp3 --image=nginx -l=app=v2
kr ngtemp4 --image=nginx -l=app=v2
kg po --show-labels
kdx po -l=app=v1
kg po --show-labels
kc deploy cncfapp --image=nginx:alpine --replicas=2 --port=8080 -- sh -c ‘sleep 3600’
k set image deploy cncfapp nginx=nginx:1.17 --record
k scale deploy cncfapp --replicas=5
k rollout history deploy cncfapp
kg po
kdd pod pod-name | grep -i image -A2
k set image deploy cncfapp nginx=nginx:777 --record
kg po
k rollout history deploy cncfapp
k rollout undo deploy cncfapp
kc deploy cloudapp --image=nginx:1.16 --replicas=4 $dr -oyaml > dep18.yaml
spec:
strategy:
type: Recreate
ka dep18.yaml
kg po
kg deploy
k set image deploy cloudapp nginx=nginx:1.17
kg po
kg deploy
k rollout history deploy cloudapp --revision=2
kc deploy onlineapp --image=nginx:1.10 --replicas=4 $dr -oyaml > dep19.yaml
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 100
maxUnavailable: 50
ka dep19.yaml
kg po
kg deploy
k set image deploy onlineapp nginx=nginx:1.11
kg po
kg deploy
k rollout history deploy onlineapp --revision=2
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mywebapp
name: mywebapp
spec:
replicas: 50
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: mywebapp
spec:
containers:
- image: nginx
name: nginx
kc deploy secureapp --image=nginx --replicas=5 $dr -oyaml >21.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: secureapp
name: secureapp
spec:
replicas: 5
selector:
matchLabels:
app: secureapp
template:
metadata:
labels:
app: secureapp
spec:
serviceAccountName: mysecuresa
containers:
- image: nginx
name: nginx
kc sa mysecuresa
ka 21.yaml
kg deployment
kg po
kc job myjob --image=busybox $dr -oyaml -- sh -c ‘echo Hello ; sleep 10 ; echo World ; sleep 20 ; echo DontPrint ‘ > job22.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: myjob
spec:
activeDeadlineSeconds: 30
template:
metadata:
spec:
containers:
- command:
- sh
- -c
- 'echo Hello ; sleep 10 ; echo World ; sleep 20 ; echo DontPrint '
image: busybox
name: myjob
restartPolicy: Never
ka job22.yaml
kg jobs
kg po
kc job job23 --image=busybox $dr -oyaml -- sh -c date > job23.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job23
spec:
completions: 20
parallelism: 5
template:
metadata:
spec:
containers:
- command:
- sh
- -c
- date
image: busybox
name: job23
restartPolicy: Never
ka job23.yaml
k logs jobs/job23
kc cj mycronjob $dr -oyaml --image=busybox --schedule=”*/5 * * * *” -— sh -c date > cj.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
jobTemplate:
metadata:
name: mycronjob
spec:
template:
metadata:
spec:
containers:
- command:
- sh
- -c
- date
image: busybox
name: mycronjob
restartPolicy: OnFailure
schedule: '*/5 * * * *'
startingDeadlineSeconds: 60
ka cj.yaml
kg cj
kg jobs
Fname=Tarun
Lname=Ghai
kc cm cmap --from-file=name.txt
kdd cm cmap
kr nginx25 --image=nginx $dr -oyaml > pod25.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx25
name: nginx25
spec:
volumes:
- name: myvol
configMap:
name: cmap
containers:
- image: busybox
name: nginx25
volumeMounts:
- name: myvol
mountPath: /etc/cmaps
ka pod25.yaml
k exec nginx25 -- cat /etc/cmaps/name.txt
kc secret generic mysecret --from-literal=uname=tarun@111 --from-literal=passwd=welcome@111kc cm mycm --from-literal=username=tarun@222 --from-literal=password=welcome@222kr nginx26 --image=nginx $dr -oyaml > pod26.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx26
name: nginx26
spec:
containers:
- image: nginx
name: nginx26
envFrom:
- configMapRef:
name: mycm
- secretRef:
name: mysecret
ka pod26.yaml k exec nginx26 -- env
kr nginx27 --image=nginx $dr -oyaml > pod27.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx27
name: nginx27
spec:
securityContext:
fsGroup: 5000
containers:
- image: nginx
name: nginx27
securityContext:
runAsUser: 1010
runAsGroup: 2020
capabilities:
add: ["NET_ADMIN"]
ka pod27.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx28
name: nginx28
spec:
securityContext:
runAsUser: 1010
containers:
- image: nginx
name: ngcon1
ports:
- containerPort: 8081
securityContext:
capabilities:
add: ["NET_ADMIN"]
- image: nginx
name: ngcon2
ports:
- containerPort: 8082
securityContext:
capabilities:
add: ["NET_ADMIN"]
- image: nginx
name: ngcon3
ports:
- containerPort: 8083
securityContext:
runAsUser: 2020
capabilities:
add: ["NET_ADMIN"]
kr livenesspod --image=k8s.gcr.io/liveness $dr -oyaml -- /server > liveness.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: livenesspod
name: livenesspod
spec:
containers:
- args:
- /server
image: k8s.gcr.io/liveness
name: livenesspod
livenessProbe:
httpGet:
path: /healthz
port: 8080
ka liveness.yaml 
kg po
kdd pod livenesspod
kr readinesspod --image=busybox $dr -oyaml -- sh -c ‘touch /tmp/healthy; sleep 600’ > readiness.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: readinesspod
name: readinesspod
spec:
containers:
- args:
- sh
- -c
- touch /tmp/healthy; sleep 600
image: busybox
name: readiness
readinessProbe:
exec:
command: ["sh","-c","cat /tmp/healthy"]
initialDelaySeconds: 20
periodSeconds: 5
ka readiness.yaml 
kg po
kdd pod readinesspod
kr nginx31 --image=nginx --port=80 --expose
k expose pod nginx31 --name=service32 --port=8080 --target-port=80
k expose pod nginx31 --name=service33 --port=8080 --target-port=80 --type=NodePort
k expose pod nginx31 --name=service34 --type=NodePort --port=80 $dr -oyaml > svc.yaml
apiVersion: v1
kind: Service
metadata:
name: service34
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
protocol: TCP
nodePort: 30010
ka svc.yaml 
kg svc
kdd svc service34
kc deployment online35 --image=nginx --replicas=2 
k expose deploy online35 --name=service35 --port=80 --type=NodePort
kg svc
kdd svc service35
kr serverapp --image=nginx 
kr clientapp --image=busybox -- sh -c "sleep 600"
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: server-access-policy
namespace: default
spec:
podSelector:
matchLabels:
app: server
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: client
kg po --show-labels
k label pod serverapp app=server
k label pod clientapp app=client
kg po --show-labels
k exec clientapp -it -- sh -c sh
wget serverapp-ipaddress
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
storageClassName: normal
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
kr pvpod --image=busybox $dr -oyaml -- sh -c "sleep 600" > pvpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: pvpod
name: pvpod
spec:
volumes:
- name: mypv
persistentVolumeClaim:
claimName: mypvc
containers:
- image: busybox
name: pvpod1
command: ["sh","-c","sleep 600"]
volumeMounts:
- name: mypv
mountPath: "/opt/path1"
- image: busybox
name: pvpod2
command: ["sh","-c","sleep 600"]
volumeMounts:
- name: mypv
mountPath: "/opt/path2"
k exec pvpod -c pvpod1 -it —- sh -c ‘echo HelloWorld > /opt/path1/file.txt’k exec pvpod -c pvpod2 -it —- sh -c ‘ls -lrt /opt/path2/file.txt’k exec pvpod -c pvpod2 -it —- sh -c ‘cat /opt/path2/file.txt'
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv1
labels:
name: mypv1
spec:
storageClassName: normal
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/opt/data1"
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv2
labels:
name: mypv2
spec:
storageClassName: normal
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/opt/data2"
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv3
labels:
name: mypv3
spec:
storageClassName: normal
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/opt/data3"
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc40
spec:
storageClassName: normal
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
selector:
matchLabels:
name: mypv2
ka https://k8s.io/examples/pods/two-container-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
restartPolicy: Never
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: nginx-container
image: nginx
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
- name: debian-container
image: debian
volumeMounts:
- name: shared-data
mountPath: /pod-data
command: ["/bin/sh"]
args: ["-c", "echo Hello from the debian container > /pod-data/index.html"] | sleep 300
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx-container
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /usr/share/nginx/html
name: shared-data
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-wv2jw
readOnly: true
- args:
- -c
- echo "Release Notes from the debian container" > /pod-data/release.html | sleep 600
command:
k exec two-containers -c debian-container -it -- sh
apt-get update
apt-get install curl
k exec two-containers -c debian-container -it -- sh -c ‘curl NGINX_POD_IP_ADDRESS/release.html’
kc ns mem-examplekubectl apply -f https://k8s.io/examples/pods/resource/memory-request-limit-2.yaml --namespace=mem-example
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-2
namespace: mem-example
spec:
containers:
- name: memory-demo-2-ctr
image: polinux/stress
resources:
requests:
memory: "50Mi"
limits:
memory: "300Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]
kubectl apply -f https://k8s.io/examples/pods/resource/memory-request-limit-3.yaml --namespace=mem-example
resources:
requests:
memory: "1Gi"
limits:
memory: "1Gi"
kc ns cpu-examplekubectl apply -f https://k8s.io/examples/pods/resource/cpu-request-limit-2.yaml --namespace=cpu-example
apiVersion: v1
kind: Pod
metadata:
name: cpu-demo-2
namespace: cpu-example
spec:
containers:
- name: cpu-demo-ctr-2
image: vish/stress
resources:
limits:
cpu: "1"
requests:
cpu: "1"
args:
- -cpus
- "2"
echo -n 'tom' | base64
echo -n 'cruise' | base64
apiVersion: v1
kind: Secret
metadata:
name: mysecret
data:
username: dG9t
password: Y3J1aXNl
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: secret-pod
image: nginx
volumeMounts:
- name: secretvolume
mountPath: /opt/secretvolume
volumes:
- name: secretvolume
secret:
secretName: mysecret
kg po 
k exec -i -t secret-pod -- /bin/bash
#ls /opt/secretvolume
#cat /opt/secretvolume/username
#cat /opt/secretvolume/password
kubectl create secret generic backend-user --from-literal=backend-username='backend-admin'kubectl create secret generic db-user --from-literal=db-username='db-admin'
apiVersion: v1
kind: Pod
metadata:
name: envvars-multiple-secrets
spec:
containers:
- name: envars-test-container
image: nginx
env:
- name: BACKEND_USERNAME
valueFrom:
secretKeyRef:
name: backend-user
key: backend-username
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: db-user
key: db-username
kubectl exec -i -t envvars-multiple-secrets -- /bin/sh -c env
apiVersion: v1
kind: Pod
metadata:
labels:
run: ngpod48
name: ngpod48
spec:
containers:
- image: nginx
name: ngpod1
- image: nginx
name: ngpod2
kdd pod ngpod48
k logs ngpod48 -c ngpod1
k logs ngpod48 -c ngpod2
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: redis
name: redis
spec:
replicas: 4
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- image: redis
imagePullPolicy: Never
name: redis
kr nginx50 --image=nginx -l=role=frontend
kr bbox50 --image=busybox -l=role=db -- sh -c 'sleep 600'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-network-policy
namespace: default
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
- namespaceSelector:
matchLabels:
layer: dbadmin
- namespaceSelector:
matchLabels:
layer: cache
podSelector:
matchLabels:
role: cacheserver

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Coding for iOS 11: How to drag & drop into collections & tables

Define KM Program Governance: Part 1

Planned Downtime

An Interview with Thomas Davis, Author of Backbone Tutorials

What I learned from building Cool Cats NFT

What is Data as a Service?

Unity Dev — Documenting my journey

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tarun Ghai

Tarun Ghai

More from Medium

What is Maven ?

Scenario Analysis of S&P 500 Index

Day 25 — Finland; and its head-scratching language.

How to Map OneDrive to a Drive Letter in Windows 11?