Namespace Troubleshooting
This guide provides solutions for common namespace-related issues in Kubernetes clusters.
Common Namespace Issues
Section titled “Common Namespace Issues”Issue: Namespace Stuck in Terminating State
Section titled “Issue: Namespace Stuck in Terminating State”Symptoms:
- Namespace shows “Terminating” status for an extended period
kubectl delete namespacecommand hangs or fails
Diagnostic Commands:
# Check namespace statuskubectl get namespace <namespace-name> -o yaml
# Check for finalizers on the namespacekubectl get namespace <namespace-name> -o jsonpath='{.spec.finalizers}'Solutions:
See the Namespace Cleanup guide for detailed procedures.
Quick solution:
kubectl get namespace <namespace-name> -o json | jq '.spec.finalizers = []' > ns.jsonkubectl replace --raw "/api/v1/namespaces/<namespace-name>/finalize" -f ns.jsonIssue: Cannot Create Resources in Namespace
Section titled “Issue: Cannot Create Resources in Namespace”Symptoms:
- Error messages like “Error from server (Forbidden): error when creating…”
- Resources fail to create despite permissions
Diagnostic Commands:
# Check for resource quotaskubectl get resourcequota -n <namespace-name>
# Check for limit rangeskubectl get limitrange -n <namespace-name>
# Check for admission webhookskubectl get validatingwebhookconfigurations,mutatingwebhookconfigurationsSolutions:
-
Adjust resource quotas:
Terminal window kubectl edit resourcequota <quota-name> -n <namespace-name> -
Check webhook configurations for issues:
Terminal window kubectl get validatingwebhookconfigurations -o yaml | grep <namespace-name>
Issue: Namespace Not Showing Resources
Section titled “Issue: Namespace Not Showing Resources”Symptoms:
- Resources exist but don’t appear in listing commands
- Inconsistent behavior between different kubectl commands
Diagnostic Commands:
# List all resource types in the namespacekubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get -n <namespace-name> --show-kind --ignore-not-foundSolutions:
-
Check RBAC permissions:
Terminal window kubectl auth can-i --list -n <namespace-name> -
Verify your current context:
Terminal window kubectl config current-contextkubectl config view --minify
Advanced Troubleshooting
Section titled “Advanced Troubleshooting”Network Policy Issues
Section titled “Network Policy Issues”If pods can’t communicate within a namespace:
# List network policieskubectl get networkpolicy -n <namespace-name>
# Test connectivity between podskubectl run test-$RANDOM --rm -it --image=alpine -n <namespace-name> -- sh -c "ping <pod-ip>"Event Monitoring
Section titled “Event Monitoring”Monitor namespace events for clues:
# Watch namespace eventskubectl get events -n <namespace-name> --sort-by='.lastTimestamp'
# Monitor specific resource eventskubectl get events -n <namespace-name> --field-selector involvedObject.name=<resource-name>Resource Contention
Section titled “Resource Contention”If namespace has performance issues:
# Check resource usagekubectl top pod -n <namespace-name>
# Check for pending podskubectl get pods -n <namespace-name> | grep PendingChecking Namespace Security
Section titled “Checking Namespace Security”Review RBAC Configuration
Section titled “Review RBAC Configuration”# List roles and bindingskubectl get roles,rolebindings -n <namespace-name>
# Check who can do whatkubectl auth can-i --list --all-namespacesNetwork Policy Validation
Section titled “Network Policy Validation”# Check if network policies are enabledkubectl get pods -n kube-system | grep -i network
# List all network policies affecting the namespacekubectl get networkpolicy -A -o wide | grep <namespace-name>Namespace Auditing
Section titled “Namespace Auditing”Resource Inventory
Section titled “Resource Inventory”Take an inventory of namespace resources:
# Export all resources in a namespacemkdir -p backup/<namespace-name>kubectl api-resources --verbs=list --namespaced -o name | xargs -n1 -I{} sh -c "kubectl get {} -n <namespace-name> -o yaml > backup/<namespace-name>/{}.yaml"Resource Ownership Analysis
Section titled “Resource Ownership Analysis”Find resources without proper ownership:
kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 -I{} bash -c "kubectl get {} -n <namespace-name> -o json | jq -r '.items[] | select(.metadata.ownerReferences == null) | \"\(.kind) \(.metadata.name) has no owner\"'"Recovering from Serious Issues
Section titled “Recovering from Serious Issues”Creating a New Namespace with Same Resources
Section titled “Creating a New Namespace with Same Resources”When a namespace is irreparably damaged:
-
Export all resources:
Terminal window kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 -I{} kubectl get {} -n <damaged-namespace> -o yaml > resources.yaml -
Edit the resources.yaml file to clean up:
- Remove
resourceVersion,uid, and other auto-generated fields - Change namespace references to the new namespace
- Remove finalizers
- Remove
-
Create new namespace and apply resources:
Terminal window kubectl create namespace <new-namespace>kubectl apply -f cleaned-resources.yaml
Recovering from Unauthorized Changes
Section titled “Recovering from Unauthorized Changes”If a namespace has been modified without authorization:
# Check recent changeskubectl get events -n <namespace-name> --sort-by='.lastTimestamp'
# Review audit logs (if enabled in your cluster)kubectl logs -n kube-system -l k8s-app=kube-apiserver --tail=1000 | grep <namespace-name>Best Practices
Section titled “Best Practices”- Use Resource Limits: Always set resource requests and limits
- Implement Network Policies: Restrict traffic between namespaces
- Regular Auditing: Periodically review namespace resources
- Use Labels and Annotations: Consistently label resources for easier management
- Namespace RBAC: Apply principle of least privilege for namespace access
