Skip to content

Commit 6885630

Browse files
committed
Create kubectl.md sheetcheat
1 parent 43fddd4 commit 6885630

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

kubectl.md

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
## Getting started
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+
---
213

3-
### Kubectl Autocomplete
14+
Getting started
15+
---------------
416

5-
#### BASH
17+
# Kubectl Autocomplete
18+
19+
## BASH
620

721
```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
22+
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
23+
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
1024
```
1125

1226
You can also use a shorthand alias for `kubectl` that also works with completion:
@@ -16,14 +30,14 @@ alias k=kubectl
1630
complete -F __start_kubectl k
1731
```
1832

19-
#### ZSH
33+
## ZSH
2034

2135
```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
36+
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
37+
echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc # add autocomplete permanently to your zsh shell
2438
```
2539

26-
### Kubectl Context and Configuration
40+
# Kubectl Context and Configuration
2741

2842
Set which Kubernetes cluster `kubectl` communicates with and modifies configuration information. See [Authenticating Across Clusters](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) with kubeconfig documentation for detailed config file information.
2943

@@ -57,11 +71,11 @@ kubectl config set-context gce --user=cluster-admin --namespace=foo \
5771
kubectl config unset users.foo # delete user foo
5872
```
5973

60-
### Apply
74+
# Apply
6175

6276
`apply` manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through running `kubectl apply`. This is the recommended way of managing Kubernetes applications on production. See [Kubectl Book](https://kubectl.docs.kubernetes.io/).
6377

64-
### Creating Objects
78+
# Creating Objects
6579

6680
Kubernetes manifests can be defined in YAML or JSON. The file extension `.yaml`, `.yml`, and `.json` can be used.
6781

@@ -113,7 +127,7 @@ data:
113127
EOF
114128
```
115129

116-
### Viewing, Finding Resources
130+
# Viewing, Finding Resources
117131

118132
```bash
119133
# Get commands with basic output
@@ -177,7 +191,7 @@ kubectl get events --sort-by=.metadata.creationTimestamp
177191
kubectl diff -f ./my-manifest.yaml
178192
```
179193

180-
### Updating Resources
194+
# Updating Resources
181195

182196
```bash
183197
kubectl set image deployment/frontend www=image:v2 # Rolling update "www" containers of "frontend" deployment, updating the image
@@ -204,7 +218,7 @@ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # Add an annota
204218
kubectl autoscale deployment foo --min=2 --max=10 # Auto scale a deployment "foo"
205219
```
206220

207-
### Patching Resources
221+
# Patching Resources
208222

209223
```bash
210224
# Partially update a node
@@ -223,7 +237,7 @@ kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "
223237
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
224238
```
225239

226-
### Editing Resources
240+
# Editing Resources
227241

228242
Edit any API resource in your preferred editor.
229243

@@ -232,7 +246,7 @@ kubectl edit svc/docker-registry # Edit the service named d
232246
KUBE_EDITOR="nano" kubectl edit svc/docker-registry # Use an alternative editor
233247
```
234248

235-
### Scaling Resources
249+
# Scaling Resources
236250

237251
```bash
238252
kubectl scale --replicas=3 rs/foo # Scale a replicaset named 'foo' to 3
@@ -241,43 +255,41 @@ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # If the deplo
241255
kubectl scale --replicas=5 rc/foo rc/bar rc/baz # Scale multiple replication controllers
242256
```
243257

244-
### Deleting Resources
258+
# Deleting Resources
245259

246260
```bash
247-
kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
261+
kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
248262
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
249263
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
250264
kubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace my-ns,
251265
# Delete all pods matching the awk pattern1 or pattern2
252266
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
253267
```
254268

255-
### Interacting with running Pods
256-
257-
```bash
258-
kubectl logs my-pod # dump pod logs (stdout)
259-
kubectl logs -l name=myLabel # dump pod logs, with label name=myLabel (stdout)
260-
kubectl logs my-pod --previous # dump pod logs (stdout) for a previous instantiation of a container
261-
kubectl logs my-pod -c my-container # dump pod container logs (stdout, multi-container case)
262-
kubectl logs -l name=myLabel -c my-container # dump pod logs, with label name=myLabel (stdout)
263-
kubectl logs my-pod -c my-container --previous # dump pod container logs (stdout, multi-container case) for a previous instantiation of a container
264-
kubectl logs -f my-pod # stream pod logs (stdout)
265-
kubectl logs -f my-pod -c my-container # stream pod container logs (stdout, multi-container case)
266-
kubectl logs -f -l name=myLabel --all-containers # stream all pods logs with label name=myLabel (stdout)
267-
kubectl run -i --tty busybox --image=busybox -- sh # Run pod as interactive shell
268-
kubectl run nginx --image=nginx --restart=Never -n
269-
mynamespace # Run pod nginx in a specific namespace
270-
kubectl run nginx --image=nginx --restart=Never # Run pod nginx and write its spec into a file called pod.yaml
271-
--dry-run -o yaml > pod.yaml
272-
273-
kubectl attach my-pod -i # Attach to Running Container
274-
kubectl port-forward my-pod 5000:6000 # Listen on port 5000 on the local machine and forward to port 6000 on my-pod
275-
kubectl exec my-pod -- ls / # Run command in existing pod (1 container case)
276-
kubectl exec my-pod -c my-container -- ls / # Run command in existing pod (multi-container case)
277-
kubectl top pod POD_NAME --containers # Show metrics for a given pod and its containers
278-
```
279-
280-
### Interacting with Nodes and Cluster
269+
# Interacting with running Pods
270+
271+
| Command | Description |
272+
| ------- | ----------- |
273+
| `kubectl logs my-pod` | dump pod logs (stdout) |
274+
| `kubectl logs -l name=myLabel` | dump pod logs, with label name=myLabel (stdout) |
275+
| `kubectl logs my-pod --previous` | dump pod logs (stdout) for a previous instantiation of a container |
276+
| `kubectl logs my-pod -c my-container` | dump pod container logs (stdout, multi-container case) |
277+
| `kubectl logs -l name=myLabel -c my-container` | dump pod logs, with label name=myLabel (stdout) |
278+
| `kubectl logs my-pod -c my-container --previous` | dump pod container logs (stdout, multi-container case) for a previous instantiation of a container |
279+
| `kubectl logs -f my-pod` | stream pod logs (stdout) |
280+
| `kubectl logs -f my-pod -c my-container` | stream pod container logs (stdout, multi-container case) |
281+
| `kubectl logs -f -l name=myLabel --all-containers` | stream all pods logs with label name=myLabel (stdout) |
282+
| `kubectl run -i --tty busybox --image=busybox -- sh` | Run pod as interactive shell |
283+
| `kubectl run nginx --image=nginx --restart=Never -n mynamespace` | Run pod nginx in a specific namespace |
284+
| `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 |
285+
| `kubectl attach my-pod -i` | Attach to Running Container |
286+
| `kubectl port-forward my-pod 5000:6000` | Listen on port 5000 on the local machine and forward to port 6000 on my-pod |
287+
| `kubectl exec my-pod -- ls /` | Run command in existing pod (1 container case) |
288+
| `kubectl exec my-pod -c my-container -- ls /` | Run command in existing pod (multi-container case) |
289+
| `kubectl top pod POD_NAME --containers` | Show metrics for a given pod and its containers |
290+
291+
292+
# Interacting with Nodes and Cluster
281293

282294
```bash
283295
kubectl cordon my-node # Mark my-node as unschedulable
@@ -292,7 +304,7 @@ kubectl cluster-info dump --output-directory=/path/to/cluster-state # Dump cur
292304
kubectl taint nodes foo dedicated=special-user:NoSchedule
293305
```
294306

295-
#### Resource types
307+
## Resource types
296308

297309
List all supported resource types along with their shortnames, [API group](https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups), whether they are [namespaced](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces), and [Kind](https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects):
298310

@@ -311,7 +323,7 @@ kubectl api-resources --verbs=list,get # All resources that support the "l
311323
kubectl api-resources --api-group=extensions # All resources in the "extensions" API group
312324
```
313325

314-
#### Formatting output
326+
## Formatting output
315327

316328
To output details to your terminal window in a specific format, add the `-o` (or `--output`) flag to a supported `kubectl` command.
317329

@@ -332,7 +344,7 @@ Examples using `-o=custom-columns`:
332344
# All images running in a cluster
333345
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
334346

335-
# All images excluding "k8s.gcr.io/coredns:1.6.2"
347+
# All images excluding "k8s.gcr.io/coredns:1.6.2"
336348
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="k8s.gcr.io/coredns:1.6.2")].image'
337349

338350
# All fields under metadata regardless of name
@@ -341,12 +353,12 @@ kubectl get pods -A -o=custom-columns='DATA:metadata.*'
341353

342354
More examples in the kubectl [reference documentation](https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns).
343355

344-
#### Kubectl output verbosity and debugging
356+
## Kubectl output verbosity and debugging
345357

346358
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).
347359

348-
| Verbosity | Description |
349-
| ----------------------------------- | -------------------------------- |
360+
| Verbosity | Description |
361+
| --------- | ----------- |
350362
| `--v=0` | Generally useful for this to always be visible to a cluster operator. |
351363
| `--v=1` | A reasonable default log level if you don’t want verbosity. |
352364
| `--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. |
@@ -357,7 +369,8 @@ Kubectl verbosity is controlled with the `-v` or `--v` flags followed by an inte
357369
| `--v=8` | Display HTTP request contents. |
358370
| `--v=9` | Display HTTP request contents without truncation of contents. |
359371

360-
## See Also
372+
See Also
373+
--------
361374

362375
* [Official tutorial](https://kubernetes.io/docs/tutorials/)
363376
* Learn more about [Overview of kubectl](https://kubernetes.io/docs/reference/kubectl/overview/)

0 commit comments

Comments
 (0)