Init Containers in Kubernetes

Hello everyone!!
I made a mistake but I couldn’t find it.
The question said.
"
There are some applications that need to be deployed on Kubernetes cluster and these apps do have some pre-requisites where some configuration need to be changes before deploying the app container. Some of these changes can not be made inside the images so DevOps team has come up with a solution to use init containers to perform these tasks during deployment. Below is a sample scenario team is going to test first.

  1. Create a Deployment named as ic-deploy-datacenter.

  2. Configure spec as replicas should be 1, labels app should be ic-datacenter, template’s metadata lables app should be same ic-datacenter.

  3. The initContainers should be names as ic-msg-datacenter, use image debian, preferably with latest tag and use command ‘/bin/bash’, ‘-c’ and ‘echo Init Done - Welcome to xFusionCorp Industries > /ic/official’. The volume mount should be named as ic-volume-datacenter and mount path should be /ic.

  4. Main container should be named as ic-main-datacenter, use image debian, preferably with latest tag and use command ‘/bin/bash’, ‘-c’ and ‘while true; do cat /ic/official; sleep 5; done’. The volume mount should be named as ic-volume-datacenter and mount path should be /ic.

  5. Volume to be named as ic-volume-datacenter and it should be an emptyDir type.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.
"
my proposed solution is

Where did I make the mistake?
Thanks.

Hello, elsebasan
I think you need to use all command instructions in the command field not in the args field. Might be the reason.

Hello.
Maybe. But this way works and do the same. It’s similar to the brabo’s solution to game of pods for the deployment drupal.
Thanks you @player001 for answer me.

@Inderpreet can you please advise? I don’t think it is wrong having the second part of the command at the “args” section.

Hi @elsebasan @Salim:
I’ll say, try this way,

command: [ “/bin/bash”]
args: [ “-c”, “while true; do cat /ic/official; sleep 5; done” ]

This way, it works.
Hope it will help. Thanks.

1 Like

Yes you are right. This is the correct way to use command and args as defined in the kubernetes documentation.

Common mistakes which everyone makes due to which mostly the tasks failed.


Hope this helps

I got a fail saying that the POD is not running, but apparently it is:

I can 't get over this error, could you please advise?
image
Thank you!

Did you mention volumemounts as well as volume in the yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: ic-deploy-devops
labels:
app: ic-devops
spec:
replicas: 1
selector:
matchLabels:
app: ic-devops
template:
metadata:
labels:
app: ic-devops
spec:
containers:
- name: ic-main-devops
image: ubuntu
command: ["/bin/bash", “-c”]
args: [‘while true; do cat /ic/beta; sleep 5; done’]
volumeMounts:
- mountPath: /ic
name: ic-volume-devops
initContainers:
- name: ic-msg-datacenter
image: ubuntu
command: ["/bin/bash", “-c”]
args: [‘echo Init Done - Welcome to xFusionCorp Industries > /ic/beta’]
volumeMounts:
- name: ic-volume-devops
mountPath: /ic
volumes:
- name: ic-volume-devops
emptyDir: {}

move “-c” from command to args in the volumeMounts. And try using double-quotes for both command and args. See my example that I had posted earlier

Hello, @victorpantea
I just tested your yaml file and it worked without any issue.

apiVersion: apps/v1
kind: Deployment
metadata:
   name: ic-deploy-devops
   labels:
    app: ic-devops
spec:
  replicas: 1
  selector:
   matchLabels:
    app: ic-devops
  template:
   metadata:
    labels:
      app: ic-devops
   spec:
    containers:
     - name: ic-main-devops
       image: ubuntu
       command: ["/bin/bash", "-c"]
       args: ['while true; do cat /ic/beta; sleep 5; done']
       volumeMounts:
        - mountPath: /ic
          name: ic-volume-devops
    initContainers:
    - name: ic-msg-datacenter
      image: ubuntu
      command: ["/bin/bash", '-c']
      args: ['echo Init Done - Welcome to xFusionCorp Industries > /ic/beta']
      volumeMounts:
      - name: ic-volume-devops
        mountPath: /ic
    volumes:
    - name: ic-volume-devops
      emptyDir: {}

The only thing that I have done was to create the pv ic-volume-devops ,

Means your issue got resolved? @victorpantea

Hello,
Yes, thank you all for the help.
I have failed on my own, by not paying attention to the pod names.