Pod multi-container design patterns – Container Orchestration with Kubernetes-1

You can run multiple containers in pods in two ways – running a container as an init container or running a container as a helper container to the main container. We’ll explore both approaches in the following subsections.

Init containers

Init containers are run before the main container is bootstrapped, so you can use them to initialize your container environment before the main container takes over. Here are some examples:

  • A directory might require a particular set of ownership or permissions before you want to start your container using the non-root user
  • You might want to clone a Git repository before starting the web server
  • You can add a startup delay
  • You can generate configuration dynamically, such as for containers that want to dynamically connect to some other pod that it is not aware of during build time but should be during runtime

Tip

Use init containers only as a last resort, as they hamper the startup time of your containers.

Try to bake the configuration within your container image or customize it.

Now, let’s look at an example to see init containers in action.

To access the resources for this section, cd into the following:

$ cd ~/modern-devops/ch5/multi-container-pod/init/

Let’s serve the example.com website from our nginx web server. We will get the example.com web page and save it as index.html in the nginx default HTML directory before starting nginx.

Access the manifest file, nginx-init.yaml, which should contain the following:

spec:

containers:

  • name: nginx-container

image: nginx volumeMounts:

– mountPath: /usr/share/nginx/html

name: html-volume

initContainers:

  • name: init-nginx

image: busybox:1.28

command: [‘sh’, ‘-c’, ‘mkdir -p /usr/share/nginx/html && wget -O /usr/share/nginx/ html/index.html http://example.com’]

volumeMounts:

  • mountPath: /usr/share/nginx/html

name: html-volume

volumes:

  • name: html-volume

emptyDir: {}

Related Posts

Static provisioning – Managing Advanced Kubernetes Resources-2

As the Service resource is created, we can create a StatefulSet resource that uses the created PersistentVolume and Service resources. The StatefulSet resource manifest, nginx-manual-statefulset.yaml, looks like…

Static provisioning – Managing Advanced Kubernetes Resources-1

Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the…

StatefulSet resources – Managing Advanced Kubernetes Resources

StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and…

Managing stateful applications – Managing Advanced Kubernetes Resources

Imagine you’re a librarian in a magical library. You have a bunch of enchanted books that store valuable knowledge. Each book has a unique story and is…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-2

Now, let’s autoscale this deployment. The Deployment resource needs at least 1 pod replica and can have a maximum of 5 pod replicas while maintaining an average…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-1

Imagine you’re the manager of a snack bar at a park. On a sunny day, lots of people come to enjoy the park, and they all want…

Leave a Reply

Your email address will not be published. Required fields are marked *