You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: kubectl.md
+41-30Lines changed: 41 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,25 @@
1
+
---
2
+
title: kubectl
3
+
category: kubernetes
4
+
layout: 2017/sheet
5
+
tags: [Featured]
6
+
updated: 2020-05-16
7
+
weight: -10
8
+
intro: |
9
+
Kubectl is a command line tool for controlling Kubernetes clusters. `kubectl` looks for a file named config in the $HOME/.kube directory. You can specify other [kubeconfig](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/) files by setting the KUBECONFIG environment variable or by setting the [--kubeconfig](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/) flag.
10
+
11
+
This overview covers kubectl syntax, describes the command operations, and provides common examples. For details about each command, including all the supported flags and subcommands, see the [kubectl](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands/) reference documentation. For installation instructions see [installing kubectl](https://kubernetes.io/docs/tasks/kubectl/install/).
12
+
---
13
+
1
14
## Getting started
2
15
3
16
### Kubectl Autocomplete
4
17
5
18
#### BASH
6
19
7
20
```bash
8
-
source<(kubectl completion bash)#active l'auto-complétion pour bash dans le shell courant, le paquet bash-completion devant être installé au préalable
9
-
echo"source <(kubectl completion bash)">>~/.bashrc #ajoute l'auto-complétion de manière permanente à votre shell bash
21
+
source<(kubectl completion bash)#setup autocomplete in bash into the current shell, bash-completion package should be installed first.
22
+
echo"source <(kubectl completion bash)">>~/.bashrc #add autocomplete permanently to your bash shell.
10
23
```
11
24
12
25
You can also use a shorthand alias for `kubectl` that also works with completion:
@@ -19,8 +32,8 @@ complete -F __start_kubectl k
19
32
#### ZSH
20
33
21
34
```zsh
22
-
source<(kubectl completion zsh)#active l'auto-complétion pour zsh dans le shell courant
23
-
echo"if [$commands[kubectl] ]; then source <(kubectl completion zsh); fi">>~/.zshrc #ajoute l'auto-complétion de manière permanente à votre shell zsh
35
+
source<(kubectl completion zsh)#setup autocomplete in zsh into the current shell
36
+
echo"[[$commands[kubectl] ]] && source <(kubectl completion zsh)">>~/.zshrc #add autocomplete permanently to your zsh shell
|`kubectl logs -l name=myLabel -c my-container`| dump pod logs, with label name=myLabel (stdout) |
277
+
|`kubectl logs my-pod -c my-container --previous`| dump pod container logs (stdout, multi-container case) for a previous instantiation of a container |
278
+
|`kubectl logs -f my-pod`| stream pod logs (stdout) |
|`kubectl logs -f -l name=myLabel --all-containers`| stream all pods logs with label name=myLabel (stdout) |
281
+
|`kubectl run -i --tty busybox --image=busybox -- sh`| Run pod as interactive shell |
282
+
|`kubectl run nginx --image=nginx --restart=Never -n mynamespace`| Run pod nginx in a specific namespace |
283
+
|`kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml`| Run pod nginx and write its spec into a file called pod.yaml |
284
+
|`kubectl attach my-pod -i`| Attach to Running Container |
285
+
|`kubectl port-forward my-pod 5000:6000`| Listen on port 5000 on the local machine and forward to port 6000 on my-pod |
286
+
|`kubectl exec my-pod -- ls /`| Run command in existing pod (1 container case) |
287
+
|`kubectl exec my-pod -c my-container -- ls /`| Run command in existing pod (multi-container case) |
288
+
|`kubectl top pod POD_NAME --containers`| Show metrics for a given pod and its containers |
289
+
279
290
280
291
### Interacting with Nodes and Cluster
281
292
@@ -332,7 +343,7 @@ Examples using `-o=custom-columns`:
332
343
# All images running in a cluster
333
344
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
334
345
335
-
# All images excluding "k8s.gcr.io/coredns:1.6.2"
346
+
# All images excluding "k8s.gcr.io/coredns:1.6.2"
336
347
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="k8s.gcr.io/coredns:1.6.2")].image'
337
348
338
349
# All fields under metadata regardless of name
@@ -345,8 +356,8 @@ More examples in the kubectl [reference documentation](https://kubernetes.io/doc
345
356
346
357
Kubectl verbosity is controlled with the `-v` or `--v` flags followed by an integer representing the log level. General Kubernetes logging conventions and the associated log levels are described [here](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md).
|`--v=0`| Generally useful for this to always be visible to a cluster operator. |
351
362
|`--v=1`| A reasonable default log level if you don’t want verbosity. |
352
363
|`--v=2`| Useful steady state information about the service and important log messages that may correlate to significant changes in the system. This is the recommended default log level for most systems. |
0 commit comments