Namespace Lifecycle Management
Namespace Creation Strategies
Creating namespaces can be accomplished through multiple methods:
## Declarative method
kubectl create namespace production
## YAML file method
kubectl apply -f namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: staging
Namespace Resource Tracking
stateDiagram-v2
[*] --> Created
Created --> Active
Active --> Terminating
Terminating --> [*]
Namespace Deletion Process
Deleting a namespace removes all resources within it:
## Delete namespace
kubectl delete namespace staging
## Force delete if stuck
kubectl delete namespace staging --grace-period=0 --force
Namespace Deletion Scenarios
Scenario |
Action |
Command |
Normal Deletion |
Graceful termination |
kubectl delete ns <name> |
Stuck Namespace |
Force deletion |
kubectl delete ns <name> --force |
Finalizer Issues |
Manual intervention |
kubectl patch ns <name> -p '{"metadata":{"finalizers":null}}' |
Handling Stuck Namespaces
When a namespace becomes stuck during deletion:
## Inspect namespace status
kubectl get namespace staging -o yaml
## Remove finalizers manually
kubectl patch namespace staging -p '{"metadata":{"finalizers":null}}'
Resource Cleanup Strategies
Implement namespace lifecycle management to prevent resource accumulation:
## List all namespaces
kubectl get namespaces
## Remove unused namespaces
kubectl delete namespace <unused-namespace>