There are some applications that need to be deployed on Kubernetes cluster and these apps have some pre-requisites where some configurations need to be changed before deploying the app container. Some of these changes cannot be made inside the images so the DevOps team has come up with a solution to use init containers to perform these tasks during deployment. Below is a sample scenario that the team is going to test first.
Create a Deployment named as ic-deploy-xfusion.
-
Configure spec as replicas should be 1, labels app should be ic-xfusion, template's metadata lables app should be the same ic-xfusion.
-
The initContainers should be named as ic-msg-xfusion, use image fedora, preferably with latest tag and use command '/bin/bash', '-c' and 'echo Init Done - Welcome to xFusionCorp Industries > /ic/news'. The volume mount should be named as ic-volume-xfusion and mount path should be /ic.
-
Main container should be named as ic-main-xfusion, use image fedora, preferably with latest tag and use command '/bin/bash', '-c' and 'while true; do cat /ic/news; sleep 5; done'. The volume mount should be named as ic-volume-xfusion and mount path should be /ic.
-
Volume to be named as ic-volume-xfusion and it should be an emptyDir type.
Here's how to create the Kubernetes Deployment named ic-deploy-xfusion
with the specified requirements using init containers and main containers:
The YAML file below configures a Kubernetes Deployment with the required init containers, main containers, and volumes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ic-deploy-xfusion
labels:
app: ic-xfusion
spec:
replicas: 1
selector:
matchLabels:
app: ic-xfusion
template:
metadata:
labels:
app: ic-xfusion
spec:
initContainers:
- name: ic-msg-xfusion
image: fedora:latest
command: ["/bin/bash"]
args: ["-c", "echo Init Done - Welcome to xFusionCorp Industries > /ic/news"]
volumeMounts:
- name: ic-volume-xfusion
mountPath: /ic
containers:
- name: ic-main-xfusion
image: fedora:latest
command: ["/bin/bash"]
args: ["-c", "while true; do cat /ic/news; sleep 5; done"]
volumeMounts:
- name: ic-volume-xfusion
mountPath: /ic
volumes:
- name: ic-volume-xfusion
emptyDir: {}
-
Deployment Metadata:
name
: The deployment is namedic-deploy-xfusion
.labels
: Labels are set toapp: ic-xfusion
.
-
Deployment Specification:
replicas
: Specifies that only one replica should be created.selector
: Match labels ensure the pods managed by this deployment have theapp: ic-xfusion
label.
-
Pod Template Metadata:
labels
: Same labelapp: ic-xfusion
is used for pod templates to match the deployment selector.
-
Init Containers:
name
: The init container is namedic-msg-xfusion
.image
: Usesfedora:latest
.command
: Runs/bin/bash
with the specified-c
argument to echo a message to/ic/news
.volumeMounts
: Mounts the volumeic-volume-xfusion
to/ic
.
-
Main Container:
name
: The main container is namedic-main-xfusion
.image
: Usesfedora:latest
.command
: Runs/bin/bash
with the specified-c
argument to continuously read and display the contents of/ic/news
.volumeMounts
: Mounts the volumeic-volume-xfusion
to/ic
.
-
Volumes:
name
: Volume is namedic-volume-xfusion
.emptyDir
: Defines an empty directory volume that persists only for the lifetime of the pod.
To apply this configuration to your Kubernetes cluster, save the YAML to a file (e.g., ic-deploy-xfusion.yaml
) and then use kubectl
to apply it:
kubectl apply -f ic-deploy-xfusion.yaml
-
Check Deployment:
kubectl get deployments
-
Check Pods:
kubectl get pods
-
Check Logs:
To ensure the init container's message is properly written, you can check the logs of the main container:
kubectl logs <pod-name>
Replace
<pod-name>
with the actual name of the pod from thekubectl get pods
command.
By following these steps, you should have a deployment configured with init containers and main containers that will perform the required setup and continuously display the message.