Manage Application Logs

Manage Application Logs

In this tutorial we will discuss about how to manage your application logs in Kubernetes.

Manage Application Logs

In real time applications development, we may get number of problems or bugs or exceptions while executing or testing the applications.

To identify the problems and their locations then we have to trace the applications flow of execution.

Logs will help us to understand data flow through applications, as well as spot when and where errors are occurring.

Let us start with logging in docker. I run a docker container called my-webapp-events and all that it does is generate random events simulating my web server.

$ docker run hub.waytoeasylearn.com/my-webapp-events:1.0.0
[2020-10-29 11:25:15,561] INFO in event-simulator: USER1 logged in
[2020-10-29 11:24:32,066] INFO in event-simulator: USER3 is viewing page2
[2020-10-29 11:24:33,067] INFO in event-simulator: USER1 is viewing page2
[2020-10-29 11:24:34,068] INFO in event-simulator: USER3 is viewing page1
[2020-10-29 11:24:35,069] INFO in event-simulator: USER3 is viewing page3
[2020-10-29 11:24:36,070] INFO in event-simulator: USER2 logged out
[2020-10-29 11:24:37,072] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2020-10-29 11:24:37,072] INFO in event-simulator: USER3 is viewing page2
[2020-10-29 11:24:38,074] INFO in event-simulator: USER4 is viewing page2
[2020-10-29 11:24:39,075] INFO in event-simulator: USER1 logged out
[2020-10-29 11:24:40,077] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2020-10-29 11:24:40,078] INFO in event-simulator: USER2 logged out
[2020-10-29 11:24:41,079] INFO in event-simulator: USER3 logged in
[2020-10-29 11:24:42,081] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2020-10-29 11:24:42,081] INFO in event-simulator: USER2 is viewing page2
[2020-10-29 11:24:43,081] INFO in event-simulator: USER3 logged out
[2020-10-29 11:24:44,082] INFO in event-simulator: USER2 is viewing page3
[2020-10-29 11:24:45,083] INFO in event-simulator: USER4 is viewing page3
[2020-10-29 11:24:46,085] INFO in event-simulator: USER3 logged out

These are events streamed to the standard output by my application.

Now, if I were to run the docker container in the background, in a detached mode using the -d option, I wouldn’t see the logs.

$ docker run -d hub.waytoeasylearn.com/my-webapp-events:1.0.0

If I wanted to view the logs, I could use following command

$ docker logs -f 50ab19a9548f
[2020-10-29 11:30:53,150] INFO in event-simulator: USER2 logged in
[2020-10-29 11:30:54,152] INFO in event-simulator: USER1 is viewing page2
[2020-10-29 11:30:55,152] INFO in event-simulator: USER4 is viewing page1
[2020-10-29 11:30:56,154] INFO in event-simulator: USER4 is viewing page3
[2020-10-29 11:30:57,155] INFO in event-simulator: USER3 is viewing page3
[2020-10-29 11:30:58,156] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2020-10-29 11:30:58,157] INFO in event-simulator: USER4 logged in
[2020-10-29 11:30:59,158] INFO in event-simulator: USER2 logged in
[2020-10-29 11:31:00,159] INFO in event-simulator: USER3 is viewing page1
[2020-10-29 11:31:01,161] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2020-10-29 11:31:01,161] INFO in event-simulator: USER2 logged out
[2020-10-29 11:31:02,162] INFO in event-simulator: USER2 is viewing page3

Here -f option helps us to see live logs of the container.

Now let’s back to the Kubernetes. We create a POD with the same docker image using the POD definition file.

apiVersion: v1
kind: Pod
metadata:
  name: my-webapp-events-pod
spec:
  containers:
  - name: my-webapp-events
    image: hub.waytoeasylearn.com/my-webapp-events:1.0.0

Once the POD is running, we can view the logs using the following command.

$ kubectl logs -f my-webapp-events-pod
[2020-10-29 11:30:53,150] INFO in event-simulator: USER2 logged in
[2020-10-29 11:30:54,152] INFO in event-simulator: USER1 is viewing page2
[2020-10-29 11:30:55,152] INFO in event-simulator: USER4 is viewing page1
[2020-10-29 11:30:56,154] INFO in event-simulator: USER4 is viewing page3
[2020-10-29 11:30:57,155] INFO in event-simulator: USER3 is viewing page3
[2020-10-29 11:30:58,156] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2020-10-29 11:30:58,157] INFO in event-simulator: USER4 logged in
[2020-10-29 11:30:59,158] INFO in event-simulator: USER2 logged in
[2020-10-29 11:31:00,159] INFO in event-simulator: USER3 is viewing page1
[2020-10-29 11:31:01,161] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2020-10-29 11:31:01,161] INFO in event-simulator: USER2 logged out
[2020-10-29 11:31:02,162] INFO in event-simulator: USER2 is viewing page3

Here you can use -f option to stream the logs live just like the docker command.

Now these logs are specific to the container running inside the POD. As we discussed before, Kubernetes PODs can have multiple docker containers in them.

apiVersion: v1
kind: Pod
metadata:
  name: my-webapp-events-pod
spec:
  containers:
  - name: my-webapp-events
    image: hub.waytoeasylearn.com/my-webapp-events:1.0.0
  - name: my-webapp-queue-listener
    image: hub.waytoeasylearn.com/my-webapp-queue:1.0.0

Now if you run the kubectl logs command with the POD name, which container’s log would it show?

If there are multiple containers within a POD then you must specify the name of the container explicitly in the command. Otherwise it would fail asking you to specify a name.

So in this case i will specify the name of the first container my-webapp-events and that prints the relevant logs.

$ kubectl logs -f my-webapp-events-pod my-webapp-events
[2020-10-29 11:30:53,150] INFO in event-simulator: USER2 logged in
[2020-10-29 11:30:54,152] INFO in event-simulator: USER1 is viewing page2
[2020-10-29 11:30:55,152] INFO in event-simulator: USER4 is viewing page1
[2020-10-29 11:30:56,154] INFO in event-simulator: USER4 is viewing page3
[2020-10-29 11:30:57,155] INFO in event-simulator: USER3 is viewing page3
[2020-10-29 11:30:58,156] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2020-10-29 11:30:58,157] INFO in event-simulator: USER4 logged in
[2020-10-29 11:30:59,158] INFO in event-simulator: USER2 logged in
[2020-10-29 11:31:00,159] INFO in event-simulator: USER3 is viewing page1
[2020-10-29 11:31:01,161] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2020-10-29 11:31:01,161] INFO in event-simulator: USER2 logged out
[2020-10-29 11:31:02,162] INFO in event-simulator: USER2 is viewing page3
Conclusion

There is a lot of nuance surrounding logging in Kubernetes. Although Kubernetes offers some basic built-in logging and monitoring functionality, it’s a far cry from a full-fledged logging solution.

To get the most out of Kubernetes logging, you’ll need an external log collection, analysis, and management tool like LogDNA, which is very easy to set up on Kubernetes distributions like IKS, where it is one of the officially supported logging solutions.

Manage Application Logs


Scroll to top