Skip to main content

Kubernetes Cheat Sheet

Kubernetes Cheat Sheet

info

Please set up an alias for kubectl as k and replace grep with findstr when using powershell terminal

1. Setting the Context and Configuration

Essential commands for starting your interaction with any Kubernetes cluster.

  • Set kubectl context:

    k config use-context [context-name]
  • View current context:

    k config current-context
  • List all contexts:

    k config get-contexts
  • View detailed kubeconfig settings:

    k config view
  • Set namespace permanently for all subsequent kubectl commands:

    k config set-context --current --namespace=[namespace]

2. Cluster Information

Commands to inspect and manage the overall cluster environment.

  • Get cluster information:

    k cluster-info
  • List all components:

    k get componentstatuses
  • Get a detailed configuration view of the cluster:

    k get cm

3. Nodes Management

Commands to view and manage nodes, which are the physical or virtual machines part of the cluster.

  • List nodes:

    k get nodes
  • Describe node:

    k describe node [node-name]
  • Mark node as unschedulable:

    k cordon [node-name]
  • Enable scheduling on node:

    k uncordon [node-name]

4. Managing Pods

Pods are the fundamental building block in Kubernetes, including commands to list containers within a pod.

  • List all pods in all namespaces:

    k get pods --all-namespaces
  • Create a pod from a YAML file:

    k apply -f [file.yaml]
  • Delete pod:

    k delete pod [pod-name]
  • Get detailed information about a pod:

    k describe pods [pod-name]
  • List containers in a pod using JSONPath:

    k get pod [pod-name] -o=jsonpath='{.spec.containers[*].name}
  • Check container images used in a pod:

    k get pod [pod-name] -o jsonpath="{.spec.containers[*].image}
  • List Environment Variables in a Pod:

    k exec <pod_name> -- env
  • View Environment Variables for a Specific Container:

    k exec <pod_name> -c <container_name> -- env
  • List Environment Variables for All Containers in a Pod:

    k exec <pod_name> -- env --all-containers=true

5. Deployments, Replicasets, and StatefulSets

Commands to manage lifecycle and scaling of applications.

  • Create deployment:

    k create deployment [name] --image=[image]
  • Scale a deployment:

    k scale deployment [deployment-name] --replicas=[number]
  • Update deployment:

    k set image deployment/[deployment-name] [container-name]=[new-image]:[tag]
  • List and manage StatefulSets:

    k get statefulsets`, `k describe statefulsets [statefulset-name]

6. Services and Networking

Configuring network access to the applications running on Kubernetes.

  • Expose deployment as a service:

    k expose deployment [deployment-name] --port=[port] --type=[type]
  • Get services:

    k get svc
  • Edit service:

    k edit svc [service-name]

7. ConfigMaps and Secrets

For managing configuration and secret data within Kubernetes.

  • Create a secret:

    k create secret generic [secret-name] --from-literal=[key]=[value]
  • List secrets:

    k get secrets
  • Create a ConfigMap:

    k create configmap [configmap-name] --from-file=[path-to-file]
  • Get ConfigMaps:

    k get configmaps

8. Debugging, Logs, and Diagnostics

Essential for troubleshooting applications and infrastructure.

  • Get logs for a pod:

    k logs [pod-name]
  • Execute a command in a container

    k exec -it [pod-name] -- [command]
  • Attach to running container:

    k attach [pod-name] -i
  • Copy files and directories to and from containers:

    k cp [local-path] [namespace]/[pod-name]:[path]

9. Resource Quotas and Limits

Managing resource allocation to ensure cluster efficiency.

  • View resource quotas:

    k describe quota
  • Create resource quota:

    k create quota [quota-name] --hard=[resource]=[value]
  • Set limits on a namespace:

    k create limitrange [limit-range-name] --[limits]

10. Advanced Resource Management

Commands for detailed and specialized resource management.

  • Drain a node for maintenance:

    k drain [node-name] --ignore-daemonsets
  • Delete resources by filenames, stdin, resources and names, or by resources and label selector:

    k delete -f [file.yaml]
  • Apply a JSON or YAML file:

    k apply -f [file.json or file.yaml]
Every Bit of Support Helps!

If you have enjoyed this post, please consider buying me a coffee ☕ to help me keep writing!