# Kubernetes Command Reference This document provides a reference for common `kubectl` commands used for managing Kubernetes clusters. Use the examples and explanations below to help manage nodes, namespaces, pods, deployments, and more. ## Table of Contents - [Kubernetes Command Reference](#kubernetes-command-reference) - [Table of Contents](#table-of-contents) - [General Commands](#general-commands) - [Node Management](#node-management) - [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 and Documentation](#api-resources-and-documentation) - [Logs and Pod Information](#logs-and-pod-information) - [Applying YAML Files](#applying-yaml-files) - [Managing Nodes and Labels](#managing-nodes-and-labels) - [Viewing Cluster Resources](#viewing-cluster-resources) - [ReplicaSet and Deployment Management](#replicaset-and-deployment-management) - [Additional Information](#additional-information) --- ## General Commands - **List API Resources** ```bash kubectl api-resources ``` --- ## Node Management - **Show all nodes** ```bash kubectl get node ``` - **Set a label on a node** ```bash kubectl label nodes kubernetes.io/= ``` --- ## Namespace Management - **List all namespaces** ```bash kubectl get namespaces # or the abbreviated version: kubectl get ns ``` - **Create a custom namespace** ```bash kubectl create ns ``` --- ## Pod Management ### Listing Pods - **List pods in the default namespace** ```bash kubectl get pod ``` - **List pods with detailed information (wide output) in the default namespace** ```bash kubectl get pod -o wide ``` - **List pods with detailed information in a specific namespace** ```bash kubectl get pod -o wide -n ``` ### Running a Pod - **Run a new pod** Use the following command structure to run a pod with various options: ```bash kubectl run { --image=, # Container image to use --port=, # Port that the container exposes -n , # Namespace in which to run the pod --env="KEY=VALUE", # Environment variables for the container --command, # Treat the following arguments as the command --replicas=, # Number of pod replicas to create --labels="key=value,key2=value2", # Labels to assign to the pod(s) --dry-run=client, # Print the object without creating it --restart=, # Pod restart policy --overrides='', # JSON override for the generated object --image-pull-policy=, # Image pull policy (Always, IfNotPresent, Never) --limits=cpu=,memory=, # Resource limits for the container --requests=cpu=,memory= # Resource requests for the container } ``` - **Example:** ```bash kubectl run mypod --image=nginx --port=80 -n mynamespace \ --env="ENV_VAR_NAME=VALUE" --command -- nginx -g "daemon off;" \ --replicas=3 --labels="app=myapp,env=prod" --dry-run=client \ --restart=Always --overrides='{"spec": {"containers": [{"name": "nginx", "image": "nginx"}]}}' \ --image-pull-policy=IfNotPresent --limits=cpu=100m,memory=256Mi \ --requests=cpu=50m,memory=128Mi ``` ### Deleting a Pod - **Delete a pod in a custom namespace** ```bash kubectl delete pod -n ``` --- ## API Resources and Documentation - **Get documentation for an API resource** ```bash kubectl explain ``` - *Example:* ```bash kubectl explain pod ``` --- ## Logs and Pod Information - **Stream logs for a running pod** ```bash kubectl logs -f -n ``` - **Get detailed state and log information for a pod** ```bash kubectl describe pod -n ``` --- ## Applying YAML Files - **Apply configuration from a YAML file to a specific namespace** ```bash kubectl apply -f -n ``` --- ## Managing Nodes and Labels - **Label a node with a custom key-value pair** ```bash kubectl label nodes kubernetes.io/= ``` --- ## Viewing Cluster Resources - **Display all resources loaded in a namespace** ```bash kubectl get all -n ``` - **Display replica sets, pods, and deployments in a specific namespace** ```bash kubectl get rs,pod,deployment -n ``` --- ## ReplicaSet and Deployment Management - **Scale a ReplicaSet** ```bash kubectl scale rs -n --replicas= ``` - **View rollout history of a deployment** ```bash kubectl rollout history deployment -n ``` - **View details of a specific revision in a deployment's rollout history** ```bash kubectl rollout history deployment -n --revision ``` - **Roll back a deployment to a specific revision** ```bash kubectl rollout undo deployment -n --to-revision= ``` > **Note:** The command for rolling back to a specific revision is `kubectl rollout undo` rather than using `--to-revision` with `kubectl rollout history`. --- ## Additional Information - **Static Manifest Files** All YAML files located under `/etc/kubernetes/manifests/` are automatically loaded after a server reboot. ---