Kubernetes-questions
-
Create a yaml file for pod in cluster
-
Kubernetes objects
Workload objects define the applications and jobs running in the cluster. Pod: The smallest deployable unit in Kubernetes. A pod runs one or more containers. ReplicaSet: Ensures a specified number of pod replicas are running at any given time. Deployment: Manages ReplicaSets and provides declarative updates for pods and ReplicaSets. StatefulSet: Manages stateful applications with persistent storage and unique pod identities. DaemonSet: Ensures that a copy of a pod runs on all or specific nodes in the cluster. Job: Creates pods to perform a specific task and ensures they complete successfully. CronJob: Manages jobs that run on a schedule, similar to a cron task. -
Few Kubernetes commands:
-
Sceanrio based question Namespace call tata, there are many service under namespace. Some services failed no pro active alerts Get list of pods that are not in running state, and in-incomplete pods
-
Faulty node one our cluster service hosting in the cluster not responding auto to not fault, I have ip of the node, we wan’t to find the services running in node
-
List pods with nodes running
1) Command to list pods running in which node and which availability zone:
kubectl get pods -o custom-columns="POD:metadata.name,NODE:spec.nodeName" | tail -n +2 | while read pod node; do
echo -n "$pod $node "
kubectl get node "$node" -o jsonpath="{.metadata.labels.topology\.kubernetes\.io/zone}"
echo ""
done
2) List of nodes and how many pods running on them:
kubectl get po -o json --all-namespaces | \
jq '.items | group_by(.spec.nodeName) | map({"nodeName": .[0].spec.nodeName, "count": length}) | sort_by(.count)'
4) Getting pods that are continuously restarting (sorting them):
kubectl get pods --all-namespaces -o json | jq -r '.items | sort_by(.status.containerStatuses[0].restartCount) | reverse[] | [.metadata.namespace, .metadata.name, .status.containerStatuses[0].restartCount] | @tsv' | column -t
kubectl get pods -o=custom-columns='NAME:spec.containers[*].name,MEMREQ:spec.containers[*].resources.requests.memory,MEMLIM:spec.containers[*].resources.limits.memory,CPUREQ:spec.containers[*].resources.requests.cpu,CPULIM:spec.containers[*].resources.limits.cpu'
kubectl get nodes -o json | \
jq -r '.items[].status.addresses[]? | select (.type == "InternalIP") | .address' | \
paste -sd "\n" -
kubectl get events --all-namespaces --field-selector type=Warning -o wide
or
kubectl get events --all-namespaces --field-selector type!=Normal -o wide
Kubernetes Commands
1. Cluster Information
- View Cluster Information:
- List All Nodes:
- Describe a Node:
2. Pod Management
- List All Pods:
- List Pods in a Specific Namespace:
- View Pod Logs:
- View Logs for a Specific Container in a Pod:
- Describe a Pod:
- Execute a Command in a Pod:
- Delete a Pod:
3. Deployment Management
- List Deployments:
- Describe a Deployment:
- Scale a Deployment:
- Apply Changes from a YAML File:
- Roll Back a Deployment:
- View Deployment History:
4. Service and Networking
- List Services:
- Describe a Service:
- Port Forward a Service:
5. Namespace Management
- List Namespaces:
- Create a Namespace:
- Delete a Namespace:
6. Troubleshooting
- List All Events:
- Check the Status of All Resources:
- Debug a Node:
- Debug a Pod:
7. Resource Management
- Get Resource Usage (using Metrics Server):
- Delete a Resource:
- Edit a Resource:
8. Custom Resources and YAML Files
- Validate a YAML File:
- Delete All Resources Defined in a File:
- List Custom Resources (CRDs):
9. Utility Commands
- Switch Context:
- View Current Context:
- List All Contexts: