Mount Redis Logs Using a Kubernetes Sidecar Container
The DevOps team is running a Redis workload in the Kubernetes cluster. The Redis container currently writes its logs to a file inside the container filesystem. The team wants to make these logs available to a separate sidecar container for monitoring and collection.
A starting Pod manifest is provided in redis-logger.yaml.
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
volumes:
- name: varlog
emptyDir: {}
containers:
- name: redis
image: redis
command: ["sh", "-c"]
args:
- redis-server --logfile /var/log/redis/redis-server.logYour task is to modify the provided configuration and recreate the redis Pod so that it uses a sidecar container to consume the Redis log file.
The Pod must contain:
- A
rediscontainer using theredisimage. - A
sidecarcontainer using thebusyboximage. - A shared
emptyDirvolume namedvarlog. - Both containers must mount the shared volume at
/var/log/redis. - The Redis container must continue writing its server logs to
/var/log/redis/redis-server.log. - The sidecar container must continuously read the Redis log file.
After configuring the Pod, verify that both containers are running and that the sidecar is receiving the Redis log output.
Save the logs produced by the sidecar container to /tmp/redis-logs.txt
Finally, verify that /tmp/redis-logs.txt contains the expected Redis log output.
sidecar containers, see the official Kubernetes documentation.Understand the Existing Configuration
The initial Pod already has an emptyDir volume named varlog, and the Redis container writes its logs to /var/log/redis/redis-server.log.
However, the Redis container is not yet using the varlog volume. To allow the sidecar to access the Redis logs, the same varlog volume must be mounted at /var/log/redis in both the Redis and sidecar containers.
Add the Redis Volume Mount
Add the varlog volume mount to the Redis container at /var/log/redis. This allows Redis to write its log file into the shared emptyDir volume, making the logs available to the sidecar container.
Add the Sidecar Container
Add a second container named sidecar using the busybox image. Configure it to continuously read the Redis log file using tail -f /var/log/redis/redis-server.log.
The sidecar must also mount the same varlog volume at /var/log/redis so it can access and read the log file created by the Redis container.
Final Pod Manifest
The completed redis-logger.yaml should look like this:
Both containers reference the same varlog volume, which uses emptyDir. The Redis container writes its logs to /var/log/redis, while the sidecar container mounts the same volume at the same path to access those logs.
This allows both containers to share the redis-server.log file through the common volume.
Apply the Pod
Deploy the Pod:
$ kubectl apply -f redis-logger.yaml
pod/redis createdVerify the Pod Status
Check the Pod:
$ kubectl get pod redis
NAME READY STATUS RESTARTS AGE
redis 2/2 Running 0 20mThe important values are READY: 2/2 and STATUS: Running, confirming that both containers in the Pod are ready and running successfully.
Inspect the Pod
Use kubectl describe to verify the containers and volume mounts:
kubectl describe pod redisLook for the Containers section and verify that it lists both redis and sidecar containers.
Under the volume mounts, both containers should show /var/log/redis from varlog, confirming that they share the same volume.
Verify the Redis Container
Check the Redis container state:
$ kubectl get pod redis -o jsonpath='{.status.containerStatuses[*].name}'
redis sidecarVerify the Sidecar Container
Check the sidecar logs:
kubectl logs redis -c sidecarThe -c sidecar option tells Kubernetes to retrieve logs specifically from the sidecar container.
You should see Redis startup messages in the output.
The exact contents of the Kubernetes log collection will depend on the Redis version and runtime environment.
Verify the Redis Log File Directly
You can verify that the log file exists inside the Redis container:
kubectl exec redis -c redis -- ls -l /var/log/redis/You should see:
redis-server.logYou can also check the file:
kubectl exec redis -c redis -- cat /var/log/redis/redis-server.logThis confirms that Redis is writing its logs to the shared volume.
Save the Sidecar Logs
Save the sidecar container logs to the required file:
kubectl logs redis -c sidecar > /tmp/redis-logs.txtThis redirects the sidecar's logs to /tmp/redis-logs.txt.
Verify the Log File
Check that the file was created:
ls -l /tmp/redis-logs.txtThen display its contents:
cat /tmp/redis-logs.txtThe file should contain the Redis log output observed by the sidecar.
Why Use a Sidecar Container?
A sidecar container can perform an additional supporting function alongside the main application container.
In this scenario, the Redis container runs Redis and writes its logs to the shared emptyDir volume.
The sidecar container continuously reads those logs from the same volume. This allows both containers to share log data without the sidecar needing direct access to the Redis container's filesystem.
Why Use emptyDir?
The shared volume is configured as:
emptyDir: {}An emptyDir volume is created when the Pod is assigned to a node.
Both containers in the Pod can access the same emptyDir volume.
The emptyDir volume is shared between the Redis and sidecar containers and exists for the lifetime of the Pod.
Both containers can access the log files through /var/log/redis. When the Pod is deleted, the emptyDir volume and all data stored in it are also removed.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
Tip 1: Understand the Kubernetes sidecar pattern
A Pod can contain multiple containers that work together. In this example, the Redis container runs the main application, while the BusyBox sidecar container performs a supporting logging function by reading the Redis log file.
Tip 2: Containers in the Same Pod Can Share Volumes
Containers within the same Pod can share a volume. Define the varlog volume once using emptyDir:
volumes:
- name: varlog
emptyDir: {}
Then mount the same volume into each container:
volumeMounts:
- name: varlog
mountPath: /var/log/redis
This allows both containers to access the same files through the shared volume.
Tip 3: Volume Names Must Match
The volume name must be identical in both volumes and volumeMounts. For example:
volumes:
- name: varlog
must match:
volumeMounts:
- name: varlog
A mismatch, such as varlog versus var-log, prevents Kubernetes from associating the volume mount with the defined volume and results in a configuration error.