Kubernetes cheatsheet

Some frequent k8s commands with params

Page content

Here is my k8s cheat sheet covering kubenetes most important commands and concepts from installing to running containers and cleaning up:

k8s lights

Installation of Kubernetes

Using Kubeadm

To install Kubernetes using kubeadm, follow these steps:

  1. Prepare the Environment:

    • Ensure the necessary kernel modules are enabled:
      sudo sysctl net/bridge/bridge-nf-call-ip6tables=1
      sudo sysctl net/bridge/bridge-nf-call-iptables=1
      sudo sysctl net/bridge/bridge-nf-call-arptables=1
      
    • Reboot the system to apply the changes.
  2. Install Docker and Other Prerequisites:

    • Install Docker:
      sudo apt-get update
      sudo apt-get install -y docker.io
      
    • Install ebtables and ethtool:
      sudo apt-get install ebtables ethtool
      
    • Install HTTPS support and curl if necessary:
      sudo apt-get update
      sudo apt-get install -y apt-transport-https curl
      
    • Add the Kubernetes repository and install kubeadm, kubelet, and kubectl:
      curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
      deb http://apt.kubernetes.io/ kubernetes-xenial main
      EOF
      sudo apt-get update
      sudo apt-get install -y kubeadm kubelet kubectl
      
  3. Initialize the Kubernetes Cluster:

    • Initialize the first control plane node:
      sudo kubeadm init
      
    • Join additional control plane and worker nodes:
      kubeadm join <control-plane-node>:8443 --token <token> --discovery-token-ca-cert-hash <hash>
      

Using Minikube or Kind

For local development, you can use minikube or kind:

  • Minikube:

    minikube start
    

    Follow the official [Get Started! guide].

  • Kind:

    curl -sfL https://get.k3s.io | sh -
    kind create cluster
    

    Follow the [Kind Quick Start page].

Common kubectl Commands

Basic Commands

  • Get Information About Resources:

    kubectl get <resource>  # List resources (e.g., pods, services, deployments)
    kubectl get pods --all-namespaces  # List all pods in all namespaces
    kubectl get pods -o wide  # List pods with more details
    kubectl get pod my-pod -o yaml  # Get a pod's YAML configuration.
    
  • Describe Resources:

    kubectl describe <resource>  # Detailed description of a resource (e.g., nodes, pods)
    kubectl describe nodes my-node
    kubectl describe pods my-pod.
    
  • Create Resources:

    kubectl create -f <manifest-file>  # Create resources from a YAML or JSON file
    kubectl create configmap <configmap-name> --from-literal=<key>=<value>  # Create a ConfigMap
    kubectl create secret generic <secret-name> --from-literal=<key>=<value>  # Create a Secret.
    
  • Delete Resources:

    kubectl delete <resource> <name>  # Delete a resource (e.g., pod, deployment)
    kubectl delete pod my-pod
    kubectl delete deployment my-deployment.
    

Pod Management

  • Define a Command and Arguments for a Container:

    apiVersion: v1
    kind: Pod
    metadata:
      name: command-demo
    spec:
      containers:
      - name: command-demo-container
        image: debian
        command: ["printenv"]
        args: ["HOSTNAME", "KUBERNETES_PORT"]
    
    kubectl apply -f <manifest-file>
    kubectl get pods
    kubectl logs command-demo.
    
  • Run a Command in a Shell:

    apiVersion: v1
    kind: Pod
    metadata:
      name: shell-demo
    spec:
      containers:
      - name: shell-demo-container
        image: debian
        command: ["/bin/sh"]
        args: ["-c", "while true; do echo hello; sleep 10;done"]
    
    kubectl apply -f <manifest-file>
    kubectl get pods
    kubectl logs shell-demo.
    

ConfigMap and Secret Management

  • Create a ConfigMap:

    kubectl create configmap <configmap-name> --from-literal=<key>=<value>
    kubectl create configmap app-config --from-literal=environment=production --from-literal=log_level=info.
    
  • Create a Secret:

    kubectl create secret generic <secret-name> --from-literal=<key>=<value>
    kubectl create secret generic db-secret --from-literal=username=admin --from-literal=password=supersecret.
    

Deployment and Service Management

  • Create a Deployment:

    kubectl create deployment <deployment-name> --image=<image-name>
    kubectl create deployment my-deployment --image=nginx:latest.
    
  • Expose a Deployment as a Service:

    kubectl expose deployment <deployment-name> --type=NodePort --port=<port>
    kubectl expose deployment my-deployment --type=NodePort --port=80.
    

Namespace and Node Management

  • List Namespaces:

    kubectl get namespaces
    
  • Create a Namespace:

    kubectl create namespace <namespace-name>
    kubectl create namespace my-namespace.
    
  • List Nodes:

    kubectl get nodes
    kubectl get nodes --selector='!node-role.kubernetes.io/control-plane'.
    

Monitoring and Debugging

  • View Logs:

    kubectl logs <pod-name>
    kubectl logs -f <pod-name>  # Follow the logs in real-time.
    
  • Describe Events:

    kubectl describe events
    
  • Check Cluster Information:

    kubectl cluster-info
    kubectl version  # Check the Kubernetes version.
    

Advanced Commands

  • Sort Resources:

    kubectl get pods --sort-by='.status.containerStatuses.restartCount'
    kubectl get services --sort-by=.metadata.name.
    
  • Filter Resources:

    kubectl get pods --field-selector=status.phase=Running
    kubectl get nodes --selector='!node-role.kubernetes.io/control-plane'.
    
  • Retrieve Specific Data:

    kubectl get configmap myconfig -o jsonpath='{.data.ca\.crt}'
    kubectl get secret my-secret --template='{{index .data "key-name-with-dashes"}}'.