Skip to content

How do I troubleshoot namespaces that are stuck in the Terminating state in my Amazon EKS cluster?

2 minute read
1

I tried to delete a namespace in my Amazon Elastic Kubernetes Service (Amazon EKS) cluster. However, the namespace is stuck in the Terminating state.

Short description

To delete a namespace, Kubernetes must first delete all the resources in the namespace. Then, Kubernetes checks registered API services to confirm that they removed all references to resources in that namespace.

A namespace might get stuck in Terminating state for the following reasons:

  • The namespace contains resources that Kubernetes can't delete because of finalizers.
  • An API service has a False status.

Resolution

Complete the following steps:

  1. To edit the configuration of your namespace that is stuck in the Terminating state, run the following command:

    kubectl edit namespace TERMINATING_NAMESPACE

    Note: Replace TERMINATING_NAMESPACE with the namespace that's stuck in the Terminating state.

  2. Remove the lines that include the finalizers in your configuration.
    Example:

    finalizers:
    - kubernetes
  3. To apply the changes, save and close the text editor. If you use VIM, then type :wq and then press Enter.

  4. To confirm that the namespace in the Terminating state is removed, run the following command:

    kubectl get namespaces

Repeat the preceding steps for any remaining namespaces that are stuck in the Terminating state.

Related information

Namespaces on the Kubernetes website

Finalizers on the Kubernetes website

AWS OFFICIALUpdated 3 months ago
4 Comments

Your friendly one-liner. Make sure you don't terminate the wrong thing. I put in a safety check to make sure the variable is at least not empty. Just set TERMINATING_NAMESPACE at the beginning (I had a few to delete, hence the automation).

TERMINATING_NAMESPACE=bad-namespace-here && echo "Force Terminating: ${TERMINATING_NAMESPACE}..." && echo "${TERMINATING_NAMESPACE}" != "" && kubectl get namespace ${TERMINATING_NAMESPACE} -o json | jq 'del(.spec.finalizers)' | kubectl replace --raw "/api/v1/namespaces/${TERMINATING_NAMESPACE}/finalize" -f -
replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR
replied 3 years ago

Sometimes even after going through the steps (1-4) mentioned in the repost article, the namespace remains stuck in terminating status. However, I was able to use the one-liner command provided in the comment by Rob to delete the same namespace that was stuck in terminating status and can confirm that it works.

As far as I can tell, the one-liner command does the same thing as the steps provided in the article (which is remove the finalizers from the object) but just in one line. The only requirement would be to install jq, if you don't have it installed already (yum install jq).

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT
replied 2 years ago