# Kubernetes Command Reference This guide provides a concise reference for common `kubectl` commands used to manage Kubernetes clusters. Whether you’re managing nodes, namespaces, pods, deployments, or autoscaling, the examples below will help you perform everyday tasks with confidence. --- ## Table of Contents - [Kubernetes Command Reference](#kubernetes-command-reference) - [Table of Contents](#table-of-contents) - [General Commands](#general-commands) - [Node Management](#node-management) - [Listing Nodes](#listing-nodes) - [Labeling Nodes](#labeling-nodes) - [Node Maintenance (Cordon/Drain)](#node-maintenance-cordondrain) - [Namespace Management](#namespace-management) - [Pod Management](#pod-management) - [Listing Pods](#listing-pods) - [Running a Pod](#running-a-pod) - [Deleting a Pod](#deleting-a-pod) - [API Resources \& Documentation](#api-resources--documentation) - [Logs \& Pod Information](#logs--pod-information) - [Applying YAML Files](#applying-yaml-files) - [Viewing Cluster Resources](#viewing-cluster-resources) - [ReplicaSet \& Deployment Management](#replicaset--deployment-management) - [Scaling and Rollouts](#scaling-and-rollouts) - [Autoscaling](#autoscaling) - [Port Forwarding](#port-forwarding) - [Additional Information](#additional-information) --- ## General Commands - **List API Resources** Display all available API resources along with their short names: ```bash kubectl api-resources ``` --- ## Node Management ### Listing Nodes - **Show All Nodes** ```bash kubectl get nodes ``` ### Labeling Nodes - **Set a Custom Label on a Node** ```bash kubectl label node kubernetes.io/= ``` > **Note:** Replace ``, ``, and `` with your desired values. ### Node Maintenance (Cordon/Drain) - **Cordon a Node** Prevent new pods from being scheduled on the node. ```bash kubectl cordon ``` - **Uncordon a Node** Mark the node as schedulable again. ```bash kubectl uncordon ``` - **Drain a Node** Evict all pods from the node (excluding those managed by DaemonSets). ```bash kubectl drain --ignore-daemonsets --delete-local-data ``` > **Warning:** Draining a node will evict running pods. Ensure that you plan this action to avoid service disruption. --- ## Namespace Management - **List All Namespaces** ```bash kubectl get namespaces # Or the shorthand: kubectl get ns ``` - **Create a New Namespace** ```bash kubectl create namespace ``` --- ## Pod Management ### Listing Pods - **List Pods in the Default Namespace** ```bash kubectl get pods ``` - **List Pods with Detailed Information (Wide Output)** ```bash kubectl get pods -o wide ``` - **List Pods in a Specific Namespace** ```bash kubectl get pods -o wide -n ``` ### Running a Pod > **Note:** The `kubectl run` command is best suited for running single pods. For more complex deployments, consider using YAML manifests. - **Basic Example:** ```bash kubectl run --image= --port= -n ``` - **Advanced Example with Multiple Options:** ```bash kubectl run mypod --image=nginx --port=80 -n mynamespace \ --env="ENV_VAR_NAME=VALUE" --command -- nginx -g "daemon off;" \ --restart=Always --dry-run=client \ --labels="app=myapp,env=prod" \ --limits=cpu=100m,memory=256Mi --requests=cpu=50m,memory=128Mi ``` **Common Options Explained:** - `--image`: Container image to use. - `--port`: Port exposed by the container. - `-n` or `--namespace`: Namespace in which to run the pod. - `--env`: Set environment variables. - `--command`: Treat the following arguments as the command to run. - `--restart`: Pod restart policy (`Always`, `OnFailure`, or `Never`). - `--labels`: Assign labels to the pod. - `--dry-run`: Validate the command without creating the pod. - `--limits` and `--requests`: Define resource limits and requests for the container. ### Deleting a Pod - **Delete a Pod in a Specific Namespace** ```bash kubectl delete pod -n ``` --- ## API Resources & Documentation - **Get Detailed Documentation for an API Resource** ```bash kubectl explain ``` *Example:* ```bash kubectl explain pod ``` --- ## Logs & Pod Information - **Stream Logs for a Running Pod** ```bash kubectl logs -f -n ``` - **Get Detailed Information About a Pod** ```bash kubectl describe pod -n ``` --- ## Applying YAML Files - **Apply a Configuration from a YAML File** Apply a YAML configuration to a specific namespace: ```bash kubectl apply -f -n ``` --- ## Viewing Cluster Resources - **Display All Resources in a Namespace** ```bash kubectl get all -n ``` - **Display ReplicaSets, Pods, and Deployments in a Specific Namespace** ```bash kubectl get rs,pods,deployments -n ``` --- ## ReplicaSet & Deployment Management ### Scaling and Rollouts - **Scale a ReplicaSet** ```bash kubectl scale rs --replicas= -n ``` - **View Rollout History of a Deployment** ```bash kubectl rollout history deployment -n ``` - **View Details of a Specific Revision** ```bash kubectl rollout history deployment -n --revision= ``` - **Roll Back a Deployment to a Specific Revision** ```bash kubectl rollout undo deployment -n --to-revision= ``` ### Autoscaling - **Autoscale a Deployment** Automatically scale a deployment based on CPU utilization: ```bash kubectl autoscale deployment -n --cpu-percent= --min= --max= ``` - **View Horizontal Pod Autoscalers (HPA)** ```bash kubectl get hpa -n ``` --- ## Port Forwarding Sometimes you need to access a service or pod directly from your local machine. Use the following command to forward a port: ```bash kubectl port-forward -n svc/ : ``` > **Example:** Forward local port 8080 to port 80 of the service named `my-service` in the `mynamespace` namespace: > > ```bash > kubectl port-forward -n mynamespace svc/my-service 8080:80 > ``` --- ## Additional Information - **Static Manifest Files** Any YAML files placed in `/etc/kubernetes/manifests/` are automatically loaded when the kubelet starts (for example, after a server reboot).