Kubernetes Operations¶
Prerequisites¶
- K3s installed and running
- kubectl available
Procedure: Apply a Manifest¶
When to use: Creating or updating Kubernetes resources (Pods, Deployments, Services).
Steps:
-
Apply a YAML file:
kubectl apply -f manifest.yaml -
Apply all files in a directory:
kubectl apply -f ./k8s/
Troubleshooting:
- "The connection to the server was refused": Check if K3s/K8s is running (
systemctl status k3s) or ifKUBECONFIGis set.
Procedure: Check Pod Status¶
When to use: Monitoring deployment rollout and health.
Steps:
-
List all pods in default namespace:
kubectl get pods -
List pods in all namespaces:
kubectl get pods -A -
Watch for changes (live update):
kubectl get pods -w
Troubleshooting:
- "ImagePullBackOff": Check image name and registry credentials. If using a local image, ensure
imagePullPolicyis set toNeverorIfNotPresentand the image exists on the node. - "CrashLoopBackOff": Application is crashing. Check logs. Use
kubectl logs <pod> --previousto see the logs of the last crashed instance. - "Pending": The pod cannot be scheduled. Run
kubectl describe pod <name>to check for resource limits (CPU/Memory) or Node Selectors/Taints.
Procedure: View Pod Logs¶
When to use: Debugging application errors.
Steps:
-
View logs:
kubectl logs <pod_name> -
View logs of a specific container (in multi-container pod):
kubectl logs <pod_name> -c <container_name> -
Follow logs:
kubectl logs -f <pod_name>
Troubleshooting:
- "Pod not found": Check if you are in the correct namespace (
-n <namespace>).
Procedure: Create a Namespace¶
When to use: Isolating resources for different environments or projects.
Steps:
-
Create namespace:
kubectl create namespace my-app -
Switch context to use it by default:
kubectl config set-context --current --namespace=my-app
Troubleshooting:
- "Already exists": No action needed, or check spelling.
Procedure: Expose a Service via NodePort¶
When to use: Making an internal application accessible from outside the cluster.
Steps:
-
Expose a deployment:
kubectl expose deployment my-dep --type=NodePort --port=80 --name=my-svc -
Find the assigned NodePort:
kubectl get svc my-svc
Troubleshooting:
- Cannot access port: Ensure firewall allows traffic on the assigned NodePort (30000-32767).
Procedure: Create an Ingress¶
When to use: Exposing HTTP/HTTPS services via a domain name.
Steps:
-
Create
ingress.yaml:apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress spec: rules: - host: app.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-svc port: number: 80 -
Apply it:
kubectl apply -f ingress.yaml
Troubleshooting:
- 404 Not Found: Ensure the Ingress Controller (Traefik/Nginx) is running and DNS points to the cluster IP.
Procedure: Debug a Failing Pod¶
When to use: Detailed investigation of why a pod is pending or failing.
Steps:
-
Describe the pod events and status:
Look at the "Events" section at the bottom.kubectl describe pod <pod_name> -
Exec into the pod (if running):
kubectl exec -it <pod_name> -- /bin/sh
Troubleshooting:
- "SchedulingFailed": Insufficient CPU/Memory resources on nodes.
Quick Reference¶
| Action | Command |
|---|---|
| Get Pods | kubectl get pods |
| Get All | kubectl get all |
| Describe | kubectl describe <res> <name> |
| Logs | kubectl logs <pod> |
| Apply | kubectl apply -f <file> |
| Delete | kubectl delete -f <file> |
| Exec | kubectl exec -it <pod> -- sh |
Related Documentation¶
- Technologies: Kubernetes
- Concepts: Container Orchestration