Init Containers Demo¶
Now, let's see init containers in action and observe how these specialized containers, as discussed in the previous section, perform their crucial tasks within the kubernetes deployment.
Docker Images¶
Here are the Docker Images used in this tutorial:
Note
reyanshkharga/alpine is the modified version of famous alpine
Docker image which includes curl
, ping
, and nslookup
linux utilities.
Step 1: Create a Deployment¶
Let's use a deployment to create pods with init containers as follows:
Note
-
until
command in Linux is used to execute a set of commands as long as the final command in theuntil
commands has an exit status which is not zero. -
nslookup
is a command-line tool for querying DNS to retrieve information about domain names and their associated IP addresses. It is used for network troubleshooting and DNS resolution.
Here's what each init container does in the above deployment:
-
init-database
initializes by checking the availability of themy-database-service
through DNS resolution, waiting until it's reachable before moving on. -
init-cache
performs a similar initialization, ensuring themy-cache-service
is accessible through DNS resolution before proceeding with the main container.
Step 2: Verify Pods¶
Tip
List the pods in watch mode so that we can observe how the status changes once the init containers finish their tasks.
You'll notice that the pods haven't finished their initialization and are currently in a non-running state. This is because the init containers are still pending completion, as the services my-database-service
and my-cache-service
haven't been created and are consequently unavailable.
Step 3: Create Services¶
Let's start creating services that init containers are waiting for:
Remember that kubelet
runs each init container sequentially. So, even if you create the my-cache-service
first, the init-cache
init container won't run until the preceding init container init-database
has completed successfully.
-
Create
my-database-service
:Once
my-database-service
is operational, you will observe that theinit-database
init container has successfully completed its tasks. -
Create
my-cache-service
:Now that
my-cache-service
is also operations,init-cache
init container would also complete its tasks successfully.
At this stage, both init containers have successfully finished their tasks. The pods in the deployment will now be initialized and the status will change to PodInitializing
, and then eventually to Running
.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: