Cka Practice Questions
Cka Practice Questions
Q: Create a job that calculates pi to 2000 decimal points using the container with
the image named perl
and the following commands issued to the container: ["perl", "-Mbignum=bpi", "-
wle", "print bpi(2000)"]
Once the job has completed, check the logs to and export the result to pi-
result.txt.
Solution:
### edit the file, edit the name, remove any ID references and include the command
argument under container spec.
aapiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: pi2000
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- image: perl
name: pi2000
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
resources: {}
restartPolicy: Never
status: {}
kc -f pi2000.yaml
### get the output from the logs and export them to text file
Solution:
sudo kubectl run nginx-deploy --labels="role=webserver,app=nginx" --image=nginx --
replicas=3 --port=80 -o yaml > nginx-deployment.yaml
### use a curl statement that connects to the IP endpoint of the nginx-service and
save the output to a file called output.txt
Solution:
Q. Create a pod called "haz-docs" with an nginx image listening on port 80.
Attach the pod to emptyDir storage, mounted to /tmp in the container.
Connect to the pod and create a file with zero bytes in the /tmp directory called
my-doc.txt.
Solution:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: haz-docs
name: haz-docs
spec:
replicas: 1
selector:
matchLabels:
run: haz-docs
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: haz-docs
spec:
containers:
- image: nginx
name: haz-docs
volumeMounts:
- mountPath: /tmp
name: tmpvolume
ports:
- containerPort: 80
resources: {}
volumes:
- name: tmpvolume
emptyDir: {}
status: {}
root@haz-docs-5b49cb4d87-2lm5g:/# cd /tmp/
root@haz-docs-5b49cb4d87-2lm5g:/tmp# touch my-doc.txt
root@haz-docs-5b49cb4d87-2lm5g:/tmp# ls
my-doc.txt
Solution:
Q. Create a file called counter.yaml in your home directory and paste the
following yaml into it:
Solution:
apiVersion: v1
kind: Pod
metadata:
name: counter
spec:
containers:
- name: count
image: busybox
args: [/bin/sh, -c, 'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep
1; done']
Q.
Create a new namespace called "cloud9".
Create a pod running k8s.gcr.io/liveness with a liveliness probe that uses httpGet
to
probe an endpoint path located at /cloud-health on port 8080.
The httpHeaders are name=Custom-Header and value=Awesome.
The initial delay is 3 seconds and the period is 3.
Solution:
kubectl run web-dep --labels="tier=frontend" --image=nginx --replicas=2 --port=80 -
o yaml > web-dep.yaml
### edit the page to add the annotation in the metadata section:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
annotations:
AppVersion: "3.4"
creationTimestamp: 2019-03-02T18:17:19Z
generation: 1
labels:
tier: frontend
Solution:
Q. Roll the image in use by the web-dep deployment to the previous version.
Do not set the version number of the image explicitly for this command.
Solution:
Solution:
Q. Configure the cluster to use 8.8.8.8 and 8.8.4.4 as upstream DNS servers.
Solution:
The answer can be found at:
https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/
apiVersion: v1
kind: ConfigMap
metadata:
name: kube-dns
namespace: kube-system
data:
stubDomains: |
{"acme.local": ["1.2.3.4"]}
upstreamNameservers: |
["8.8.8.8", "8.8.4.4"]
Solution:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: ultima-dep
namespace: default
spec:
selector:
matchLabels:
app: ultima-app
template:
metadata:
labels:
app: ultima-app
spec:
containers:
- name: pause-pod
image: k8s.gcr.io/pause:2.0
env:
- name: ULTIMA
value: 55.55.58.23
kc -f env-ultima.yaml
Q. Figure out a way to create a pod with 3 replicas using the the nginx container
that can have pods deployed
on a worker node and the master node if needed.
Solution:
Taints: node-role.kubernetes.io/master:NoSchedule
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
tolerations:
- key: "node-role.kubernetes.io/master"
operator: "Equal"
effect: "NoSchedule"
Q. Copy all Kubernetes scheduler logs into a logs directory in your home
directory.
Solution:
gp --namespace=kube-system
Q. Run the pod below until the counter in exceeds 30, export the log file into a
file called counter-log.txt.
Solution:
apiVersion: v1
kind: Pod
metadata:
name: counter
spec:
containers:
- name: count
image: busybox
args: [/bin/sh, -c, 'i=0; while true; do echo "$i: $(date)"; echo "$(date) -
File - $i" >> /var/www/countlog; i=$((i+1)); sleep 3; done']
gl POD > counter-log.txt
Solution:
apiVersion: v1
data:
password.txt: aWhlYXJ0a2l0dGVucw==
username.txt: YWRtaW4=
kind: Secret
metadata:
creationTimestamp: 2019-03-03T00:21:16Z
name: db-user-pass
namespace: default
resourceVersion: "30182"
selfLink: /api/v1/namespaces/default/secrets/db-user-pass
uid: 42b979da-3d4a-11e9-8f41-06f514f6b3f0
type: Opaque
Q.
Create a ConfigMap called web-config that contains the following two entries:
'web_port' set to 'localhost:8080'
'external_url' set to 'reddit.com'
Run a pod called web-config-pod running nginx, expose the configmap settings as
environment variables inside the nginx container.
### this has to be done in several steps, first create configmap from literals on
command line
Name: test-cm
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
external_url:
----
reddit.com
web_port:
----
localhost:8080
Events: <none>
### create the pod deployment yaml and then edit the file
spec:
containers:
- image: nginx
env:
- name: WEB_PORT
valueFrom:
configMapKeyRef:
name: test-cm
key: web_port
- name: EXTERNAL_URL
valueFrom:
configMapKeyRef:
name: test-cm
key: external_url
imagePullPolicy: Always
name: web-config-pod
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
## mysql requires a pv and pvc with the yaml to create them found here:
https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-
application/#deploy-mysql
Q.
Create a pod running k8s.gcr.io/liveness with the following arguments:
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
command:
- cat
- /tmp/healthy
Output the events of the description showing that the container started and then
the health check failed.
### interesting way to quickly spin up busy box pod and run shell temporarily
kubectl run -i --tty --image busybox dns-test --restart=Never --rm /bin/sh
### to get a list of any of the jsonpath objects, just do a kubectl get pods -o
json and it will
list them like this, these are the json path items you can get under status:
}
}
],
"hostIP": "192.168.0.3",
"phase": "Running",
"podIP": "10.244.2.33",
"qosClass": "BestEffort",
"startTime": "2019-03-09T22:52:09Z"
}
use status.phase as example: kubectl get pods --sort-by=.status.phase
https://elastisys.com/2018/12/10/backup-kubernetes-how-and-why/