> ## Content Index
> Fetch the complete content index at: https://skills.devopscube.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Configure Init Containers and Resource Constraints in Kubernetes
- URL: https://skills.devopscube.com/add-an-init-container-with-resource-constraints/
- Published: 2026-09-17T14:50:40.000Z
- Updated: 2026-10-14T14:00:03.000Z
- Author: Sulaiman
- Tags: #CKA-Exam-question, #blog, #docs, Workloads & Scheduling

You are a Kubernetes administrator managing a cluster used by DevOpscube. A Pod named `devopscube-web` is already running in the cluster.

The application team requires an initialization step to be completed before the NGINX application starts. The Pod must also have appropriate CPU and memory resource constraints configured for both the init container and the application container.

Your task is to retrieve the existing Pod configuration and modify it to meet the application's requirements.

Configure the Pod with an init container named `init-permissions` using the `busybox` image. The init container should print a permissions setup message, wait for 5 seconds, and then complete successfully.

Apply the following resource constraints to the init container:

- CPU request: `50m`
- Memory request: `64Mi`
- CPU limit: `100m`
- Memory limit: `128Mi`

Configure the existing NGINX container with:

- CPU request: `100m`
- Memory request: `128Mi`
- CPU limit: `200m`
- Memory limit: `256Mi`

After making the changes, redeploy the Pod and verify that:

- The init container executes successfully before the NGINX container starts.
- The init container has the required resource requests and limits.
- The NGINX container has the required resource requests and limits.
- The Pod reaches the Running state.

The initial Pod can be created using:

```sh
kubectl run devopscube-web --image=nginx --port=80
```

📚

For more details about [Init Container](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/?ref=skills.devopscube.com),[Resource](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/?ref=skills.devopscube.com) refer to the official Kubernetes documentation.

---

### Export the Existing Pod Manifest

The Pod is already running, so first export its current manifest to a YAML file:

```
kubectl get pod devopscube-web -o yaml > web-init.yaml
```

This saves the Pod's current configuration in `web-init.yaml` for modification.

```
$ cat web-init.yaml

apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: ced71426ef705086538156eeff5f87262c5386bd04b613261ecc4f78ae900de3
    cni.projectcalico.org/podIP: 10.244.140.91/32
    cni.projectcalico.org/podIPs: 10.244.140.91/32
  creationTimestamp: "2026-09-15T06:23:38Z"
  generation: 1
  labels:
    run: devopscube-web
  name: devopscube-web
  namespace: default
  resourceVersion: "808198"
  uid: 6e14b3e2-0946-478d-a1c2-26148a7c45bb
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: devopscube-web
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-hthgk
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: node02
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
```

⚠️

When you export a live Pod, Kubernetes includes runtime-generated fields such as `status`, `resourceVersion`, `uid`, `creationTimestamp`, and `managedFields`. These fields are not usually needed when recreating the Pod.   
  
For CKA tasks, use the exported YAML as a starting point and keep only the fields required to recreate the Pod.

### Modify the Pod Manifest

Edit `web-init.yaml` and configure the Pod to include the required init container:

web-init.yaml after Copy 

apiVersion: v1 kind: Pod metadata:  name: devopscube-web  labels:  run: devopscube-web spec:  initContainers:  \- name: init-permissions  image: busybox  command: \["/bin/sh", "-c"\]  args:  \- echo "Setting up permissions" && sleep 5  resources:  requests:  cpu: "50m"  memory: "64Mi"  limits:  cpu: "100m"  memory: "128Mi"  containers:  \- name: devopscube-web  image: nginx  ports:  \- containerPort: 80  resources:  requests:  cpu: "100m"  memory: "128Mi"  limits:  cpu: "200m"  memory: "256Mi" 

### Understand the Manifest

The Pod has two containers: an init container and the main `nginx` container. The `init-permissions` container runs first, performs its setup tasks, waits for 5 seconds, and then completes successfully. 

Only after the init container finishes does the `nginx` container start and continue running as the main application container.

### Understanding Resource Requests and Limits

Each container has resource requests and limits. A request tells Kubernetes how much CPU and memory the container needs for scheduling, while a limit sets the maximum resources the container can use. 

In this example, both the init container and NGINX have requests lower than their limits. 

💡

****Important:** Kubernetes uses ****requests** for scheduling a Pod on a suitable node, while ****limits** cap how much CPU and memory a container can use. Always ensure ****Request ≤ Limit** for each resource.

### Redeploy the Modified Pod

Since fields such as `initContainers` and container resources are not typically modified in place, delete the existing Pod and recreate it from the updated manifest.First delete the existing Pod:

```
$ kubectl delete pod devopscube-web

pod "devopscube-web" deleted from default namespace
```

Then create the modified Pod:

```
$ kubectl apply -f web-init.yaml

pod/devopscube-web created
```

### Verify the Init Container

Watch the Pod to confirm that the init container completes before the main container starts:

```
$ kubectl get pod devopscube-web -w

NAME             READY   STATUS     RESTARTS   AGE
devopscube-web   0/1     Init:0/1   0          7s
devopscube-web   0/1     PodInitializing   0          9s
devopscube-web   1/1     Running           0          11s
```

During startup, you may see `0/1 Init:0/1` while the init container is running. After it completes successfully, the Pod changes to `1/1 Running`. The exact timing may vary slightly.

The important sequence is that the Pod starts with the init container in the state. The container runs and must complete successfully before the NGINX container starts. Once it finishes, NGINX starts, and the Pod reaches Running status.

### Check Init Container Logs

Check the logs to verify that the init container ran successfully:

```
$ kubectl logs devopscube-web -c init-permissions

Setting up permissions
```

This confirms that the init container executed successfully.

### Verify Resource Configuration

Run the following command to verify the Pod's resource configuration:

```
kubectl describe pod devopscube-web
```

Under the container sections, verify the resource configuration.

You should see the init container configured with:

```
Requests:
  cpu:     50m
  memory:  64Mi

Limits:
  cpu:     100m
  memory:  128Mi
```

And the NGINX container configured with:

```
Requests:
  cpu:     100m
  memory:  128Mi

Limits:
  cpu:     200m
  memory:  256Mi
```

## CKA Exam Tips

These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.

### 1\. Don't Confuse Init Completion with Pod Completion

The init container should finish successfully and show **`Completed`**, while the main NGINX container continues running. Therefore, the Pod should eventually show **`1/1 Running`** after the init container completes.

### 2\. Remember the Location of Resources

Resource requests and limits are defined under the individual container's **`resources`** section. The same structure applies to both regular containers and init containers, with each container having its own CPU and memory configuration.