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#

ckad question 1
  • 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

ckad question 2
  • 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/v1
kind: CronJob
metadata:
creationTimestamp: null
name: show-date
spec:
jobTemplate:
metadata:
creationTimestamp: null
name: show-date
spec:
template:
metadata:
creationTimestamp: null
spec:
containers:
- image: busybox
name: show-date
command:
- /bin/sh
- -c
- echo $(date)
resources: {}
restartPolicy: OnFailure
schedule: '*/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#

ckad question 3
  • 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/data
name: data-vol

finally add a sidecar container with a while lopp, please noted

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: data-exchange
name: data-exchange
spec:
volumes:
- name: data-vol
emptyDir: {}
containers:
- name: sidecar
image: busybox
volumeMounts:
- mountPath: /var/app/data
name: data-vol
command: ['/bin/sh']
args: ['-c', 'while true; do ls /var/app/data | wc -l ; sleep 2; done']
- name: main-app
image: busybox
resources: {}
volumeMounts:
- mountPath: /var/app/data
name: data-vol
command: ['/bin/sh']
args:
[
'-c',
'counter=1; while true; do echo $counter > /var/app/data/$counter-data.txt;sleep 2;counter=$(($counter+1)); done'
]
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

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#

ckad question 4
  • 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: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 3Gi
accessModes:
- ReadWriteOnce
hostPath:
path: '/mnt/data'

Assume that you are an user, and use create a persistent volume claim, this like creating pods

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
namespace: speedvol
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

then create a pod using the pvc

apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
namespace: speedvol
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: 'http-server'
volumeMounts:
- mountPath: '/usr/share/nginx/html'
name: task-pv-storage

Question 5 Init Container#

ckad question 1

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: setup
image: busybox
command: ['/bin/sh', '-c', 'wget -O- www.google.com']

or

initContainers:
- name: setup
image: busybox
command: ['/bin/sh']
args: ['-c', 'wget -O- www.google.com']

or like a command with args

initContainers:
- name: setup
image: busybox
command: ['wget']
args: ['-O-', 'wwww.google.com']

Question 5b Multiple Container Pod#

ckad question 1
  • 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: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: newpod
name: newpod
spec:
volumes:
- name: shared-vol
emptyDir: {}
initContainers:
- name: configurer
image: busybox
volumeMounts:
- mountPath: /usr/shared/app
name: shared-vol
command: ['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:latest
name: web
volumeMounts:
- mountPath: /usr/shared/app
name: shared-vol
ports:
- containerPort: 8080
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}

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 pods
kubectl describe pod newpod
kubectl logs newpod
  • the wget command
wget -O FILE URL