Skip to main content

exit-code-127

Exit Code 127 in Kubernetes​

What is Exit Code 127?​

Exit Code 127 occurs when a command or executable that is specified in a Kubernetes Pod is not found within the container. This typically indicates that the command is either misspelled or does not exist in the image.

Why Do We Get This Error?​

You may encounter Exit Code 127 due to several reasons, including:

  • Misspelled Command: The command you intended to run may have been typed incorrectly.
  • Nonexistent Executable: The executable you are trying to run does not exist in the container's filesystem.
  • Path Issues: The command may not be present in the directories listed in the PATH environment variable.

Simulation of Exit Code 127​

  1. Pod Configuration: We will create a Kubernetes Pod that attempts to execute a non-existent script.

    File Name: exit-code-127-simulation.yaml Configuration:

    apiVersion: v1
    kind: Pod
    metadata:
    name: exit-code-127-simulation
    spec:
    containers:
    - name: exit-code-127
    image: alpine:latest
    command: ["/bin/sh", "-c", "/non-exist-script.sh"] # This script does not exist
    restartPolicy: OnFailure
  2. Deploy the Simulation: To deploy the above configuration, run the following command:

    kubectl apply -f exit-code-127-simulation.yaml
  3. Expected Outcome: When you apply this configuration, the Pod will fail to start due to the missing script, and you can check the logs with:

    kubectl logs exit-code-127-simulation

    The output will indicate that the command was not found:

    /bin/sh: /non-exist-script.sh: not found

Fixing Exit Code 127​

To resolve the Exit Code 127 error, you need to ensure that the command or script you want to run exists in the container image.

  1. Updated Pod Configuration: Here’s the updated configuration that runs a valid command.

    File Name: exit-code-127-fix.yaml Configuration:

    apiVersion: v1
    kind: Pod
    metadata:
    name: exit-code-127-fix
    spec:
    containers:
    - name: exit-code-127
    image: alpine:latest
    command: ["echo", "This command exists!"] # Valid command that exists
    restartPolicy: OnFailure
  2. Deploy the Fix: To deploy the fixed configuration, run the following command:

    kubectl apply -f exit-code-127-fix.yaml
  3. Expected Outcome: With this configuration, when you apply it and check the logs, you will see the successful execution of the command:

    kubectl logs exit-code-127-fix

    The output will be:

    This command exists!

How to Prevent Exit Code 127 in the Future​

To avoid encountering Exit Code 127 in future deployments, consider the following best practices:

  • Verify Command Availability: Always check that the command or executable you intend to use is included in the container image.
  • Check Spelling and Path: Ensure that commands are correctly spelled and that any paths used to execute files are valid.
  • Testing in Local Environment: Test the commands in a local environment that closely matches the container to ensure they work as expected.
  • Use Multi-Stage Builds: If your application requires certain tools or binaries, consider using multi-stage builds in your Dockerfile to ensure the necessary files are present in your final image.

Conclusion​

Exit Code 127 is a common issue that occurs when a specified command or file cannot be found within a Kubernetes Pod. By ensuring that commands are valid and available within your container images, you can prevent this error from disrupting your deployments. This guide has provided a simulation of the error and a straightforward fix, along with strategies to help you avoid this issue in the future.