CKA · Question #4
Perform the following tasks: Add an init container to hungry-bear (which has been defined in spec file /opt/KUCC00108/pod-spec-KUCC00108.yaml) The init container should create an empty file named…
The task requires modifying a Kubernetes Pod YAML definition to include an init container that creates a specific file in a shared volume, and then applying this updated configuration to create the pod.
Question
Exhibit
Explanation
The task requires modifying a Kubernetes Pod YAML definition to include an init container that creates a specific file in a shared volume, and then applying this updated configuration to create the pod.
Approach. The correct approach involves a sequence of steps to modify the specified pod definition and then deploy it:
-
Access the Pod Specification File: Since the user is
root, they can directly edit the file. Open the YAML file using a text editor likeviornano:vi /opt/KUCC00108/pod-spec-KUCC00108.yaml -
Add Volume Definition: Ensure a shared
volumeis defined within thespecsection of the Pod, which both the init container and the main container can mount. AnemptyDirvolume is suitable for this temporary shared storage.spec: volumes: - name: workdir-volume emptyDir: {} # ... existing initContainers (if any) and containers -
Add Init Container Definition: Insert the
initContainerssection at the same level ascontainerswithin thespecsection. The init container should:- Have a
name(e.g.,prepare-workdir). - Use a minimal
imagelikebusyboxto perform the file creation. - Execute a
commandtotouch /workdir/calm.txt. mountthe sharedworkdir-volumeat/workdir.
spec: # ... volumes section initContainers: - name: prepare-workdir image: busybox command: ["sh", "-c", "touch /workdir/calm.txt"] volumeMounts: - name: workdir-volume mountPath: /workdir # ... containers section - Have a
-
Mount Volume in Main Container: Ensure the primary container(s) of the pod also
mountthe sameworkdir-volumeat/workdir. This is crucial for the main application to 'detect' thecalm.txtfile.spec: # ... initContainers section containers: - name: hungry-bear # Or whatever the main container is named image: <your-image> volumeMounts: - name: workdir-volume mountPath: /workdir # ... other container properties -
Save the File: Save the changes to the YAML file (e.g., press
Esc, then:wqinvi). -
Create the Pod: Use
kubectl(or its aliask) to create the pod from the modified YAML file. Since the question says 'create the pod',k createis appropriate. If the pod might already exist and need an update,k applywould be more robust.k create -f /opt/KUCC00108/pod-spec-KUCC00108.yamlIfcreatefails because the pod already exists, delete it first (k delete -f ...) thencreate, or usek apply -f ....
Common mistakes.
- common_mistake. Common mistakes include:
- Forgetting Shared Volume: Not defining a shared
volumeand correspondingvolumeMountsfor both the init container and the main container. Without a shared volume, the file created by the init container would be lost once the init container completes, and the main container would not be able to 'detect' it. - Incorrect YAML Syntax/Indentation: YAML is sensitive to indentation. Incorrect spacing can lead to parsing errors when applying the manifest.
- Wrong Image/Command for Init Container: Using an image that doesn't contain the
shshell ortouchcommand, or specifying an incorrect command to create the file. - Incorrect Placement of
initContainers: Placing theinitContainerssection incorrectly in the YAML hierarchy (it must be underspec, at the same level ascontainers). - Not Creating the Pod: Modifying the file but forgetting to apply the changes to the Kubernetes cluster using
kubectl createorkubectl apply. - Attempting to Create File in Main Container: While technically possible, creating the file in the main container's entrypoint or pre-start hook would not meet the implicit requirement that 'If /workdir/calm.txt is not detected, the pod should exit' (i.e., the main containers shouldn't even start if the file isn't present). Init containers are designed for such prerequisites.
Concept tested. The core technical concepts tested are Kubernetes Init Containers, their lifecycle and purpose (executing setup tasks before main containers), the use of shared volumes between init containers and main containers, Kubernetes Pod YAML manifest structure, and basic kubectl command-line operations for resource management.
Reference. https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
Topics
Community Discussion
No community discussion yet for this question.
