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
> kubectl apply -f ./dir # create resource(s) in all manifest files in dir
36
+
> kubectl apply -f https://git.io/vPieo # create resource(s) from url
37
+
> kubectl create deployment nginx --image=nginx # start a single instance of nginx
38
+
> kubectl explain pods # get the documentation for pod manifests
39
+
40
+
# create multiple YAML objects from stdin
41
+
cat <<EOF | kubectl apply -f -
42
+
apiVersion: v1
43
+
kind: Pod
44
+
metadata:
45
+
name: busybox-sleep
46
+
spec:
47
+
containers:
48
+
- name: busybox
49
+
image: busybox
50
+
args:
51
+
- sleep
52
+
- "1000000"
53
+
---
54
+
apiVersion: v1
55
+
kind: Pod
56
+
metadata:
57
+
name: busybox-sleep-less
58
+
spec:
59
+
containers:
60
+
- name: busybox
61
+
image: busybox
62
+
args:
63
+
- sleep
64
+
- "1000"
65
+
EOF
66
+
67
+
# create a secret with several keys
68
+
cat <<EOF | kubectl apply -f -
69
+
apiVersion: v1
70
+
kind: Secret
71
+
metadata:
72
+
name: mysecret
73
+
type: Opaque
74
+
data:
75
+
password: $(echo -n "s33msi4"| base64 -w0)
76
+
username: $(echo -n "jane"| base64 -w0)
77
+
EOF
78
+
```
79
+
22
80
#### Nodes
23
-
```bash
81
+
```sh
24
82
> kubectl get no # get the nodes you have
25
83
> kubectl get no -o wide # get the wide description of the nodes
26
84
> kubectl get no --all-namespaces # get the all available nodes in the cluster
27
85
> kubectl get no --show-labels=[bool(true/false)] # get the cluster info with the labels
28
86
> kubectl get no -o json # get the node output in json
87
+
> kubectl get node --selector='!node-role.kubernetes.io/master'# get all worker nodes (use a selector to exclude results that have a label named 'node-role.kubernetes.io/master')
29
88
> kubectl get no --watch=true # get the status of the nodes by every 2 seconds
30
89
> kubectl get no --sort-by='{.metadata.name}'# get the sorted info of the nodes
31
90
> kubectl get --filename=[path to the directory or yaml file] # get the status of nodes using file path
@@ -39,30 +98,79 @@ What is Kubernetes?
39
98
> kubectl delete no [node_name] # to delete the node with give node name
40
99
```
41
100
#### Pods
42
-
```bash
43
-
> kubectl get po # get the pods in the current namespace
44
-
> kubectl get po -o wide # get the pods in the current namespace in wide
45
-
> kubectl get po --show-labels # get the pods with their labels
46
-
> kubectl get po -l app=nginx # get the pods with the label name app=nginx
47
-
> kubectl get po -o yaml # get the pods output in yaml format
48
-
> kubectl get pod [pod_name] -o yaml --export # get the output in yaml format of a particular pod
49
-
> kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml # export the output of pod in some file
50
-
> kubectl get pods --field-selector status.phase=Running # check that pods which are running
51
-
> kubectl get po --all-namespaces # get pods of all the namespaces
52
-
> kubectl get po --allow-missing-template-keys=true # If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
53
-
> kubectl get po --chunk-size=500 # Return large lists in chunks rather than all at once. Pass 0 to disable.
54
-
> kubectl get po -o json # get the pods in json format
55
-
> kubectl get po --watch=true # apply watch to get the status of every pod in 2 seconds
56
-
> kubectl get po --sort-by='{.metadata.name}'# get the sorted info about the pods
57
-
> kubectl describe po # describe the pods
58
-
> kubectl describe po --all-namespaces # describe the pods in all namespaces
59
-
> kubectl describe po [pod_name] # describe the pod given with the name
60
-
> kubectl delete po [pod_name] # delete the given name pod
61
-
> kubectl delete pods --all # delete all the pods in current namespace
101
+
```sh
102
+
> kubectl get po # get the pods in the current namespace
103
+
> kubectl get po -o wide # get the pods in the current namespace in wide
104
+
> kubectl get po --show-labels # get the pods with their labels
105
+
> kubectl get po -l app=nginx # get the pods with the label name app=nginx
106
+
> kubectl get po -o yaml # get the pods output in yaml format
107
+
> kubectl get pod [pod_name] -o yaml --export # get the output in yaml format of a particular pod
108
+
> kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'# list pods sorted by restart count
109
+
> kubectl get pods --selector=app=cassandra -o \
110
+
jsonpath='{.items[*].metadata.labels.version}'# get the version label of all pods with label app=cassandra
111
+
> kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml # export the output of pod in some file
112
+
> kubectl get pods --field-selector status.phase=Running # check that pods which are running
113
+
> kubectl get po --all-namespaces # get pods of all the namespaces
114
+
> kubectl get po --allow-missing-template-keys=true # If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
115
+
> kubectl get po --chunk-size=500 # Return large lists in chunks rather than all at once. Pass 0 to disable.
116
+
> kubectl get po -o json # get the pods in json format
117
+
> kubectl get po --watch=true # apply watch to get the status of every pod in 2 seconds
118
+
> kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}'| cut -d/ -f3 # list all containerIDs of initContainer of all pods Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
119
+
> kubectl get po --sort-by='{.metadata.name}'# get the sorted info about the pods
120
+
> kubectl describe po # describe the pods
121
+
> kubectl describe po --all-namespaces # describe the pods in all namespaces
122
+
> kubectl describe po [pod_name] # describe the pod given with the name
123
+
> kubectl delete po [pod_name] # delete the given name pod
124
+
> kubectl delete pods --all # delete all the pods in current namespace
125
+
```
126
+
127
+
#### Rollout
128
+
```sh
129
+
> kubectl set image deployment/frontend www=image:v2 # rolling update "www" containers of "frontend" deployment, updating the image
130
+
> kubectl rollout history deployment/frontend # check the history of deployments including the revision
131
+
> kubectl rollout undo deployment/frontend # rollback to the previous deployment
132
+
> kubectl rollout undo deployment/frontend --to-revision=2 # rollback to a specific revision
133
+
> kubectl rollout status -w deployment/frontend # watch rolling update status of "frontend" deployment until completion
134
+
> kubectl rollout restart deployment/frontend # rolling restart of the "frontend" deployment
> kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'# update a container's image; spec.containers[*].name is required because it's a merge key
141
+
> kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'# update a container's image using a json patch with positional arrays
142
+
> kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'# disable a deployment livenessProbe using a json patch with positional arrays
143
+
> kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'# add a new element to a positional array
144
+
```
145
+
146
+
#### Scale
147
+
```sh
148
+
> kubectl scale --replicas=3 rs/foo # scale a replicaset named 'foo' to 3
149
+
> kubectl scale --replicas=3 -f foo.yaml # scale a resource specified in "foo.yaml" to 3
150
+
> kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # if the deployment named mysql's current size is 2, scale mysql to 3
0 commit comments