Kubernetes
Cluster & Context
bash
kubectl cluster-info # View cluster information
kubectl config current-context # Show current context
kubectl config use-context <context> # Switch context
kubectl get nodes # List all nodesViewing Resources
bash
kubectl get pods # List all pods
kubectl get pods -n <namespace> # List pods in specific namespace
kubectl get services # List all services
kubectl get deployments # List deployments
kubectl describe pod <pod-name> # Show detailed info about a pod
kubectl get all # List all resourcesCreating & Applying
bash
kubectl apply -f deployment.yaml # Apply/create resources from file
kubectl create deployment nginx --image=nginx # Create deployment imperatively
kubectl expose deployment nginx --port=80 --type=LoadBalancer # Expose serviceUpdating & Scaling
bash
kubectl scale deployment nginx --replicas=3 # Scale to 3 replicas
kubectl set image deployment/nginx nginx=nginx:1.19 # Update image
kubectl rollout status deployment/nginx # Check rollout progress
kubectl rollout undo deployment/nginx # Rollback to previous versionDebugging & Logs
bash
kubectl delete pod <pod-name> # Delete a pod
kubectl delete deployment <name> # Delete a deployment
kubectl delete -f deployment.yaml # Delete resources from fileAdvanced
bash
kubectl explain pods # Show resource documentation
kubectl top nodes # View resource usage
kubectl get events # View cluster eventsYAML Example
Pod
yaml
apiVersion: v1
kind: Pod
metadata:
name: simple-pod
labels:
app: myapp
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"Key fields:
apiVersion- API version for the resource (v1 for Pod)kind- Resource type (Pod)metadata.name- Name of the podmetadata.labels- Labels for organizing and selecting podsspec.containers- List of containers in the podname- Container nameimage- Docker image to useports- Exposed portsresources- CPU and memory requests/limits
To create this pod:
bash
kubectl apply -f pod.yamlDeployment
A Deployment manages multiple Pod replicas and provides rolling updates:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"Key fields:
spec.replicas- Number of Pod replicas to maintainspec.selector- Selects pods by labels (matches template labels)spec.template- Pod template for creating replicas
To create this deployment:
bash
kubectl apply -f deployment.yamlService
A Service exposes Pods to the network using a stable IP and load balancing:
yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: myapp
spec:
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: myappKey fields:
spec.type- Service type (ClusterIP, NodePort, LoadBalancer)spec.ports- Port mappingsport- Service porttargetPort- Pod container port
spec.selector- Routes traffic to pods matching these labels
To create this service:
bash
kubectl apply -f service.yamlHow They Work Together
Service (nginx-service)
↓ (selector: app: myapp)
Deployment (nginx-deployment)
↓ (creates 3 replicas)
Pods (with label: app: myapp)
├─ nginx-deployment-xxxxx (Port 80)
├─ nginx-deployment-yyyyy (Port 80)
└─ nginx-deployment-zzzzz (Port 80)- Deployment creates 3 nginx Pod replicas with label
app: myapp - Service uses selector
app: myappto find and route traffic to these Pods - Clients access the Service, which load-balances requests across the Pods
