Skip to content

Commit 7391107

Browse files
authored
Update README.md
1 parent f8b8912 commit 7391107

File tree

1 file changed

+147
-38
lines changed

1 file changed

+147
-38
lines changed

README.md

+147-38
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,81 @@ What is Kubernetes?
1010
> kubectl config current-context # get the current context of the cluster
1111
> kubectl config get-clusters # get the k8s cluster
1212
> kubectl config get-contexts # get the contexts that cluster acquired
13+
> kubectl config view -o jsonpath='{.users[].name}' # display the first user
14+
> kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
15+
> kubectl config use-context my-cluster-name # set the default context to my-cluster-name
1316
> kubectl config rename-context # to rename the context of the cluster
1417
> kubectl config set-context --current --namespace=[your namespace] # change the current context of the cluster
1518
> kubectl config view # view the config of the cluster
1619
> kubectl cluster-info # get the info about the cluster dns and master node
20+
> kubectl config unset users.foo # delete user foo
1721
> kubectl get componentstatuses # get the status of the components in the cluster like scheduler
22+
> kubectl config set-context --current --namespace=ggckad-s2 # permanently save the namespace for all subsequent kubectl commands in that context
23+
> kubectl config set-credentials kubeuser/foo.kubernetes.com \
24+
--username=kubeuser --password=kubepassword # add a new cluster to your kubeconf that supports basic auth
25+
> kubectl config set-context gce --user=cluster-admin --namespace=foo \
26+
&& kubectl config use-context gce # set a context utilizing a specific username and namespace
1827
```
1928

2029
## Viewing Resource Information
2130

31+
#### Apply
32+
```sh
33+
> kubectl apply -f ./my-manifest.yaml # create resource(s)
34+
> kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
35+
> 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+
2280
#### Nodes
23-
```bash
81+
```sh
2482
> kubectl get no # get the nodes you have
2583
> kubectl get no -o wide # get the wide description of the nodes
2684
> kubectl get no --all-namespaces # get the all available nodes in the cluster
2785
> kubectl get no --show-labels=[bool(true/false)] # get the cluster info with the labels
2886
> 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')
2988
> kubectl get no --watch=true # get the status of the nodes by every 2 seconds
3089
> kubectl get no --sort-by='{.metadata.name}' # get the sorted info of the nodes
3190
> 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?
3998
> kubectl delete no [node_name] # to delete the node with give node name
4099
```
41100
#### 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
135+
```
136+
137+
#### Patching
138+
```sh
139+
> kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # partially update a node
140+
> 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
151+
> kubectl scale --replicas=5 rc/foo rc/bar rc/baz # scale multiple replication controllers
152+
```
153+
154+
#### Delete
155+
```sh
156+
> kubectl delete -f ./pod.json # delete a pod using the type and name specified in pod.json
157+
> kubectl delete pod,service baz foo # delete pods and services with same names "baz" and "foo"
158+
> kubectl delete pods,services -l name=myLabel # delete pods and services with label name=myLabel
159+
> kubectl -n my-ns delete pod,svc --all # delete all pods and services in namespace my-ns,
160+
```
161+
162+
# API-Resources
163+
```sh
164+
> kubectl api-resources --namespaced=true # all namespaced resources
165+
> kubectl api-resources --namespaced=false # all non-namespaced resources
166+
> kubectl api-resources -o name # all resources with simple output (just the resource name)
167+
> kubectl api-resources -o wide # all resources with expanded (aka "wide") output
168+
> kubectl api-resources --verbs=list,get # all resources that support the "list" and "get" request verbs
169+
> kubectl api-resources --api-group=extensions # all resources in the "extensions" API group
62170
```
63171

64172
#### Namespaces
65-
```bash
173+
```sh
66174
> kubectl get ns # get all the namespaces
67175
> kubectl get ns -o yaml # get the output of namespace in yaml format
68176
> kubectl get ns -o json # get the output of namespace in json format
@@ -72,21 +180,22 @@ What is Kubernetes?
72180
```
73181

74182
#### Services
75-
```bash
76-
> kubectl get svc # get the services in current namespace
77-
> kubectl get svc -o wide # get the services in wide format
78-
> kubectl get svc -o yaml # get the services in yaml format
79-
> kubectl get svc --show-labels # get the services with their labels
80-
> kubectl get svc --all-namespaces # get services in all namespaces
81-
> kubectl describe svc # describe all the services
82-
> kubectl describe svc [service_name] # describe the given service name
83-
> kubectl describe svc --all-namespaces # describe the services in all namespaces
84-
> kubectl delete svc -l key=value # delete the svc with the given label
85-
> kubectl delete svc --all # delete all the svc in current namespace
183+
```sh
184+
> kubectl get svc # get the services in current namespace
185+
> kubectl get svc -o wide # get the services in wide format
186+
> kubectl get svc -o yaml # get the services in yaml format
187+
> kubectl get svc --show-labels # get the services with their labels
188+
> kubectl get svc --all-namespaces # get services in all namespaces
189+
> kubectl get services --sort-by=.metadata.name # list the services sorted by name
190+
> kubectl describe svc # describe all the services
191+
> kubectl describe svc [service_name] # describe the given service name
192+
> kubectl describe svc --all-namespaces # describe the services in all namespaces
193+
> kubectl delete svc -l key=value # delete the svc with the given label
194+
> kubectl delete svc --all # delete all the svc in current namespace
86195
```
87196

88197
#### Deployments
89-
```bash
198+
```sh
90199
> kubectl get deploy # get the deployments in current namespace
91200
> kubectl get deploy -o wide # get the deployments in wide info
92201
> kubectl get deploy -o yaml # get the deployments in yaml format
@@ -99,7 +208,7 @@ What is Kubernetes?
99208
```
100209

101210
#### ConfigMaps
102-
```bash
211+
```sh
103212
> kubectl get cm # get the current config map
104213
> kubectl get cm -n=[namespace] # get the config map given namespace
105214
> kubectl get cm -n=[namespace] -o yaml # get the config map given namespace in yaml format
@@ -112,7 +221,7 @@ What is Kubernetes?
112221
```
113222

114223
#### Logs
115-
```bash
224+
```sh
116225
> kubectl logs [pod_name] # get the logs of the given pod name
117226
> kubectl logs [pod_name] -n=[namespace] # get the logs of the given pod name in given namespace
118227
> kubectl logs --since=1h [pod_name] # get the logs of the given pod name since one hour
@@ -128,7 +237,7 @@ What is Kubernetes?
128237
```
129238

130239
#### Secrets
131-
```bash
240+
```sh
132241
> kubectl get secrets # get the secrets in current namespace
133242
> kubeclt get secrets -n=[namespace] # get the secrets in the given namespace
134243
> kubectl get secrets -n=[namespace] -o yaml # get the secrets in the given namespace in yaml format
@@ -140,7 +249,7 @@ What is Kubernetes?
140249
```
141250

142251
#### ReplicaSets
143-
```bash
252+
```sh
144253
> kubectl get rs # get the replica sets in current namespace
145254
> kubectl get rs -o wide # get the replica sets in wide format
146255
> kubectl get rs -o yaml # get the replica sets in yaml format

0 commit comments

Comments
 (0)