Amazon EKS (k8s) namespace 삭제가 안될 경우 (terminating status)

2024. 10. 15. 19:41AWS/Amazon EKS

728x90
SMALL
  • 개요
Amazon EKS에서 namespace를 삭제할 때 terminating으로 상태가 멈춰 있을 경우
namespace 및 current context 확인 기능 추가 (2024.10.08)
지우려는 대상 namespace가 있는 지 확인 기능 추가 (2024.10.15)

 

  • 스크립트
#!/bin/bash

# Define the stuck namespace as a variable
NAMESPACE=$1

# Check if the namespace is provided
if [ -z "$NAMESPACE" ]; then
  echo "Usage: $0 <namespace>"
  exit 1
fi

# Check if the namespace exists
if ! kubectl get namespace "$NAMESPACE" > /dev/null 2>&1; then
  echo "Namespace \"$NAMESPACE\" does not exist."
  exit 1
fi

# Get the current Kubernetes context
CURRENT_CONTEXT=$(kubectl config current-context)

# Display the current context and the namespace to be deleted
echo "Current Kubernetes context: $CURRENT_CONTEXT"
echo "The namespace you want to delete is: $NAMESPACE"

# Ask for user confirmation to proceed
read -p "Do you want to delete \"$NAMESPACE\" namespace? Type 'yes' to continue: " CONFIRMATION

# Check if the user input is 'yes'
if [ "$CONFIRMATION" != "yes" ]; then
  echo "Operation cancelled."
  exit 1
fi

# Export the namespace JSON to a temporary file
kubectl get namespace "$NAMESPACE" -o json > tempfile.json

# Remove the finalizers block from the JSON file
jq 'del(.spec.finalizers)' tempfile.json > tempfile_modified.json

# Apply the modified JSON file to remove the stuck namespace
kubectl replace --raw "/api/v1/namespaces/$NAMESPACE/finalize" -f ./tempfile_modified.json

# Check if the namespace is removed
kubectl get namespaces

# Clean up the temporary files
rm tempfile.json tempfile_modified.json

echo "Namespace $NAMESPACE should be removed."

 

  • 사용
# chmod +x ns_remove.sh
# ./ns_remove.sh <namespace_want_to_remove>
728x90
LIST