This article reviews a lab exercise on creating multi-container pods in Kubernetes and forwarding logs to Elasticsearch.
In this article, we review a lab exercise on multi-container pods. The lab demonstrates how to identify container counts in pods, create multi-container pods using specified images, and add a sidecar container to forward logs to Elasticsearch. This step-by-step guide will help you understand these concepts and implement them in your Kubernetes environment.
To determine the number of containers in the red pod, you have two options:
Check the “READY” column in the pod listing—the numbers indicate total containers versus how many are ready.
Use the describe command to inspect the pod details. In the output, look for the “Containers:” section. For example, the following YAML snippet shows three container entries named apple, wine, and searle:
Next, determine the container names in the blue pod. Examining its details reveals two containers, named teal and navy. Below is an excerpt of the pod details:
Rename the first container from yellow to lemon and add the sleep command. Then, include a second container named gold with the redis image. The updated YAML should look like this:
Once the pods are up and running, open the Kibana UI using the link provided above your terminal. Kibana serves as the dashboard for viewing the logs gathered by Elasticsearch. Elasticsearch collects log data—such as metrics and application logs—and Kibana visualizes them.To view logs from Kibana, run:
Next, examine the app pod to verify its container configuration and image details. The app pod runs a single container with an event simulator that sends log messages to /log/app.log. An excerpt from its description is shown below:
Inspect the log entries to identify any user issues. For example, the logs indicate that USER5 experienced login problems due to the account being locked after multiple failed attempts:
Copy
Ask AI
[2022-04-17 18:21:57,696] INFO in event-simulator: USER4 is viewing page3[2022-04-17 18:21:58,698] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS....
Adding a Sidecar Container for Log Shipping to Elasticsearch
To forward logs from the app pod to Elasticsearch, a sidecar container must be added. This container, named sidecar, utilizes a custom Fluent Bit image (configured similarly to Filebeat) to read logs from a shared volume and forward them to Elasticsearch.
The current definition of the app pod includes only the event simulator container. To implement log forwarding, modify the pod definition to include both the event simulator and the sidecar container, ensuring that they share the same volume.Below is an example YAML snippet with the updated configuration:
Since Kubernetes does not permit the modification of container sets on a live pod, you must force replace the existing pod with the updated YAML. If you try to edit the pod interactively, you might see an error similar to:
Editing an existing pod to add or remove containers is not allowed. Instead, update the configuration file and force replace the pod.
To apply the changes, use the force replace command:
After updating the app pod with the sidecar container, open the Kibana UI to confirm that logs are flowing into the Discover section. Follow these steps:
Create an index pattern in Kibana (for example, filebeat-*).
Configure the time filter as needed.
Navigate to the Discover section to view the log entries.
The following image shows the Kibana interface for creating an index pattern:
Once the index pattern is established, view the logs in the Discover section:
This confirms that logs from the app pod are successfully forwarded to Elasticsearch and visualized in Kibana.This concludes the guide on setting up multi-container pods and integrating a sidecar container for log shipping to Elasticsearch. Happy learning!