Wednesday, March 5, 2025

kubernetes basic commands

   

🔹 Basic Commands

kubectl version                           # Check kubectl & server version

kubectl cluster-info                      # Get cluster information

kubectl get nodes                         # List all nodes in the cluster

kubectl get pods                          # List all pods in the default namespace

kubectl get services                      # List all services in the default namespace

kubectl config get-contexts               # List all available contexts

kubectl config use-context <context-name> # Switch between clusters

kubectl describe node <node-name>         # Get details of a specific node


🔹 Namespace Management

kubectl get namespaces                    # List all namespaces

kubectl create namespace <namespace-name> # Create a new namespace

kubectl delete namespace <namespace-name> # Delete a namespace

kubectl config set-context --current --namespace=<namespace>  # Set default namespace


🔹 Pod Management

kubectl get pods -o wide                  # Get pod details with node info

kubectl describe pod <pod-name>           # Describe pod details

kubectl logs <pod-name>                   # View logs of a pod

kubectl logs -f <pod-name>                # View live logs of a pod

kubectl exec -it <pod-name> -- /bin/sh    # Access pod shell (use /bin/bash if available)

kubectl delete pod <pod-name>             # Delete a specific pod

kubectl delete pod --all                  # Delete all pods


🔹 Deployments & ReplicaSets

kubectl get deployments                   # List all deployments

kubectl describe deployment <deployment-name> # Describe a deployment

kubectl scale deployment <deployment-name> --replicas=3 # Scale deployment

kubectl rollout restart deployment <deployment-name> # Restart a deployment

kubectl rollout status deployment <deployment-name>   # Check rollout status

kubectl delete deployment <deployment-name> # Delete a deployment


🔹 Service & Networking

kubectl get services                      # List all services

kubectl describe service <service-name>   # Get details of a service

kubectl expose deployment <deployment-name> --type=NodePort --port=80  # Expose a deployment

kubectl port-forward pod/<pod-name> 8080:80  # Forward port 8080 to pod port 80

kubectl delete service <service-name>     # Delete a service


🔹 ConfigMaps & Secrets

kubectl create configmap my-config --from-literal=key1=value1 # Create ConfigMap

kubectl get configmaps                      # List all ConfigMaps

kubectl describe configmap my-config        # Describe a ConfigMap

kubectl delete configmap my-config          # Delete a ConfigMap


kubectl create secret generic my-secret --from-literal=password=mypassword  # Create a Secret

kubectl get secrets                         # List all Secrets

kubectl describe secret my-secret           # Describe a Secret

kubectl delete secret my-secret             # Delete a Secret


🔹 Persistent Volume & Storage

kubectl get pv                            # List Persistent Volumes

kubectl get pvc                           # List Persistent Volume Claims

kubectl delete pvc <pvc-name>             # Delete a PVC


🔹 Jobs & CronJobs

kubectl get jobs                          # List Jobs

kubectl describe job <job-name>           # Describe a Job

kubectl delete job <job-name>             # Delete a Job


kubectl get cronjobs                      # List CronJobs

kubectl delete cronjob <cronjob-name>     # Delete a CronJob


🔹 Ingress Management

kubectl get ingress                       # List all ingress rules

kubectl describe ingress <ingress-name>   # Get details of an ingress rule

kubectl delete ingress <ingress-name>     # Delete an ingress rule


🔹 Monitoring & Debugging

kubectl top nodes                         # Show CPU & memory usage of nodes

kubectl top pods                          # Show CPU & memory usage of pods

kubectl events                            # Show cluster events

kubectl get events --sort-by='.lastTimestamp'  # Get events sorted by time

kubectl get pods --field-selector=status.phase=Pending  # Get all pending pods


🔹 Role-Based Access Control (RBAC)

kubectl get roles                         # List all roles

kubectl get rolebindings                   # List role bindings

kubectl describe role <role-name>         # Describe a role

kubectl delete role <role-name>           # Delete a role


🔹 Apply YAML Configurations

kubectl apply -f <file>.yaml              # Apply a configuration file

kubectl delete -f <file>.yaml             # Delete resources from a YAML file

kubectl diff -f <file>.yaml               # Show changes before applying


🔹 Miscellaneous

kubectl get all                           # Get all resources in the namespace

kubectl cluster-info dump                 # Dump complete cluster info

kubectl get pods --all-namespaces         # Get all pods in all namespaces

kubectl config view                       # View kubeconfig settings

kubectl drain <node-name> --ignore-daemonsets --force  # Safely drain a node

kubectl taint nodes <node-name> key=value:NoSchedule  # Taint a node

No comments:

Post a Comment

testing