Kubernetes: How to pass pipe character in readiness probe command
Asked Answered
K

1

8

I am having an issue with passing a pipe character | in readiness probe command.

I want to have a probe command:

curl --silent http://localhost:8080/actuator/health | grep --quiet -e '^{\"status\"\:\"UP\".*}$'

Here is how I have defined the probe:

# kubectl get pod my_pod -o yaml

readinessProbe:
  exec:
    command:
    - curl
    - --silent
    - http://localhost:8080/actuator/health
    - '|'
    - grep
    - --quiet
    - -e
    - '''^{\"status\"\:\"UP\".*}$'''

Readiness probe fails with a message:

Readiness probe failed: curl: option --quiet: is unknown curl: try 'curl --help' or 'curl --manual' for more information

The error can be reproduce when command is executed without pipe character |:

curl --silent http://localhost:8080/actuator/health grep --quiet -e '^{\"status\"\:\"UP\".*}$'

For some reason pipe is not interpreted by Kubernetes.

Can you please help me with passing pipe in deployment?

Khalsa answered 19/11, 2019 at 10:45 Comment(0)
R
23

Kubernetes doesn't run a shell to process commands on its own; it just runs them directly. The closest equivalent in a shell would be

curl '--silent' 'http://...' '|' 'grep' ...

That is, | here doesn't split two separate commands, because that's shell syntax; without a shell it becomes another parameter to curl, as do all of the words after it.

You need to provide the shell wrapper yourself:

readinessProbe:
  exec:
    command:
      - sh
      - -c
      - curl --silent http://localhost:8080/actuator/health | grep --quiet -e '^{\"status\"\:\"UP\".*}$'

You can use alternate YAML syntax to make this a little more readable. (> means to fold following lines into a single string; - means to strip leading and trailing whitespace.

readinessProbe:
  exec:
    command:
      - sh
      - -c
      - >-
         curl --silent http://localhost:8080/actuator/health |
         grep --quiet -e '^{\"status\"\:\"UP\".*}$'
Rule answered 19/11, 2019 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.