The Nautilus DevOps team is planning to deploy some micro services on Kubernetes platform. The team has already set up a Kubernetes cluster and now they want to set up some namespaces, deployments etc. Based on the current requirements, the team has shared some details as below:
Create a namespace named dev and deploy a POD within it. Name the pod dev-nginx-pod and use the nginx image with the latest tag. Ensure to specify the tag as nginx:latest.
Note: The kubectl utility on the jump-host has been configured to work with the Kubernetes cluster.
Setup Kubernetes Namespaces and PODs: Complete Guide
Solution
Step 1: Create the Namespace
First, let's create the dev namespace:
kubectl create namespace dev
Expected output:
namespace/dev created
Step 2: Verify the Namespace
Check that the namespace was created:
kubectl get namespaces
Output:
NAME STATUS AGE
default Active 10d
dev Active 5s
kube-node-lease Active 10d
kube-public Active 10d
kube-system Active 10d
Step 3: Create the Pod in the dev Namespace
Now create the pod named dev-nginx-pod using the nginx:latest image in the dev namespace:
kubectl run dev-nginx-pod \
--image=nginx:latest \
--namespace=dev \
--restart=Never \
--port=80
Expected output:
pod/dev-nginx-pod created
Step 4: Verify the Pod
Check if the pod is running in the dev namespace:
kubectl get pods -n dev
Output:
NAME READY STATUS RESTARTS AGE
dev-nginx-pod 1/1 Running 0 10s
Step 5: Get Detailed Information
Check pod details:
kubectl describe pod dev-nginx-pod -n dev
Step 6: Verify Everything
Let's verify all requirements are met:
# Check pod in dev namespace
kubectl get pods -n dev --show-labels
# Check pod image
kubectl get pod dev-nginx-pod -n dev -o jsonpath='{.spec.containers[0].image}'
# Check pod name
kubectl get pod dev-nginx-pod -n dev -o jsonpath='{.metadata.name}'
Alternative Methods
Method 1: Using YAML Manifest (Recommended for Production)
Create a file named dev-namespace-pod.yaml:
# Create namespace
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
# Create pod in dev namespace
apiVersion: v1
kind: Pod
metadata:
name: dev-nginx-pod
namespace: dev
labels:
app: nginx
environment: dev
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
restartPolicy: Never
Apply the configuration:
kubectl apply -f dev-namespace-pod.yaml
Method 2: One-Liner with Namespace
Create namespace and pod in one line:
kubectl create namespace dev && kubectl run dev-nginx-pod --image=nginx:latest --namespace=dev --restart=Never
Method 3: Using YAML Generation
Generate YAML for the pod in the namespace:
kubectl run dev-nginx-pod \
--image=nginx:latest \
--namespace=dev \
--restart=Never \
--dry-run=client -o yaml > dev-nginx-pod.yaml
kubectl apply -f dev-nginx-pod.yaml
Verification Commands
Complete Verification Checklist
# 1. Check namespace exists
kubectl get namespace dev
# 2. List pods in dev namespace
kubectl get pods -n dev
# 3. Check pod status
kubectl get pod dev-nginx-pod -n dev -o wide
# 4. Check pod image
kubectl get pod dev-nginx-pod -n dev -o jsonpath='{.spec.containers[0].image}'
# 5. Check pod events
kubectl describe pod dev-nginx-pod -n dev | grep -A 5 Events
# 6. Check pod logs (if needed)
kubectl logs dev-nginx-pod -n dev
# 7. Check all resources in dev namespace
kubectl get all -n dev
Quick Status Check
# One command to see everything
kubectl get namespace dev && kubectl get pods -n dev
Pod Operations in the Namespace
Access the Nginx Web Server
# Port forward to access nginx
kubectl port-forward dev-nginx-pod 8080:80 -n dev
# Then access http://localhost:8080 in browser
# Or use curl in another terminal
curl http://localhost:8080
Execute Commands in the Pod
# Get a shell in the pod
kubectl exec -it dev-nginx-pod -n dev -- /bin/bash
# Check nginx version
kubectl exec dev-nginx-pod -n dev -- nginx -v
# List files in nginx web directory
kubectl exec dev-nginx-pod -n dev -- ls -la /usr/share/nginx/html
View Pod Logs
# View logs
kubectl logs dev-nginx-pod -n dev
# Follow logs in real-time
kubectl logs -f dev-nginx-pod -n dev
# View previous logs (if pod restarted)
kubectl logs dev-nginx-pod -n dev --previous
Clean Up
Delete the Pod
# Delete the pod
kubectl delete pod dev-nginx-pod -n dev
Delete the Namespace (and all resources in it)
# Delete the namespace (will delete all resources in it)
kubectl delete namespace dev
Delete Using YAML
kubectl delete -f dev-namespace-pod.yaml
Advanced Namespace Management
1. Create Multiple Namespaces
kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace production
2. Set Default Namespace
# Set default namespace for current context
kubectl config set-context --current --namespace=dev
# Now all commands without -n will use dev namespace
kubectl get pods
3. Switch Between Namespaces
# View current context
kubectl config view --minify | grep namespace
# Switch to a different namespace
kubectl config set-context --current --namespace=staging
4. List All Pods in All Namespaces
kubectl get pods --all-namespaces
# or
kubectl get pods -A
5. Resource Quotas in Namespace
apiVersion: v1
kind: ResourceQuota
metadata:
name: dev-quota
namespace: dev
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "10"
6. Limit Ranges in Namespace
apiVersion: v1
kind: LimitRange
metadata:
name: dev-limits
namespace: dev
spec:
limits:
- max:
cpu: "2"
memory: 4Gi
min:
cpu: "100m"
memory: 128Mi
default:
cpu: "500m"
memory: 256Mi
defaultRequest:
cpu: "250m"
memory: 128Mi
type: Container
Namespace Best Practices
✅ DO's
-
Use namespaces for environment separation
-
dev,staging,production - Isolates resources and configurations
-
-
Use namespaces for team separation
-
team-engineering,team-data - Prevents resource conflicts
-
-
Apply resource quotas
- Prevents one namespace from consuming all resources
- Ensures fair resource distribution
-
Use RBAC with namespaces
- Limit access to specific namespaces
- Implement least privilege principle
-
Use labels for categorization
environment: devowner: team-engineering
❌ DON'Ts
-
Don't use default namespace for production
- Harder to manage and secure
- Can cause resource conflicts
-
Don't create too many namespaces
- Maintenance overhead
- Can become confusing
-
Don't ignore resource limits
- Can cause cluster instability
- Impacts other namespaces
-
Don't use namespaces for very small environments
- Overhead not worth it
- Use labels instead
Troubleshooting Namespace Issues
Issue 1: Pod Not Found
# Error: pod not found
# Solution: Check namespace
kubectl get pods -n dev
Issue 2: Namespace Not Found
# Error: namespace "dev" not found
# Solution: Create namespace
kubectl create namespace dev
Issue 3: Permission Denied
# Error: unauthorized to access namespace
# Solution: Check RBAC permissions
kubectl auth can-i get pods --namespace=dev
Issue 4: Resource Quota Exceeded
# Error: exceeded quota
# Solution: Check quota usage
kubectl describe quota -n dev
Issue 5: Pod Stuck in Pending
# Pod stays in Pending state
# Check events
kubectl describe pod dev-nginx-pod -n dev
Complete Example: Multi-Environment Setup
Here's a complete example with multiple namespaces:
# dev-namespace.yaml
---
apiVersion: v1
kind: Namespace
metadata:
name: dev
labels:
environment: development
type: non-production
---
apiVersion: v1
kind: Namespace
metadata:
name: staging
labels:
environment: staging
type: pre-production
---
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
environment: production
type: production
---
# Deploy dev pod
apiVersion: v1
kind: Pod
metadata:
name: dev-nginx-pod
namespace: dev
labels:
app: nginx
environment: dev
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
# Deploy staging pod (different image version)
apiVersion: v1
kind: Pod
metadata:
name: staging-nginx-pod
namespace: staging
labels:
app: nginx
environment: staging
spec:
containers:
- name: nginx
image: nginx:1.21.6
ports:
- containerPort: 80
Apply everything:
kubectl apply -f dev-namespace.yaml
Quick Reference Commands
Namespace Commands
# Create namespace
kubectl create namespace <name>
# List namespaces
kubectl get namespaces
# Delete namespace
kubectl delete namespace <name>
# Get namespace details
kubectl describe namespace <name>
Pod Commands with Namespace
# Create pod in namespace
kubectl run <pod-name> --image=<image> --namespace=<ns>
# List pods in namespace
kubectl get pods -n <ns>
# Get pod details
kubectl describe pod <pod-name> -n <ns>
# Delete pod from namespace
kubectl delete pod <pod-name> -n <ns>
# Get logs
kubectl logs <pod-name> -n <ns>
# Exec into pod
kubectl exec -it <pod-name> -n <ns> -- /bin/bash
Lab Completion Summary
✅ Task Requirements Checklist
- [x] Namespace created:
devnamespace exists - [x] Pod created:
dev-nginx-podindevnamespace - [x] Image specified: Using
nginx:latest - [x] Pod running: Status is
Runningwith 1/1 READY - [x] Proper tagging: Image tag is
nginx:latest
Final Verification Command
# Show everything in one go
echo "=== Namespaces ===" && \
kubectl get namespace dev && \
echo -e "\n=== Pods in dev namespace ===" && \
kubectl get pods -n dev && \
echo -e "\n=== Pod Details ===" && \
kubectl get pod dev-nginx-pod -n dev -o wide && \
echo -e "\n=== Container Image ===" && \
kubectl get pod dev-nginx-pod -n dev -o jsonpath='{.spec.containers[0].image}' && echo
Expected output:
=== Namespaces ===
NAME STATUS AGE
dev Active 1m
=== Pods in dev namespace ===
NAME READY STATUS RESTARTS AGE
dev-nginx-pod 1/1 Running 0 45s
=== Pod Details ===
NAME READY STATUS RESTARTS AGE IP NODE
dev-nginx-pod 1/1 Running 0 45s 10.22.0.10 jump-host
=== Container Image ===
nginx:latest
Success! 🎉
You have successfully:
- ✅ Created the
devnamespace - ✅ Deployed the
dev-nginx-podpod withnginx:latestimage - ✅ Verified the pod is running in the correct namespace
- ✅ Demonstrated various operations within the namespace
The Nautilus DevOps team's requirement has been fully satisfied!
Top comments (0)