Environment Variables in Kubernetes

Hi team,

I created the pod as was indicated, but the task is failing:

Below my yaml file:

apiVersion: v1
kind: Pod
metadata:
name: envars-fieldref
labels:
purpose: demonstrate-envars
namespace: fieldref-namespace
spec:
containers:

  • name: fieldref-container
    image: nginx
    command: [‘/bin/sh’]
    args: [‘-c’, ‘while true; do echo -en ‘/n’; printenv NODE_NAME POD_NAME POD_NAMESPACE; printenv POD_IP POD_SERVICE_ACCOUNT; sleep 10; done;’]
    env:
    • name: NODE_NAME
      value: spec.nodeName
    • name: POD_NAME
      value: metadata.name
    • name: POD_NAMESPACE
      value: metadata.namespace
    • name: POD_IP
      value: status.podIP
    • name: POD_SERVICE_ACCOUNT
      value: spec.serviceAccountName

I checked and the printenv was ok.

Bu fail said:
image

This is a very common issue which we have observed with most of the Kubernetes related tasks. The validation checks for command without any arguments. Therefore you should have used args in the same command line instead of defining it separately.

1 Like

I think the way you assigned values to your enVars is not the proper way. I believe you must use fieldRef:

1 Like

Thanks @kleansoul will try that way next time.

hi @bladimirh, sorry for inconvenience this is marked success for you

1 Like

Hi @rahul456 @Inderpreet @kodekloud-support3

My task Print Environment Variables failed with the below error.

Task Status - Failed
command ‘echo’ is not used

Please check the below output as the kubectl logs shows the correct output using echo.

thor@jump_host ~$ kubectl get pods
NAME                    READY   STATUS      RESTARTS   AGE
print-envars-greeting   0/1     Completed   0          25s

thor@jump_host ~$ kubectl logs -f print-envars-greeting
Welcome to Stratos Datacenter

Manifest file also has echo command and related arguments configured as requested in task.

command: ["/bin/echo"]
args: ["$(GREETING) $(COMPANY) $(GROUP)"]

Please check and update. Thank you!

hi @kleansoul, was this task assigned to you today or its old one? If its old one did you already have today’s task in your bucket ?

@rahul456

Print Environment Variables Aug 18, 2020 3:00:29 PM Aug 26, 2020 3:00:29 PM

This is a task assigned to me 1hr ago which I completed immediately but failed. The validation seems to be incorrect, therefore I requested you to recheck.

@kleansoul, this is marked pending for you, please give it an another try.

After providing all the evidence of the task, I was hoping you would have marked success.

Anyways I again attempted with the same solution and now its successful. Thank you for fixing the issue. :+1:

Thank you so much @rahul456

@rahul456 @mmumshad , HI I feel there are couple of issues with below line:
echo -en ‘/n’

this is how its getting printed:
kubectl logs envars-fieldref -n fieldref-namespace
-en /n
node01
envars-fieldref
fieldref-namespace
10.44.0.1
default

As you can see, getting “-en /n” string in the output

If I just keep echo ‘\n’; in the args, we get correct new line character in the output:

thor@jump_host ~$ kubectl logs envars-fieldref -n fieldref-namespace

node01
envars-fieldref
fieldref-namespace
10.44.0.1
default

node01
envars-fieldref
fieldref-namespace
10.44.0.1
default

hi @salilpune1, did it fail for you ?

@Inderpreet and @rahul456
My task failed can you please check why it faild. Thank you

@nashwan, can you please share your pod template ?

@rahul456 actually I think I know the problem I think the question asked me about redis image if I am not wrong. unfortunately haven’t captured screen the question and the pod template from what I submitted. would still appreciate if you can check and verify? thank you heaps!

@nashwan, this is marked pending for you, please give it an another try.

@rahul456 thank you very much, really appreciate your support.

Nope… It worked. Thanks.