From 27e89c1f8424ea9e9842f69179717c712becd529 Mon Sep 17 00:00:00 2001 From: Eric Tune Date: Tue, 1 Dec 2015 09:16:06 -0800 Subject: [PATCH] Revert the revert of kubernetes/kubernetes#15674 --- docs/user-guide/kubectl-cheatsheet.md | 159 ++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 docs/user-guide/kubectl-cheatsheet.md diff --git a/docs/user-guide/kubectl-cheatsheet.md b/docs/user-guide/kubectl-cheatsheet.md new file mode 100644 index 00000000000..58eab5fd444 --- /dev/null +++ b/docs/user-guide/kubectl-cheatsheet.md @@ -0,0 +1,159 @@ + + + + +WARNING +WARNING +WARNING +WARNING +WARNING + +

PLEASE NOTE: This document applies to the HEAD of the source tree

+ +If you are using a released version of Kubernetes, you should +refer to the docs that go with that version. + + +The latest release of this document can be found +[here](http://releases.k8s.io/release-1.1/docs/user-guide/kubectl-cheatsheet.md). + +Documentation for other releases can be found at +[releases.k8s.io](http://releases.k8s.io). + +-- + + + + + + +An assortment of compact kubectl examples + +See also: [Kubectl overview](kubectl-overview.md) and [JsonPath guide](jsonpath.md). + +## Creating Objects + +```console +$ kubectl create -f ./file.yml # create resource(s) in a json or yaml file + +$ kubectl create -f ./file1.yml -f ./file2.yaml # create resource(s) in a json or yaml file + +$ kubectl create -f ./dir # create resources in all .json, .yml, and .yaml files in dir + +# Create from a URL +$ kubectl create -f http://www.fpaste.org/279276/48569091/raw/ + +# Create multiple YAML objects from stdin +$ cat < # List a particular replication controller +$ kubectl get replicationcontroller # List a particular RC + +# Verbose output +$ kubectl describe nodes +$ kubectl describe pods +$ kubectl describe pods/ # Equivalent to previous +$ kubectl describe pods # Lists pods created by using common prefix + +# List Services Sorted by Name +$ kubectl get services --sort-by=.metadata.name + +# List pods Sorted by Restart Count +$ kubectl get pods --sort-by=.status.containerStatuses[0].restartCount + +# Get the version label of all pods with label app=cassandra +$ kubectl get pods --selector=app=cassandra rc -o 'jsonpath={.items[*].metadata.labels.version}' + +# Get ExternalIPs of all nodes +$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=ExternalIP)].address}' + +# List Names of Pods that belong to Particular RC +# "jq" command useful for transformations that are too complex for jsonpath +$ sel=$(./kubectl get rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"') +$ sel=${sel%?} # Remove trailing comma +$ pods=$(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})` + +# Check which nodes are ready +$ kubectl get nodes -o jsonpath='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'| tr ';' "\n" | grep "Ready=True" +``` + +## Modifying and Deleting Resources + +```console +$ kubectl label pods new-label=awesome # Add a Label +$ kubectl annotate pods icon-url=http://goo.gl/XXBTWq # Add an annotation + +# TODO: examples of kubectl edit, patch, delete, replace, scale, and rolling-update commands. +``` + +## Interacting with running Pods + +```console +$ kubectl logs # dump pod logs (stdout) +$ kubectl logs -f # stream pod logs (stdout) until canceled (ctrl-c) or timeout + +$ kubectl run -i --tty busybox --image=busybox -- sh # Run pod as interactive shell +$ kubectl attach -i # Attach to Running Container +$ kubectl port-forward # Forward port of Pod to your local machine +$ kubectl port-forward # Forward port to service +$ kubectl exec -- ls / # Run command in existing pod (1 container case) +$ kubectl exec -c -- ls / # Run command in existing pod (multi-container case) +``` + + + + +[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl-cheatsheet.md?pixel)]() +