Files
my-docs/Containerization & Orchestration/Kubernetes/3-Commands.md

6.9 KiB
Executable File
Raw Blame History

Below is an enhanced version of your Kubernetes Command Reference. This revision further refines the structure, improves consistency, and adds a few extra sections for clarity and ease of use.


Kubernetes Command Reference

This guide provides a concise reference for common kubectl commands used to manage Kubernetes clusters. Whether youre managing nodes, namespaces, pods, deployments, or autoscaling, the examples below will help you perform everyday tasks with confidence.


Table of Contents


General Commands

  • List API Resources
    Display all available API resources along with their short names:

    kubectl api-resources
    

Node Management

Listing Nodes

  • Show All Nodes

    kubectl get nodes
    

Labeling Nodes

  • Set a Custom Label on a Node

    kubectl label node <node-name> kubernetes.io/<label-key>=<label-value>
    

    Note: Replace <node-name>, <label-key>, and <label-value> with your desired values.

Node Maintenance (Cordon/Drain)

  • Cordon a Node
    Prevent new pods from being scheduled on the node.

    kubectl cordon <node-name>
    
  • Uncordon a Node
    Mark the node as schedulable again.

    kubectl uncordon <node-name>
    
  • Drain a Node
    Evict all pods from the node (excluding those managed by DaemonSets).

    kubectl drain <node-name> --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

    kubectl get namespaces
    # Or the shorthand:
    kubectl get ns
    
  • Create a New Namespace

    kubectl create namespace <namespace-name>
    

Pod Management

Listing Pods

  • List Pods in the Default Namespace

    kubectl get pods
    
  • List Pods with Detailed Information (Wide Output)

    kubectl get pods -o wide
    
  • List Pods in a Specific Namespace

    kubectl get pods -o wide -n <namespace-name>
    

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:

    kubectl run <pod-name> --image=<image-name> --port=<port-number> -n <namespace-name>
    
  • Advanced Example with Multiple Options:

    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

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

API Resources & Documentation

  • Get Detailed Documentation for an API Resource

    kubectl explain <api-resource-name>
    

    Example:

    kubectl explain pod
    

Logs & Pod Information

  • Stream Logs for a Running Pod

    kubectl logs -f -n <namespace-name> <pod-name>
    
  • Get Detailed Information About a Pod

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

Applying YAML Files

  • Apply a Configuration from a YAML File

    Apply a YAML configuration to a specific namespace:

    kubectl apply -f <yaml-file> -n <namespace-name>
    

Viewing Cluster Resources

  • Display All Resources in a Namespace

    kubectl get all -n <namespace-name>
    
  • Display ReplicaSets, Pods, and Deployments in a Specific Namespace

    kubectl get rs,pods,deployments -n <namespace-name>
    

ReplicaSet & Deployment Management

Scaling and Rollouts

  • Scale a ReplicaSet

    kubectl scale rs <replicaset-name> --replicas=<count> -n <namespace-name>
    
  • View Rollout History of a Deployment

    kubectl rollout history deployment <deployment-name> -n <namespace-name>
    
  • View Details of a Specific Revision

    kubectl rollout history deployment <deployment-name> -n <namespace-name> --revision=<number>
    
  • Roll Back a Deployment to a Specific Revision

    kubectl rollout undo deployment <deployment-name> -n <namespace-name> --to-revision=<number>
    

Autoscaling

  • Autoscale a Deployment
    Automatically scale a deployment based on CPU utilization:

    kubectl autoscale deployment <deployment-name> -n <namespace-name> --cpu-percent=<target-cpu-percentage> --min=<min-pods> --max=<max-pods>
    
  • View Horizontal Pod Autoscalers (HPA)

    kubectl get hpa -n <namespace-name>
    

Port Forwarding

Sometimes you need to access a service or pod directly from your local machine. Use the following command to forward a port:

kubectl port-forward -n <namespace-name> svc/<service-name> <local-port>:<target-port>

Example: Forward local port 8080 to port 80 of the service named my-service in the mynamespace namespace:

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).