Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Last active August 19, 2018 21:24
Show Gist options
  • Save murarisumit/88dc1b8c06e06e012ad55b9afddb4364 to your computer and use it in GitHub Desktop.
Save murarisumit/88dc1b8c06e06e012ad55b9afddb4364 to your computer and use it in GitHub Desktop.
kubectl cheatsheet #kubernetes
  • Label:

    • Labels are key/value pairs that are attached to objects, such as pods.
    • Labels can be attached to objects at creation time and subsequently added and modified at any time.
    • Each object can have a set of key/value labels defined. Each Key must be unique for a given object.
  • Service:

    • A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service.
    • Behind a service, pods come and go with autoscaling but service is by which we communicate.
  • Namespace:

    • Within Namespaces level we can apply resource limits.
    • Most Kubernetes resources (e.g. pods, services, replication controllers, and others) are in some namespaces.
    • However namespace resources are not themselves in a namespace.
    • And low-level resources, such as nodes and persistentVolumes, are not in any namespace.
    • Service endpoint created has dns entry of form <service-name>.<namespace-name>.svc.cluster.local.
      • So with namespace we can have similar services across multiple namespaces such as Development, Staging and Production
  • Get components(etcd, scheduler ...) status : kubectl get cs

Node

  • Get all nodes in cluster : kubectl get no
  • Watch nodes continuously: kubectl get nodes -w

Pods

  • Get pods running: kubectl get po
  • Get pods with labels : kubectl get pods --show-labels
  • Select pods with specific label : kubectl get pod -l class
  • Get non-running pods: kubectl get pods --field-selector=status.phase!=Running
  • Grep pod with specific name : kubectl delete pod $(kubectl get pods --no-headers | grep "pod-name" | awk '{ print 0 }')
    • Little bash fun up ther^, if you know exact name use: kubectl delete pod pod-name
  • Get pods not running in 'non-default' namespace: kubectl get services --field-selector metadata.namespace!=default
  • Kubec

Services

  • Get all running services: kubectl get svc

Cluster

Kubectl

  • Kubectl output with custom field and sorted:

    • kubectl get pod --sort-by .status.containerStatuses[].restartCount -o custom-columns=Name:.metadata.name,restartCount:.status.containerStatuses[].restartCount
  • Kubectl output filter with selected field

  • Via a label selector, the client/user can identify a set of objects. The label selector is the core grouping primitive in Kubernetes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment