Code snippents shouldn't include the command prompt (#12779)

This commit is contained in:
Neha Yadav
2019-03-07 15:01:05 +05:30
committed by Kubernetes Prow Robot
parent 99e4d3bac6
commit d3cca48e3f
40 changed files with 1052 additions and 441 deletions
@@ -90,9 +90,16 @@ a given action, and works regardless of the authorization mode used.
```bash
$ kubectl auth can-i create deployments --namespace dev
kubectl auth can-i create deployments --namespace dev
```
```
yes
$ kubectl auth can-i create deployments --namespace prod
```
```shell
kubectl auth can-i create deployments --namespace prod
```
```
no
```
@@ -100,7 +107,9 @@ Administrators can combine this with [user impersonation](/docs/reference/access
to determine what action other users can perform.
```bash
$ kubectl auth can-i list secrets --namespace dev --as dave
kubectl auth can-i list secrets --namespace dev --as dave
```
```
no
```
@@ -116,7 +125,9 @@ These APIs can be queried by creating normal Kubernetes resources, where the res
field of the returned object is the result of the query.
```bash
$ kubectl create -f - -o yaml << EOF
kubectl create -f - -o yaml << EOF
```
```
apiVersion: authorization.k8s.io/v1
kind: SelfSubjectAccessReview
spec:
@@ -19,10 +19,15 @@ To run an nginx Deployment and expose the Deployment, see [kubectl run](/docs/re
docker:
```shell
$ docker run -d --restart=always -e DOMAIN=cluster --name nginx-app -p 80:80 nginx
docker run -d --restart=always -e DOMAIN=cluster --name nginx-app -p 80:80 nginx
```
55c103fa129692154a7652490236fee9be47d70a8dd562281ae7d2f9a339a6db
```
$ docker ps
```shell
docker ps
```
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 9 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp nginx-app
```
@@ -31,7 +36,9 @@ kubectl:
```shell
# start the pod running nginx
$ kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
kubectl run --image=nginx nginx-app --port=80 --env="DOMAIN=cluster"
```
```
deployment "nginx-app" created
```
@@ -41,7 +48,9 @@ deployment "nginx-app" created
```shell
# expose a port through with a service
$ kubectl expose deployment nginx-app --port=80 --name=nginx-http
kubectl expose deployment nginx-app --port=80 --name=nginx-http
```
```
service "nginx-http" exposed
```
@@ -66,7 +75,9 @@ To list what is currently running, see [kubectl get](/docs/reference/generated/k
docker:
```shell
$ docker ps -a
docker ps -a
```
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14636241935f ubuntu:16.04 "echo test" 5 seconds ago Exited (0) 5 seconds ago cocky_fermi
55c103fa1296 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-app
@@ -75,7 +86,9 @@ CONTAINER ID IMAGE COMMAND CREATED
kubectl:
```shell
$ kubectl get po
kubectl get po
```
```
NAME READY STATUS RESTARTS AGE
nginx-app-8df569cb7-4gd89 1/1 Running 0 3m
ubuntu 0/1 Completed 0 20s
@@ -88,22 +101,30 @@ To attach a process that is already running in a container, see [kubectl attach]
docker:
```shell
$ docker ps
docker ps
```
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp nginx-app
```
$ docker attach 55c103fa1296
```shell
docker attach 55c103fa1296
...
```
kubectl:
```shell
$ kubectl get pods
kubectl get pods
```
```
NAME READY STATUS RESTARTS AGE
nginx-app-5jyvm 1/1 Running 0 10m
```
$ kubectl attach -it nginx-app-5jyvm
```shell
kubectl attach -it nginx-app-5jyvm
...
```
@@ -116,22 +137,33 @@ To execute a command in a container, see [kubectl exec](/docs/reference/generate
docker:
```shell
$ docker ps
docker ps
```
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55c103fa1296 nginx "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:80->80/tcp nginx-app
$ docker exec 55c103fa1296 cat /etc/hostname
```
```shell
docker exec 55c103fa1296 cat /etc/hostname
```
```
55c103fa1296
```
kubectl:
```shell
$ kubectl get po
kubectl get po
```
```
NAME READY STATUS RESTARTS AGE
nginx-app-5jyvm 1/1 Running 0 10m
```
$ kubectl exec nginx-app-5jyvm -- cat /etc/hostname
```shell
kubectl exec nginx-app-5jyvm -- cat /etc/hostname
```
```
nginx-app-5jyvm
```
@@ -141,14 +173,14 @@ To use interactive commands.
docker:
```shell
$ docker exec -ti 55c103fa1296 /bin/sh
docker exec -ti 55c103fa1296 /bin/sh
# exit
```
kubectl:
```shell
$ kubectl exec -ti nginx-app-5jyvm -- /bin/sh
kubectl exec -ti nginx-app-5jyvm -- /bin/sh
# exit
```
@@ -162,7 +194,9 @@ To follow stdout/stderr of a process that is running, see [kubectl logs](/docs/r
docker:
```shell
$ docker logs -f a9e
docker logs -f a9e
```
```
192.168.9.1 - - [14/Jul/2015:01:04:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" "-"
192.168.9.1 - - [14/Jul/2015:01:04:03 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.35.0" "-"
```
@@ -170,7 +204,9 @@ $ docker logs -f a9e
kubectl:
```shell
$ kubectl logs -f nginx-app-zibvs
kubectl logs -f nginx-app-zibvs
```
```
10.240.63.110 - - [14/Jul/2015:01:09:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
10.240.63.110 - - [14/Jul/2015:01:09:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
```
@@ -178,7 +214,9 @@ $ kubectl logs -f nginx-app-zibvs
There is a slight difference between pods and containers; by default pods do not terminate if their processes exit. Instead the pods restart the process. This is similar to the docker run option `--restart=always` with one major difference. In docker, the output for each invocation of the process is concatenated, but for Kubernetes, each invocation is separate. To see the output from a previous run in Kubernetes, do this:
```shell
$ kubectl logs --previous nginx-app-zibvs
kubectl logs --previous nginx-app-zibvs
```
```
10.240.63.110 - - [14/Jul/2015:01:09:01 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
10.240.63.110 - - [14/Jul/2015:01:09:02 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.26.0" "-"
```
@@ -192,32 +230,53 @@ To stop and delete a running process, see [kubectl delete](/docs/reference/gener
docker:
```shell
$ docker ps
docker ps
```
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9ec34d98787 nginx "nginx -g 'daemon of" 22 hours ago Up 22 hours 0.0.0.0:80->80/tcp, 443/tcp nginx-app
```
$ docker stop a9ec34d98787
```shell
docker stop a9ec34d98787
```
```
a9ec34d98787
```
$ docker rm a9ec34d98787
```shell
docker rm a9ec34d98787
```
```
a9ec34d98787
```
kubectl:
```shell
$ kubectl get deployment nginx-app
kubectl get deployment nginx-app
```
```
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-app 1 1 1 1 2m
```
$ kubectl get po -l run=nginx-app
```shell
kubectl get po -l run=nginx-app
```
```
NAME READY STATUS RESTARTS AGE
nginx-app-2883164633-aklf7 1/1 Running 0 2m
$ kubectl delete deployment nginx-app
```
```shell
kubectl delete deployment nginx-app
```
```
deployment "nginx-app" deleted
```
$ kubectl get po -l run=nginx-app
```shell
kubectl get po -l run=nginx-app
# Return nothing
```
@@ -236,7 +295,9 @@ To get the version of client and server, see [kubectl version](/docs/reference/g
docker:
```shell
$ docker version
docker version
```
```
Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
@@ -252,7 +313,9 @@ OS/Arch (server): linux/amd64
kubectl:
```shell
$ kubectl version
kubectl version
```
```
Client Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.9+a3d1dfa6f4335", GitCommit:"9b77fed11a9843ce3780f70dd251e92901c43072", GitTreeState:"dirty", BuildDate:"2017-08-29T20:32:58Z", OpenPaasKubernetesVersion:"v1.03.02", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.9+a3d1dfa6f4335", GitCommit:"9b77fed11a9843ce3780f70dd251e92901c43072", GitTreeState:"dirty", BuildDate:"2017-08-29T20:32:58Z", OpenPaasKubernetesVersion:"v1.03.02", GoVersion:"go1.7.5", Compiler:"gc", Platform:"linux/amd64"}
```
@@ -264,7 +327,9 @@ To get miscellaneous information about the environment and configuration, see [k
docker:
```shell
$ docker info
docker info
```
```
Containers: 40
Images: 168
Storage Driver: aufs
@@ -286,7 +351,9 @@ WARNING: No swap limit support
kubectl:
```shell
$ kubectl cluster-info
kubectl cluster-info
```
```
Kubernetes master is running at https://108.59.85.141
KubeDNS is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kube-dns/proxy
kubernetes-dashboard is running at https://108.59.85.141/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
@@ -81,11 +81,11 @@ range, end | iterate list | {range .items[*]}[{.metadata.nam
Examples using `kubectl` and JSONPath expressions:
```shell
$ kubectl get pods -o json
$ kubectl get pods -o=jsonpath='{@}'
$ kubectl get pods -o=jsonpath='{.items[0]}'
$ kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
kubectl get pods -o json
kubectl get pods -o=jsonpath='{@}'
kubectl get pods -o=jsonpath='{.items[0]}'
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
```
On Windows, you must _double_ quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:
+55 -46
View File
@@ -33,27 +33,27 @@ where `command`, `TYPE`, `NAME`, and `flags` are:
* `TYPE`: Specifies the [resource type](#resource-types). Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms. For example, the following commands produce the same output:
```shell
$ kubectl get pod pod1
$ kubectl get pods pod1
$ kubectl get po pod1
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
```
* `NAME`: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed, for example `$ kubectl get pods`.
* `NAME`: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed, for example `kubectl get pods`.
When performing an operation on multiple resources, you can specify each resource by type and name or specify one or more files:
* To specify resources by type and name:
* To group resources if they are all the same type: `TYPE1 name1 name2 name<#>`.<br/>
Example: `$ kubectl get pod example-pod1 example-pod2`
Example: `kubectl get pod example-pod1 example-pod2`
* To specify multiple resource types individually: `TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>`.<br/>
Example: `$ kubectl get pod/example-pod1 replicationcontroller/example-rc1`
Example: `kubectl get pod/example-pod1 replicationcontroller/example-rc1`
* To specify resources with one or more files: `-f file1 -f file2 -f file<#>`
* [Use YAML rather than JSON](/docs/concepts/configuration/overview/#general-configuration-tips) since YAML tends to be more user-friendly, especially for configuration files.<br/>
Example: `$ kubectl get pod -f ./pod.yaml`
Example: `kubectl get pod -f ./pod.yaml`
* `flags`: Specifies optional flags. For example, you can use the `-s` or `--server` flags to specify the address and port of the Kubernetes API server.<br/>
@@ -176,7 +176,7 @@ Output format | Description
In this example, the following command outputs the details for a single pod as a YAML formatted object:
```shell
$ kubectl get pod web-pod-13je7 -o=yaml
kubectl get pod web-pod-13je7 -o=yaml
```
Remember: See the [kubectl](/docs/user-guide/kubectl/) reference documentation for details about which output format is supported by each command.
@@ -190,13 +190,13 @@ To define custom columns and output only the details that you want into a table,
Inline:
```shell
$ kubectl get pods <pod-name> -o=custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion
kubectl get pods <pod-name> -o=custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion
```
Template file:
```shell
$ kubectl get pods <pod-name> -o=custom-columns-file=template.txt
kubectl get pods <pod-name> -o=custom-columns-file=template.txt
```
where the `template.txt` file contains:
@@ -251,7 +251,7 @@ kubectl [command] [TYPE] [NAME] --sort-by=<jsonpath_exp>
To print a list of pods sorted by name, you run:
```shell
$ kubectl get pods --sort-by=.metadata.name
kubectl get pods --sort-by=.metadata.name
```
## Examples: Common operations
@@ -262,52 +262,52 @@ Use the following set of examples to help you familiarize yourself with running
```shell
# Create a service using the definition in example-service.yaml.
$ kubectl create -f example-service.yaml
kubectl create -f example-service.yaml
# Create a replication controller using the definition in example-controller.yaml.
$ kubectl create -f example-controller.yaml
kubectl create -f example-controller.yaml
# Create the objects that are defined in any .yaml, .yml, or .json file within the <directory> directory.
$ kubectl create -f <directory>
kubectl create -f <directory>
```
`kubectl get` - List one or more resources.
```shell
# List all pods in plain-text output format.
$ kubectl get pods
kubectl get pods
# List all pods in plain-text output format and include additional information (such as node name).
$ kubectl get pods -o wide
kubectl get pods -o wide
# List the replication controller with the specified name in plain-text output format. Tip: You can shorten and replace the 'replicationcontroller' resource type with the alias 'rc'.
$ kubectl get replicationcontroller <rc-name>
kubectl get replicationcontroller <rc-name>
# List all replication controllers and services together in plain-text output format.
$ kubectl get rc,services
kubectl get rc,services
# List all daemon sets, including uninitialized ones, in plain-text output format.
$ kubectl get ds --include-uninitialized
kubectl get ds --include-uninitialized
# List all pods running on node server01
$ kubectl get pods --field-selector=spec.nodeName=server01
kubectl get pods --field-selector=spec.nodeName=server01
```
`kubectl describe` - Display detailed state of one or more resources, including the uninitialized ones by default.
```shell
# Display the details of the node with name <node-name>.
$ kubectl describe nodes <node-name>
kubectl describe nodes <node-name>
# Display the details of the pod with name <pod-name>.
$ kubectl describe pods/<pod-name>
kubectl describe pods/<pod-name>
# Display the details of all the pods that are managed by the replication controller named <rc-name>.
# Remember: Any pods that are created by the replication controller get prefixed with the name of the replication controller.
$ kubectl describe pods <rc-name>
kubectl describe pods <rc-name>
# Describe all pods, not including uninitialized ones
$ kubectl describe pods --include-uninitialized=false
kubectl describe pods --include-uninitialized=false
```
{{< note >}}
@@ -326,39 +326,39 @@ the pods running on it, the events generated for the node etc.
```shell
# Delete a pod using the type and name specified in the pod.yaml file.
$ kubectl delete -f pod.yaml
kubectl delete -f pod.yaml
# Delete all the pods and services that have the label name=<label-name>.
$ kubectl delete pods,services -l name=<label-name>
kubectl delete pods,services -l name=<label-name>
# Delete all the pods and services that have the label name=<label-name>, including uninitialized ones.
$ kubectl delete pods,services -l name=<label-name> --include-uninitialized
kubectl delete pods,services -l name=<label-name> --include-uninitialized
# Delete all pods, including uninitialized ones.
$ kubectl delete pods --all
kubectl delete pods --all
```
`kubectl exec` - Execute a command against a container in a pod.
```shell
# Get output from running 'date' from pod <pod-name>. By default, output is from the first container.
$ kubectl exec <pod-name> date
kubectl exec <pod-name> date
# Get output from running 'date' in container <container-name> of pod <pod-name>.
$ kubectl exec <pod-name> -c <container-name> date
kubectl exec <pod-name> -c <container-name> date
# Get an interactive TTY and run /bin/bash from pod <pod-name>. By default, output is from the first container.
$ kubectl exec -ti <pod-name> /bin/bash
kubectl exec -ti <pod-name> /bin/bash
```
`kubectl logs` - Print the logs for a container in a pod.
```shell
# Return a snapshot of the logs from pod <pod-name>.
$ kubectl logs <pod-name>
kubectl logs <pod-name>
# Start streaming the logs from pod <pod-name>. This is similar to the 'tail -f' Linux command.
$ kubectl logs -f <pod-name>
kubectl logs -f <pod-name>
```
## Examples: Creating and using plugins
@@ -368,43 +368,52 @@ Use the following set of examples to help you familiarize yourself with writing
```shell
# create a simple plugin in any language and name the resulting executable file
# so that it begins with the prefix "kubectl-"
$ cat ./kubectl-hello
cat ./kubectl-hello
#!/bin/bash
# this plugin prints the words "hello world"
echo "hello world"
# with our plugin written, let's make it executable
$ sudo chmod +x ./kubectl-hello
sudo chmod +x ./kubectl-hello
# and move it to a location in our PATH
$ sudo mv ./kubectl-hello /usr/local/bin
sudo mv ./kubectl-hello /usr/local/bin
# we have now created and "installed" a kubectl plugin.
# we can begin using our plugin by invoking it from kubectl as if it were a regular command
$ kubectl hello
kubectl hello
```
```
hello world
```
```
# we can "uninstall" a plugin, by simply removing it from our PATH
$ sudo rm /usr/local/bin/kubectl-hello
sudo rm /usr/local/bin/kubectl-hello
```
In order to view all of the plugins that are available to `kubectl`, we can use
the `kubectl plugin list` subcommand:
```shell
$ kubectl plugin list
kubectl plugin list
```
```
The following kubectl-compatible plugins are available:
/usr/local/bin/kubectl-hello
/usr/local/bin/kubectl-foo
/usr/local/bin/kubectl-bar
```
```
# this command can also warn us about plugins that are
# not executable, or that are overshadowed by other
# plugins, for example
$ sudo chmod -x /usr/local/bin/kubectl-foo
$ kubectl plugin list
sudo chmod -x /usr/local/bin/kubectl-foo
kubectl plugin list
```
```
The following kubectl-compatible plugins are available:
/usr/local/bin/kubectl-hello
@@ -419,7 +428,7 @@ We can think of plugins as a means to build more complex functionality on top
of the existing kubectl commands:
```shell
$ cat ./kubectl-whoami
cat ./kubectl-whoami
#!/bin/bash
# this plugin makes use of the `kubectl config` command in order to output
@@ -432,12 +441,12 @@ context in our KUBECONFIG file:
```shell
# make the file executable
$ sudo chmod +x ./kubectl-whoami
sudo chmod +x ./kubectl-whoami
# and move it into our PATH
$ sudo mv ./kubectl-whoami /usr/local/bin
sudo mv ./kubectl-whoami /usr/local/bin
$ kubectl whoami
kubectl whoami
Current user: plugins-user
```