Skip to main content

CKAD Practice Exam – Part 2

Hands-On Lab

 

Photo of Will Boyd

Will Boyd

DevOps Team Lead in Content

Length

01:30:00

Difficulty

Intermediate

This lab is designed to help prepare for the kinds of tasks and scenarios encountered during the Certified Kubernetes Application Developer (CKAD) exam. In this lab, we will practice working with multi-container pods by creating a pod that implements an adapter design pattern. We will create a pod that generates some log output and uses an adapter pod to format the output.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

CKAD Practice Exam - Part 2

The scenario

Our team has a pod which generates some log output. However, they want to consume the data using an external application, which requires the data to be in a specific format. Our task is to create a pod design that utilizes an adapter running fluentd to format the output from the main container. These are the things we have to worry about:

  • Create the pod descriptor in /usr/ckad/adapter-pod.yml. An empty file has already been created for us.

  • The pod should be named counter.

  • Add a container to the pod that runs the busybox image, and name it count.

  • Run the count container with the following arguments:

    - /bin/sh
    - -c
    - >
    i=0;
    while true;
    do
      echo "$i: $(date)" >> /var/log/1.log;
      echo "$(date) INFO $i" >> /var/log/2.log;
      i=$((i+1));
      sleep 1;
    done
  • Add another container called adapter to the pod, and make it run the k8s.gcr.io/fluentd-gcp:1.30 image.

  • There is a fluentd configuration located on the server at /usr/ckad/fluent.conf. Load the data from this file into a ConfigMap called fluentd-config. Mount this ConfigMap to the adapter container so that the config data is located inside the container in a file at /fluentd/etc/fluent.conf.

  • Add an environment variable to the adapter container called FLUENTD_ARGS with the value -c /fluentd/etc/fluent.conf.

  • Create a volume for the pod in such a way that the storage will be deleted if the pod is removed from a node. Mount this volume to both containers at /var/log. The count container will output log data to this volume, and the adapter container will read the data from the same volume.

  • Create a hostPath volume where the adapter container will output the formatted log data. Store the data at the /usr/ckad/log_output path. Mount the volume to the adapter container at /var/logout.

Get logged in

Use the credentials and server IP in the hands-on lab overview page to log in with SSH.

Create a ConfigMap to store the fluentd configuration

Let's become root so that we can read and edit the config file:

[user@host]$ sudo -i

We need the contents of the config file, so that we can put them into our config map:

[root@host]# cat /usr/ckad/fluent.conf

Now we need to create a descriptor for the ConfigMap (with whatever text editor we like) fluentd-config.yml. What we read in the cat command is going down after the fluent.conf: | line. Let's make sure that our indenting is correct:

apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
data:
  fluent.conf: |
    &ltsource>
      type tail
      format none
      path /var/log/1.log
      pos_file /var/log/1.log.pos
      tag count.format1
    &lt/source>

    &ltsource>
      type tail
      format none
      path /var/log/2.log
      pos_file /var/log/2.log.pos
      tag count.format2
    &lt/source>

    &ltmatch **>
      @type file
      path /var/logout/count
      time_slice_format %Y%m%d%H%M%S
      flush_interval 5s
      log_level trace
    &lt/match>

Create the ConfigMap in the cluster:

[root@host]# kubectl apply -f fluentd-config.yml

Create the pod descriptor

Edit the descriptor file (again, with whatever text editor we like) /usr/ckad/adapter-pod.yml. And once again, make sure that we have proper indentation:

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)" >> /var/log/1.log;
        echo "$(date) INFO $i" >> /var/log/2.log;
        i=$((i+1));
        sleep 1;
      done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: adapter
    image: k8s.gcr.io/fluentd-gcp:1.30
    env:
    - name: FLUENTD_ARGS
      value: -c /fluentd/etc/fluent.conf
    volumeMounts:
    - name: varlog
      mountPath: /var/log
    - name: config-volume
      mountPath: /fluentd/etc
    - name: logout
      mountPath: /var/logout
  volumes:
  - name: varlog
    emptyDir: {}
  - name: config-volume
    configMap:
      name: fluentd-config
  - name: logout
    hostPath:
      path: /usr/ckad/log_output

Create the Pod in the cluster and make sure it is working

Let's create the pod:

[root@host]# kubectl apply -f /usr/ckad/adapter-pod.yml

And then we'll verify that the pod starts up:

[root@host]# kubectl get pod counter

We can also verify that everything is working by checking the output directory on the worker node. We'll log into the worker from the master:

[root@host]# ssh cloud_user@<WORKER NODE IP ADDRESS>

If we look, we'll see fluentd output files in the output directory:

[user@host]$ ls /usr/ckad/log_output

Conclusion

Well, we've finished. Our team can use an external application to process their pod's log output. Congratulations!