
Introduction
Managing a Kubernetes environment requires expertise in handling Pods, Clusters, Services, Monitoring, Namespaces, Deployments, Configurations, and Secrets. This guide covers essential commands and real-world examples to help DevOps professionals streamline Kubernetes management.
1️⃣ Pod Management
Pods are the smallest deployable units in Kubernetes. Managing Pods efficiently ensures application reliability and scalability.
🔹 Creating a Pod
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f pod.yaml
🔹 Viewing Pod Status
kubectl get pods
Output:
NAME READY STATUS RESTARTS AGE
my-app-pod 1/1 Running 0 10s
🔹 Describe Pod
kubectl describe pod my-app-pod
🔹 Deleting a Pod
kubectl delete pod my-app-pod
2️⃣ Cluster Management
A Kubernetes cluster consists of nodes that run containerized applications.
🔹 View Cluster Info
kubectl cluster-info
🔹 List Nodes
kubectl get nodes
Output:
NAME STATUS ROLES AGE VERSION
worker-node-1 Ready <none> 10d v1.26.0
🔹 Drain a Node for Maintenance
kubectl drain worker-node-1 --ignore-daemonsets --delete-emptydir-data
🔹 Uncordon a Node (Re-enable scheduling)
kubectl uncordon worker-node-1
3️⃣ Service Management
Services expose applications running in pods within the cluster or to external users.
🔹 Create a Service
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
kubectl apply -f service.yaml
🔹 List Services
kubectl get services
🔹 Describe a Service
kubectl describe service my-service
🔹 Delete a Service
kubectl delete service my-service
4️⃣ Resource Monitoring
Monitoring ensures cluster health and resource optimization.
🔹 View Pod Logs
kubectl logs my-app-pod
🔹 Monitor Live Logs
kubectl logs -f my-app-pod
🔹 View Resource Usage
kubectl top pod
Output:
NAME CPU(cores) MEMORY(bytes)
my-app-pod 100m 200Mi
5️⃣ Namespace Management
Namespaces help manage different environments within a cluster.
🔹 List Namespaces
kubectl get namespaces
🔹 Create a Namespace
kubectl create namespace dev-environment
🔹 Delete a Namespace
kubectl delete namespace dev-environment
6️⃣ Deployment Management
Deployments manage updates and scaling of applications.
🔹 Create a Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f deployment.yaml
🔹 List Deployments
kubectl get deployments
🔹 Scale a Deployment
kubectl scale deployment my-app-deployment --replicas=5
🔹 Delete a Deployment
kubectl delete deployment my-app-deployment
7️⃣ Configuration & Secrets Management
🔹 Create a ConfigMap
kubectl create configmap app-config --from-literal=ENV=production
🔹 View ConfigMaps
kubectl get configmaps
🔹 Create a Secret
kubectl create secret generic db-secret --from-literal=username=admin --from-literal=password=securepass
🔹 View Secrets
kubectl get secrets
❓ FAQs
1️⃣ How do I restart all pods in a namespace?
kubectl delete pods --all -n my-namespace
2️⃣ How do I scale a deployment?
kubectl scale deployment my-app-deployment --replicas=5
3️⃣ How do I check logs of a failing pod?
kubectl logs -f my-app-pod
4️⃣ How do I check cluster events?
kubectl get events --sort-by=.metadata.creationTimestamp
5️⃣ How do I find which node a pod is running on?
kubectl get pod my-app-pod -o wide
6️⃣ How do I restart a pod without deleting it?
kubectl rollout restart deployment my-app-deployment
🎯 Conclusion
Managing Kubernetes effectively requires understanding pods, clusters, services, monitoring, namespaces, deployments, and secrets. This guide provided real-world examples, troubleshooting steps, and best practices.
Stay tuned for more Kubernetes deep dives! 🚀🔥
📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!
Originally published on Medium.