Skip to content

Latest commit

 

History

History
259 lines (172 loc) · 5.07 KB

common-kubernetes-commands.md

File metadata and controls

259 lines (172 loc) · 5.07 KB

Below is a list of the most common kubectl commands you'll frequently use to manage your setup:


Cluster Information and Health

Check cluster components (control plane availability):

kubectl get componentstatuses

Get general cluster information:

kubectl cluster-info

List all nodes in the cluster:

kubectl get nodes

Get detailed information about a node:

kubectl describe node <node-name>

Workload Management

View all pods across all namespaces:

kubectl get pods --all-namespaces

List the pods in a specific namespace (e.g., default, longhorn-system):

kubectl get pods -n <namespace>

Get detailed information for a specific pod:

kubectl describe pod <pod-name> -n <namespace>

Delete a pod (useful for forcing a restart):

kubectl delete pod <pod-name> -n <namespace>

Create or apply resources from a YAML file (deploy manifests, services, etc.):

kubectl apply -f <filename>.yaml

Get logs from a pod (basic logs for debugging):

kubectl logs <pod-name> -n <namespace>

Stream continuous logs from a pod (for dynamic updates):

kubectl logs -f <pod-name> -n <namespace>

Get logs for a specific container in a multi-container pod:

kubectl logs <pod-name> -c <container-name> -n <namespace>

Service & Endpoint Management 13. List all services in a namespace:

kubectl get svc -n <namespace>

Get detailed information about a service:

kubectl describe svc <service-name> -n <namespace>

Forward a local port to a port within a pod (e.g., for accessing a specific pod's service locally like a database):

kubectl port-forward <pod-name> <local-port>:<remote-port> -n <namespace>

Storage Management (Longhorn) 16. List Longhorn volumes:

kubectl get volumes -n longhorn-system

Describe a Longhorn volume:

kubectl describe <longhorn-volume-name> -n longhorn-system

Check the status of Longhorn-csi or any stateful sets:

kubectl get statefulsets -n longhorn-system

Namespace Management

List all namespaces:

kubectl get namespaces

Switch context to a different namespace:
Change the default namespace to avoid having to specify -n <namespace> each time:

kubectl config set-context --current --namespace=<namespace>

PostgreSQL Management (example provider)

List PostgreSQL-related resources (assuming you have some CRDs or kubedb/k8s-postgres-operator):

kubectl get postgresql -n <namespace>

Describe a PostgreSQL instance, if any:

kubectl describe postgresql <pg-instance-name> -n <namespace>

Connect to the PostgreSQL pod for database debugging:

kubectl exec -it <pg-pod-name> -n <namespace> -- psql -U postgres

Resource & Utilization Monitoring

View resource usage (CPU/Memory) for nodes and pods (if metrics-server is installed):

kubectl top nodes
kubectl top pods -n <namespace>

Check events for troubleshooting issues within a namespace:

kubectl get events -n <namespace>

Get details about a Deployment:

kubectl describe deployment <deployment-name> -n <namespace>

Scale Deployments

Scale up/down the number of replicas in a Deployment:

kubectl scale deployment <deployment-name> --replicas=<number-of-replicas> -n <namespace>

Debugging & Troubleshooting

Check recent events to diagnose issues:

kubectl get events --sort-by='.metadata.creationTimestamp' -n <namespace>

Open a shell session inside a running container:

kubectl exec -it <pod-name> -n <namespace> -- /bin/bash

Run one-off commands in a container (e.g., to run a curl command):

kubectl exec -it <pod-name> -n <namespace> -- curl <url>

Service Account Management (if interacting with APIs or permissions)

List all service accounts:

kubectl get serviceaccounts -n <namespace>

Get details about a specific service account:

kubectl describe serviceaccount <service-account-name> -n <namespace>

Configuration Management

View all ConfigMaps (commonly used for storing configuration data):

kubectl get configmap -n <namespace>

Describe a specific ConfigMap for details:

kubectl describe configmap <configmap-name> -n <namespace>

List Secrets (API keys, credentials, etc.):

kubectl get secrets -n <namespace>

Decode a base64-encoded Secret to reveal its content:

kubectl get secret <secret-name> -n <namespace> -o jsonpath="{.data.<secret-key>}" | base64 --decode

  • Always be careful when using commands like delete, especially if you're managing persistent services like Longhorn, PostgreSQL databases, etc., since it could lead to unintended data loss.
  • You may also install tools like kubectl krew to extend the functionality of kubectl.