KKE level 2 task 2

I have done the task correctly but it still fails, I am attaching screenshot in this topic for your reference.
the question asks to create an init container I tried creating that but it goes in infinite loop and the main container is never executed, so i added the side-container as normal container but it still fails.

It would have helped if you had shared the YAML manifest you used to complete this task.

The sidecar container needs to be in initContainers with restartPolicy: Always set:

hello refer to my complete solutions for kk engineer kubernetes solutions and if it helps make sure to give a star to my repo . :slightly_smiling_face:

and for your task this is solution https://github.com/MiqdadProjects/kodekloud-kubernetes-solutions/blob/main/LEVEL02/TASK02/Task-02-WebserverPod.md

complete solutions form level 1 to 4

Dear @MIQDAD ,
Thank you for your reply with the solution post but the solution shared by @Santosh_KodeKloud above works and your solution fails, why ?
I am assuming you have completed task and created the solution file in github a long time back when the question doesn’t ask to create the sidecar-container as initContainer so your solution works logically even I also tried the same but it fail to pass the solution test cases set by kodekloud.
You can check my 1st screenshot 3rd point in the question it asks to create initContainer.
So what does @Santosh_KodeKloud has it has restartPolicy: Always. Generally if a pod has initContainer then it executes and finishes first then starts the main container but in the question the command to be added in the initContainer runs for infinite time hence initContainer never completes and blocks the main container from running.
So what does restartPolicy: Always do ?
When the pod is getting created it firsts creates the initContainer. As restartPolicy is set to Always it treats this specially and it doesn’t wait for initContainer to complete but just waits for initContainer to start first, Once initContainer starts it goes to the main pod to start it and runs successfully both containers and KodeKloud Engineer team has smartly added one line in the 5th point that is “Ensure all containers are in a running state”.

1 Like

Hi @cpyoyo911,

I was going through this myself, facing the same question.

In my mind having an init container with the command that they have provided did not make sense because that loop is infinite. Meaning that, the init container would never exit and the main container would never start.

I did some digging and found that starting with k8s v1.29, sidecar containers require a restartPolicy: Always.

With this configuration, the behavior of an init container changes entirely.

Previously, we would have something like this: init runs -> must exit 0 -> then main container starts.

With the restartPolicy in place we have init starts -> runs alongside main container -> restarts if it crashes

Here is the official documentation for this feature: Sidecar Containers | Kubernetes

Hope this helps!

1 Like