Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolnagpal authored Apr 9, 2020
1 parent f8b8912 commit 7391107
Showing 1 changed file with 147 additions and 38 deletions.
185 changes: 147 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,81 @@ What is Kubernetes?
> kubectl config current-context # get the current context of the cluster
> kubectl config get-clusters # get the k8s cluster
> kubectl config get-contexts # get the contexts that cluster acquired
> kubectl config view -o jsonpath='{.users[].name}' # display the first user
> kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
> kubectl config use-context my-cluster-name # set the default context to my-cluster-name
> kubectl config rename-context # to rename the context of the cluster
> kubectl config set-context --current --namespace=[your namespace] # change the current context of the cluster
> kubectl config view # view the config of the cluster
> kubectl cluster-info # get the info about the cluster dns and master node
> kubectl config unset users.foo # delete user foo
> kubectl get componentstatuses # get the status of the components in the cluster like scheduler
> kubectl config set-context --current --namespace=ggckad-s2 # permanently save the namespace for all subsequent kubectl commands in that context
> kubectl config set-credentials kubeuser/foo.kubernetes.com \
--username=kubeuser --password=kubepassword # add a new cluster to your kubeconf that supports basic auth
> kubectl config set-context gce --user=cluster-admin --namespace=foo \
&& kubectl config use-context gce # set a context utilizing a specific username and namespace
```

## Viewing Resource Information

#### Apply
```sh
> kubectl apply -f ./my-manifest.yaml # create resource(s)
> kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
> kubectl apply -f ./dir # create resource(s) in all manifest files in dir
> kubectl apply -f https://git.io/vPieo # create resource(s) from url
> kubectl create deployment nginx --image=nginx # start a single instance of nginx
> kubectl explain pods # get the documentation for pod manifests

# create multiple YAML objects from stdin
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep
spec:
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000000"
---
apiVersion: v1
kind: Pod
metadata:
name: busybox-sleep-less
spec:
containers:
- name: busybox
image: busybox
args:
- sleep
- "1000"
EOF

# create a secret with several keys
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: $(echo -n "s33msi4" | base64 -w0)
username: $(echo -n "jane" | base64 -w0)
EOF
```

#### Nodes
```bash
```sh
> kubectl get no # get the nodes you have
> kubectl get no -o wide # get the wide description of the nodes
> kubectl get no --all-namespaces # get the all available nodes in the cluster
> kubectl get no --show-labels=[bool(true/false)] # get the cluster info with the labels
> kubectl get no -o json # get the node output in json
> 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')
> kubectl get no --watch=true # get the status of the nodes by every 2 seconds
> kubectl get no --sort-by='{.metadata.name}' # get the sorted info of the nodes
> kubectl get --filename=[path to the directory or yaml file] # get the status of nodes using file path
Expand All @@ -39,30 +98,79 @@ What is Kubernetes?
> kubectl delete no [node_name] # to delete the node with give node name
```
#### Pods
```bash
> kubectl get po # get the pods in the current namespace
> kubectl get po -o wide # get the pods in the current namespace in wide
> kubectl get po --show-labels # get the pods with their labels
> kubectl get po -l app=nginx # get the pods with the label name app=nginx
> kubectl get po -o yaml # get the pods output in yaml format
> kubectl get pod [pod_name] -o yaml --export # get the output in yaml format of a particular pod
> kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml # export the output of pod in some file
> kubectl get pods --field-selector status.phase=Running # check that pods which are running
> kubectl get po --all-namespaces # get pods of all the namespaces
> 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.
> kubectl get po --chunk-size=500 # Return large lists in chunks rather than all at once. Pass 0 to disable.
> kubectl get po -o json # get the pods in json format
> kubectl get po --watch=true # apply watch to get the status of every pod in 2 seconds
> kubectl get po --sort-by='{.metadata.name}' # get the sorted info about the pods
> kubectl describe po # describe the pods
> kubectl describe po --all-namespaces # describe the pods in all namespaces
> kubectl describe po [pod_name] # describe the pod given with the name
> kubectl delete po [pod_name] # delete the given name pod
> kubectl delete pods --all # delete all the pods in current namespace
```sh
> kubectl get po # get the pods in the current namespace
> kubectl get po -o wide # get the pods in the current namespace in wide
> kubectl get po --show-labels # get the pods with their labels
> kubectl get po -l app=nginx # get the pods with the label name app=nginx
> kubectl get po -o yaml # get the pods output in yaml format
> kubectl get pod [pod_name] -o yaml --export # get the output in yaml format of a particular pod
> kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' # list pods sorted by restart count
> kubectl get pods --selector=app=cassandra -o \
jsonpath='{.items[*].metadata.labels.version}' # get the version label of all pods with label app=cassandra
> kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml # export the output of pod in some file
> kubectl get pods --field-selector status.phase=Running # check that pods which are running
> kubectl get po --all-namespaces # get pods of all the namespaces
> 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.
> kubectl get po --chunk-size=500 # Return large lists in chunks rather than all at once. Pass 0 to disable.
> kubectl get po -o json # get the pods in json format
> kubectl get po --watch=true # apply watch to get the status of every pod in 2 seconds
> 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.
> kubectl get po --sort-by='{.metadata.name}' # get the sorted info about the pods
> kubectl describe po # describe the pods
> kubectl describe po --all-namespaces # describe the pods in all namespaces
> kubectl describe po [pod_name] # describe the pod given with the name
> kubectl delete po [pod_name] # delete the given name pod
> kubectl delete pods --all # delete all the pods in current namespace
```

#### Rollout
```sh
> kubectl set image deployment/frontend www=image:v2 # rolling update "www" containers of "frontend" deployment, updating the image
> kubectl rollout history deployment/frontend # check the history of deployments including the revision
> kubectl rollout undo deployment/frontend # rollback to the previous deployment
> kubectl rollout undo deployment/frontend --to-revision=2 # rollback to a specific revision
> kubectl rollout status -w deployment/frontend # watch rolling update status of "frontend" deployment until completion
> kubectl rollout restart deployment/frontend # rolling restart of the "frontend" deployment
```

#### Patching
```sh
> kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # partially update a node
> 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
> 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
> 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
> kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]' # add a new element to a positional array
```

#### Scale
```sh
> kubectl scale --replicas=3 rs/foo # scale a replicaset named 'foo' to 3
> kubectl scale --replicas=3 -f foo.yaml # scale a resource specified in "foo.yaml" to 3
> kubectl scale --current-replicas=2 --replicas=3 deployment/mysql # if the deployment named mysql's current size is 2, scale mysql to 3
> kubectl scale --replicas=5 rc/foo rc/bar rc/baz # scale multiple replication controllers
```

#### Delete
```sh
> kubectl delete -f ./pod.json # delete a pod using the type and name specified in pod.json
> kubectl delete pod,service baz foo # delete pods and services with same names "baz" and "foo"
> kubectl delete pods,services -l name=myLabel # delete pods and services with label name=myLabel
> kubectl -n my-ns delete pod,svc --all # delete all pods and services in namespace my-ns,
```

# API-Resources
```sh
> kubectl api-resources --namespaced=true # all namespaced resources
> kubectl api-resources --namespaced=false # all non-namespaced resources
> kubectl api-resources -o name # all resources with simple output (just the resource name)
> kubectl api-resources -o wide # all resources with expanded (aka "wide") output
> kubectl api-resources --verbs=list,get # all resources that support the "list" and "get" request verbs
> kubectl api-resources --api-group=extensions # all resources in the "extensions" API group
```

#### Namespaces
```bash
```sh
> kubectl get ns # get all the namespaces
> kubectl get ns -o yaml # get the output of namespace in yaml format
> kubectl get ns -o json # get the output of namespace in json format
Expand All @@ -72,21 +180,22 @@ What is Kubernetes?
```

#### Services
```bash
> kubectl get svc # get the services in current namespace
> kubectl get svc -o wide # get the services in wide format
> kubectl get svc -o yaml # get the services in yaml format
> kubectl get svc --show-labels # get the services with their labels
> kubectl get svc --all-namespaces # get services in all namespaces
> kubectl describe svc # describe all the services
> kubectl describe svc [service_name] # describe the given service name
> kubectl describe svc --all-namespaces # describe the services in all namespaces
> kubectl delete svc -l key=value # delete the svc with the given label
> kubectl delete svc --all # delete all the svc in current namespace
```sh
> kubectl get svc # get the services in current namespace
> kubectl get svc -o wide # get the services in wide format
> kubectl get svc -o yaml # get the services in yaml format
> kubectl get svc --show-labels # get the services with their labels
> kubectl get svc --all-namespaces # get services in all namespaces
> kubectl get services --sort-by=.metadata.name # list the services sorted by name
> kubectl describe svc # describe all the services
> kubectl describe svc [service_name] # describe the given service name
> kubectl describe svc --all-namespaces # describe the services in all namespaces
> kubectl delete svc -l key=value # delete the svc with the given label
> kubectl delete svc --all # delete all the svc in current namespace
```

#### Deployments
```bash
```sh
> kubectl get deploy # get the deployments in current namespace
> kubectl get deploy -o wide # get the deployments in wide info
> kubectl get deploy -o yaml # get the deployments in yaml format
Expand All @@ -99,7 +208,7 @@ What is Kubernetes?
```

#### ConfigMaps
```bash
```sh
> kubectl get cm # get the current config map
> kubectl get cm -n=[namespace] # get the config map given namespace
> kubectl get cm -n=[namespace] -o yaml # get the config map given namespace in yaml format
Expand All @@ -112,7 +221,7 @@ What is Kubernetes?
```

#### Logs
```bash
```sh
> kubectl logs [pod_name] # get the logs of the given pod name
> kubectl logs [pod_name] -n=[namespace] # get the logs of the given pod name in given namespace
> kubectl logs --since=1h [pod_name] # get the logs of the given pod name since one hour
Expand All @@ -128,7 +237,7 @@ What is Kubernetes?
```

#### Secrets
```bash
```sh
> kubectl get secrets # get the secrets in current namespace
> kubeclt get secrets -n=[namespace] # get the secrets in the given namespace
> kubectl get secrets -n=[namespace] -o yaml # get the secrets in the given namespace in yaml format
Expand All @@ -140,7 +249,7 @@ What is Kubernetes?
```

#### ReplicaSets
```bash
```sh
> kubectl get rs # get the replica sets in current namespace
> kubectl get rs -o wide # get the replica sets in wide format
> kubectl get rs -o yaml # get the replica sets in yaml format
Expand Down

0 comments on commit 7391107

Please sign in to comment.