ckad练习题- multi-container-pods
Multi-Container Pods (10%)
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 containerapp
uses the imagebusybox
and runs the commandwhile true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskspace.txt; sleep 5; done;
. The adapter containertransformer
uses the imagebusybox
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 systems. - Before 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.
Solution:
kubectl run adapter --image=busybox --restart=Never -o yaml --dry-run -- /bin/sh -c 'while true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskspace.txt; sleep 5; done;' > adapter.yaml
The final Pod YAML file should look something like this:
apiVersion: v1 kind: Pod metadata: creationTimestamp: null name: adapter spec: volumes: - name: config-volume emptyDir: {} containers: - args: - /bin/sh - -c - 'while true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskspace.txt; sleep 5; done;' image: busybox name: app volumeMounts: - name: config-volume mountPath: /var/logs resources: {} - image: busybox name: transformer args: - /bin/sh - -c - 'sleep 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;' volumeMounts: - name: config-volume mountPath: /var/logs dnsPolicy: ClusterFirst restartPolicy: Never status: {}
kubectl exec adapter --container=transformer -it -- /bin/sh / # ls -l -rw-r--r-- 1 root root 205 May 12 20:43 2019-05-12-20-43-32-transformed.txt -rw-r--r-- 1 root root 369 May 12 20:43 2019-05-12-20-43-52-transformed.txt ... / # cat 2019-05-12-20-43-52-transformed.txt 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root 4.0K /root / # exit
分类:
k8s
, cloud native
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix