Maintainers: feitnomore
This is a hack to implement a sidecar container that will run within a Pod in Kubernetes and will read the sysout of the main Pod's container.
This hack takes advantage of Kubernetes Python Client to find the name of the container that we'll be reading the sysout as well as to actually read the sysout from it.
WARNING: Use it at your own risk.
The idea here is to be able to read the sysout of the application running on the Pod and process the output in any sort of method. The application would log everything that it has on the standard output/standard error, and the sidecar would read its logs and process it, for example, pushing it to a Logstash or to a Kafka Topic.
This approach would be interesting because it would allow the application to use any library it wants and any technology it wants, as far as it outputs the information to standard out/standard error, this way, you would not need to enforce any library usage or any technology usage by the application.
Note: Don't think only about logging, but imagine you can use it for event handling as well.
In order for this to work, we need to create a ServiceAccount
, a ClusterRole
and a ClusterRoleBinding
. We'll be granting get
, watch
and list
to resources pods
and pods/logs
.
$ kubectl apply -f https://raw.githubusercontent.com/feitnomore/sysout-handler/master/examples/sysout-handler_ServiceAccount.yaml
$ kubectl apply -f https://raw.githubusercontent.com/feitnomore/sysout-handler/master/examples/sysout-handler_ClusterRole.yaml
$ kubectl apply -f https://raw.githubusercontent.com/feitnomore/sysout-handler/master/examples/sysout-handler_ClusterRoleBinding.yaml
Note: The ServiceAccount will be created in test
Namespace.
git clone https://github.com/feitnomore/sysout-handler.git
-
Container Names
The application will look for the first container on thePod
that doesn't matchistio-proxy
(Istio's sidecar) orsysout-handler
(ourselves). If you have more containers that you want to ignore, you should adjust your code in thehelpers/kubeclient.py
ongetContainerName
. -
Handling Logic
This code will only print the lines it reads. The handling of the line, and actual actions needs to be coded. For example, I have a code here that parses the lines and send them to a Kafka Topic. This handling would have to be called fromhelpers/kubeclient.py
onreadSysout
. -
Environment
I encourage you to read all the configuration from the environment. We are already doing it for the Podname
, and PodNamespace
. So, for example, if you plan on sending data to a Kafka Topic, add some environment variables on the YAML of the Sidecar (see examples) for theserver
,port
andtopic
. After that, make sure you read and test them onsysout-handler.py
initialization. -
Exception
There is pretty much no exception handling here. Make sure you test it well, and add proper exception handling to your cases, specially if you are working with standardized outputs. -
Dockerfile
Make sure you editDockerfile
according to your changes. If you add any libs, modules or files, make sure to add valid pip commands and copy commands on theDockerfile
so that your changes are saved on the image.
cd sysout-handler
docker build -t sysout-handler .
export MY_REPO="my_local_repository"
docker tag sysout-handler:latest $MY_REPO/sysout-handler:latest
docker push $MY_REPO/sysout-handler:latest
Note: Remember to set MY_REPO
to your Docker Hub repository or your local repository.
Add the sidecar to your Pod. Under the containers
specification, add the follwing container
:
- name: sysout-handler
image: my_local_repository/sysout-handler:latest
imagePullPolicy: Always
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
serviceAccountName: sysout-handler
Note: Remember to set the image
to the repository you used in the previous step.
Note: Remember this will only work in test
Namespace, because the ServiceAccount
was created on test
Namespace.
Note: If you changed your code and need to add more environment variables, this is the place where you would add them.
Note: You can think about adding this to your automation pipeline so that you have the sidecar added automatically when deploying your Pods.