Introduction#
- question 1 namespace, context, pod
- question 2 create cronjob
- question 3 sidecar container
- question 4 persistent volume
- question 5 init container
- question 5b multiple container pod
Question 1 NameSpace and Pod#
- create a name space ckad
- in the ckad namespace create a pod name pod1 with image of nginx and epose port 80
- verify pod running
- retrieve the ip address of the pod
- run a temporary pod using image busybox, shell into it and run a wget against pod1
- view the logs of the pod1
- delete pod1 and the namespace
here is the solution
create namespace
kubectl create namespace ckad
set context
kubectl config set-context --current --namespace=ckad
run a pod
kubectl run pod1 --image=nginx --dry-run=client --output=yaml > q1.yaml
double check the q1.yaml and apply
kubectl create -f q1.yaml
get the ip of the pod
kubectl get pods --output=wide
run a busybox temporary
kubectl run busybox --image=busybox --rm -it --command -- bin/sh
inside the interative busybox
wget IP_ADDRESS_POD_1
Question 2 CronJob#
Question 12 Create a CronJob
- create a cronjob named show-date that runs every minute
- executes the shell command echo "Current date:$(date)"
- watch the jobs as they are being scheduled
- identify one of the pods that run thte CronJob and render the logs
- determine the number of successfull executions the CronJob will keep in its history
- delete the job
create a cronjob
kubectl create cronjob show-date --image=busybox --schedule="*/1 * * * *" --dry-run=client --output=yaml > q12CronJob.yaml
then add the command to the yaml as the following
apiVersion: batch/v1kind: CronJobmetadata:creationTimestamp: nullname: show-datespec:jobTemplate:metadata:creationTimestamp: nullname: show-datespec:template:metadata:creationTimestamp: nullspec:containers:- image: busyboxname: show-datecommand:- /bin/sh- -c- echo $(date)resources: {}restartPolicy: OnFailureschedule: '*/1 * * * *'status: {}
then apply or deploy the cronjob
kubectl create -f q12CronJob.yaml
watch the scheduled job running
kubectl get cronjob show-date --watch
choose a pod and logs
kubectl get pods
show logs of the selected pod
kubectl log PODNAMAE
check number of successfull echo date
kubectl get cronjob show-date -o yaml
delete the cronjob
kubectl delete cronjob show-date
Question 3 Sidecar Container#
- create a yaml file for a pod named data-exchange, main app container named main-app use busybox
- the container runs a command that writes a new file every 30 seconds into /var/app/data, the filename follows the pattern counter-data.txt
- the variable counter is incremented every interval and starts with value of 1
- modify yaml by adding a sidecar named sidecar, image busybox
- and runs a command that counts the number of files produced by main-app container, output the number to stdout
- define a volume of type emptydir, mount path /var/app/data for both container
- create the pod, tail logs of sidecar container, delete pod force
create yaml for a pod named data-exchange
kubectl run data-exchange --image=busybox --dry-run=client --output=yaml > q18.yaml
then add command to create a new file every 30 seconds
command: ["/bin/sh"]args:["-c","counter=1;while true;do echo $counter > /var/app/data/$counter-data.txt;sleep 2;counter=$(($counter+1));done"]
then add volume and mount path
volumeMounts:- mountPath: /var/app/dataname: data-vol
finally add a sidecar container with a while lopp, please noted
apiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: data-exchangename: data-exchangespec:volumes:- name: data-volemptyDir: {}containers:- name: sidecarimage: busyboxvolumeMounts:- mountPath: /var/app/dataname: data-volcommand: ['/bin/sh']args: ['-c', 'while true; do ls /var/app/data | wc -l ; sleep 2; done']- name: main-appimage: busyboxresources: {}volumeMounts:- mountPath: /var/app/dataname: data-volcommand: ['/bin/sh']args:['-c','counter=1; while true; do echo $counter > /var/app/data/$counter-data.txt;sleep 2;counter=$(($counter+1)); done']dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}
check log enable streaming logs
kubectl logs data-exchange -c sidecar -f
check logs tail
kubectl logs data-exchange -c sidecar --tail=10
delete the pod
kubectl delete pod data-exchange
Question 4 Persistent Volume#
- create a persistent volume that uses local storage, accessible from all namespaces
- create a persistent volume claim, namespace speedvol
- run a pod with the name pv-pod, use the pvc, namespace speed vol
just follow this docs link
Assume that you are an admin and you create a persistent volume, this exist at the cluster level.
apiVersion: v1kind: PersistentVolumemetadata:name: task-pv-volumelabels:type: localspec:storageClassName: manualcapacity:storage: 3GiaccessModes:- ReadWriteOncehostPath:path: '/mnt/data'
Assume that you are an user, and use create a persistent volume claim, this like creating pods
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: task-pv-claimnamespace: speedvolspec:storageClassName: manualaccessModes:- ReadWriteOnceresources:requests:storage: 1Gi
then create a pod using the pvc
apiVersion: v1kind: Podmetadata:name: task-pv-podnamespace: speedvolspec:volumes:- name: task-pv-storagepersistentVolumeClaim:claimName: task-pv-claimcontainers:- name: task-pv-containerimage: nginxports:- containerPort: 80name: 'http-server'volumeMounts:- mountPath: '/usr/share/nginx/html'name: task-pv-storage
Question 5 Init Container#
deploy
kubectl create -f q5.yaml
get logs of the init container
kubectl logs complex-pod -c setup
shell into the main container or the app container
kubectl exec complex-pod -it -c app -- /bin/sh
there are different syntax to run a command inside a container. First one is like running a shell/bash script, so-called run a command in a shell
initContainers:- name: setupimage: busyboxcommand: ['/bin/sh', '-c', 'wget -O- www.google.com']
or
initContainers:- name: setupimage: busyboxcommand: ['/bin/sh']args: ['-c', 'wget -O- www.google.com']
or like a command with args
initContainers:- name: setupimage: busyboxcommand: ['wget']args: ['-O-', 'wwww.google.com']
Question 5b Multiple Container Pod#
- create a new pod in a yaml file named business-app.yaml
- the pod should define two containers
- one init container and one main application container
- name init container - configurer
- name main container - web
- the init container uses busybox image
- main container uses the speedracer5dave/nodejs-read-config:latest
- expose the main container on port 8080
create a new pod
kubectl run newpod --image=speedracer5dave/nodejs-read-config:latest --port=8080 --dry-run=client --output=yaml > q16.yaml
then edit the yaml to add
- initContainer
- shared volume
- mount path
apiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: newpodname: newpodspec:volumes:- name: shared-volemptyDir: {}initContainers:- name: configurerimage: busyboxvolumeMounts:- mountPath: /usr/shared/appname: shared-volcommand: ['wget']args:['-O','/usr/shared/app/config.json','https://raw.githubusercontent.com/speedracer55/ckad-crash-course/master/exercises/07-creating-init-container/app/config/config.json']containers:- image: speedracer5dave/nodesjs-read-config:latestname: webvolumeMounts:- mountPath: /usr/shared/appname: shared-volports:- containerPort: 8080resources: {}dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}
the deploy
kubectl create -f q16.yaml
shell into pod and check
kubectl exec newpod -it -- /bin/sh
find the downloaded config.json
cd /usr/shared/app/config.json
troubleshooting by
kubectl get podskubectl describe pod newpodkubectl logs newpod
- the wget command
wget -O FILE URL