Created
June 9, 2018 21:23
-
-
Save matthewpalmer/8f04c26705c6b6b8f56e5c397c61d1e8 to your computer and use it in GitHub Desktop.
Example Kubernetes YAML for the multi-container adapter design pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example YAML configuration for the adapter pattern. | |
# It defines a main application container which writes | |
# the current date and system usage information to a log file | |
# every five seconds. | |
# The adapter container reads what the application has written and | |
# reformats it into a structure that a hypothetical monitoring | |
# service requires. | |
# To run: | |
# kubectl apply -f pod.yaml | |
# Once the pod is running: | |
# | |
# (Connect to the application pod) | |
# kubectl exec pod-with-adapter -c app-container -it sh | |
# | |
# (Take a look at what the application is writing.) | |
# cat /var/log/top.txt | |
# | |
# (Take a look at what the adapter has reformatted it to.) | |
# cat /var/log/status.txt | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: pod-with-adapter | |
spec: | |
# Create a volume called 'shared-logs' that the | |
# app and adapter share. | |
volumes: | |
- name: shared-logs | |
emptyDir: {} | |
containers: | |
# Main application container | |
- name: app-container | |
# This application writes system usage information (`top`) to a status | |
# file every five seconds. | |
image: alpine | |
command: ["/bin/sh"] | |
args: ["-c", "while true; do date > /var/log/top.txt && top -n 1 -b >> /var/log/top.txt; sleep 5;done"] | |
# Mount the pod's shared log file into the app | |
# container. The app writes logs here. | |
volumeMounts: | |
- name: shared-logs | |
mountPath: /var/log | |
# Adapter container | |
- name: adapter-container | |
# This sidecar container takes the output format of the application | |
# (the current date and system usage information), simplifies | |
# and reformats it for the monitoring service to come and collect. | |
# In this example, our monitoring service requires status files | |
# to have the date, then memory usage, then CPU percentage each | |
# on a new line. | |
# Our adapter container will inspect the contents of the app's top file, | |
# reformat it, and write the correctly formatted output to the status file. | |
image: alpine | |
command: ["/bin/sh"] | |
# A long command doing a simple thing: read the `top.txt` file that the | |
# application wrote to and adapt it to fit the status file format. | |
# Get the date from the first line, write to `status.txt` output file. | |
# Get the first memory usage number, write to `status.txt`. | |
# Get the first CPU usage percentage, write to `status.txt`. | |
args: ["-c", "while true; do (cat /var/log/top.txt | head -1 > /var/log/status.txt) && (cat /var/log/top.txt | head -2 | tail -1 | grep | |
-o -E '\\d+\\w' | head -1 >> /var/log/status.txt) && (cat /var/log/top.txt | head -3 | tail -1 | grep | |
-o -E '\\d+%' | head -1 >> /var/log/status.txt); sleep 5; done"] | |
# Mount the pod's shared log file into the adapter | |
# container. | |
volumeMounts: | |
- name: shared-logs | |
mountPath: /var/log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment