3. Multi-Container Pods (10%)
1. Implementing the Adapter Pattern
The adapter pattern helps with providing a simplified, homogenized view of an application running within a container. For example, we could stand up another container that unifies the log output of the application container. As a result, other monitoring tools can rely on a standardized view of the log output without having to transform it into an expected format.
Create a new Pod in a YAML file named
adapter.yaml
. The Pod declares two containers. The container app uses the imagebusybox
and runs the commandwhile true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskspace.txt; sleep 5; done;
. The adapter container transformer uses the image busybox and runs the commandsleep 20; while true; do while read LINE; do echo "$LINE" | cut -f2 -d"|" >> $(date +%Y-%m-%d-%H-%M-%S)-transformed.txt; done < /var/logs/diskspace.txt; sleep 20; done;
to strip the log output off the date for later consumption my a monitoring tool. Be aware that the logic does not handle corner cases (e.g. automatically deleting old entries) and would look different in production systemsBefore creating the Pod, define an
emptyDir
volume. Mount the volume in both containers with the path/var/logs
.Create the Pod, log into the container transformer. The current directory should continuously write a new file every 20 seconds.
答案:
The final Pod YAML file should look something like this:
最后更新于